fork download
  1. #include <stdio.h>
  2.  
  3. // **************************************************
  4. // Structure: countyTotals
  5. //
  6. // Description:
  7. // Holds the total count of each valid Irish county code.
  8. // Includes a count for invalid county codes as well.
  9. // **************************************************
  10. struct countyTotals
  11. {
  12. int totalCorkCodes;
  13. int totalDublinCodes;
  14. int totalGalwayCodes;
  15. int totalLimerickCodes;
  16. int totalTiperaryCodes;
  17. int totalWaterfordCodes;
  18. int totalInvalidCountryCodes;
  19. };
  20.  
  21. // **************************************************
  22. // Function: freqOfIrishCounties
  23. //
  24. // Description:
  25. // Counts the occurrences of each valid Irish county code
  26. // (Cork, Dublin, Galway, Limerick, Tiperary, Waterford)
  27. // in an array of characters. Both uppercase and lowercase
  28. // letters are accepted. Any invalid county code is also
  29. // counted separately.
  30. //
  31. // Parameters:
  32. // countyArrayList[] - array containing the county codes
  33. // size - number of elements in the array
  34. //
  35. // Returns:
  36. // A struct containing totals for each county and invalid codes.
  37. // **************************************************
  38. struct countyTotals freqOfIrishCounties(char countyArrayList[], int size)
  39. {
  40. struct countyTotals myCountyTotals = {0,0,0,0,0,0,0}; // holds the county counts
  41. // and initializes them to zero
  42.  
  43. // Review each of the county codes in the array
  44. for (int i = 0; i < size; i++)
  45. {
  46. // increments the total of the current county character value
  47. // note that case does not matter, both upper and lower count the same
  48. // towards the totals
  49. switch (countyArrayList[i])
  50. {
  51. case 'C':
  52. case 'c':
  53. myCountyTotals.totalCorkCodes++;
  54. break;
  55.  
  56. case 'D':
  57. case 'd':
  58. myCountyTotals.totalDublinCodes++;
  59. break;
  60.  
  61. case 'G':
  62. case 'g':
  63. myCountyTotals.totalGalwayCodes++;
  64. break;
  65.  
  66. case 'L':
  67. case 'l':
  68. myCountyTotals.totalLimerickCodes++;
  69. break;
  70.  
  71. case 'T':
  72. case 't':
  73. myCountyTotals.totalTiperaryCodes++;
  74. break;
  75.  
  76. case 'W':
  77. case 'w':
  78. myCountyTotals.totalWaterfordCodes++;
  79. break;
  80.  
  81. default:
  82. myCountyTotals.totalInvalidCountryCodes++;
  83. break;
  84. } // switch
  85. } // for
  86.  
  87. return (myCountyTotals);
  88. } // freqOfIrishCounties
  89.  
  90.  
  91. // **************************************************
  92. // Function: main
  93. //
  94. // Description:
  95. // Demonstrates the freqOfIrishCounties function with
  96. // a sample array of county codes and prints the results.
  97. // **************************************************
  98. int main(void)
  99. {
  100. char countyCodes[] = {'C', 'c', 'E', 'G', 'G', 'L', 'l', 'l', 'T', 'W', 'J', 'd'};
  101. int size = sizeof(countyCodes) / sizeof(countyCodes[0]);
  102.  
  103. struct countyTotals result = freqOfIrishCounties(countyCodes, size);
  104.  
  105. printf("Total Cork Codes: %d\n", result.totalCorkCodes);
  106. printf("Total Dublin Codes: %d\n", result.totalDublinCodes);
  107. printf("Total Galway Codes: %d\n", result.totalGalwayCodes);
  108. printf("Total Limerick Codes: %d\n", result.totalLimerickCodes);
  109. printf("Total Tiperary Codes: %d\n", result.totalTiperaryCodes);
  110. printf("Total Waterford Codes: %d\n", result.totalWaterfordCodes);
  111. printf("Total Invalid County Codes: %d\n", result.totalInvalidCountryCodes);
  112.  
  113. return 0;
  114. }
  115.  
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
stdout
Total Cork Codes: 2
Total Dublin Codes: 1
Total Galway Codes: 2
Total Limerick Codes: 3
Total Tiperary Codes: 1
Total Waterford Codes: 1
Total Invalid County Codes: 2