fork download
  1. #include <stdio.h>
  2.  
  3. struct countyTotals
  4. {
  5. int totalCorkCodes;
  6. int totalDublinCodes;
  7. int totalGalwayCodes;
  8. int totalLimerickCodes;
  9. int totalTiperaryCodes;
  10. int totalWaterfordCodes;
  11. int totalInvalidCountryCodes;
  12. };
  13.  
  14.  
  15. // **************************************************
  16. // Function: freqOfIrishCounties
  17. //
  18. // Description: Shows the total number of occurrences
  19. // of each county code (same as where the cars are
  20. // registered) after being given an array of any size
  21. //
  22. // Parameters: countyArrayList[] - array with county list
  23. // size - size of the array
  24. //
  25. // Returns: structure updated with total number of
  26. // occurrences for each county code
  27. //
  28. // ***************************************************
  29. // add function block comments
  30. struct countyTotals freqOfIrishCounties(char countyArrayList[], int size)
  31. {
  32.  
  33. struct countyTotals myCountyTotals = {0,0,0,0,0,0,0}; // holds the country counts
  34. // and initializes them to zero
  35.  
  36. // Review each of the county codes in the array
  37. for(int i = 0; i < size; i++)
  38. {
  39. // increments the total of the current county character value
  40. // ... note that case does not matter, both upper and lower count the same
  41. // towards the totals
  42. switch (countyArrayList[i])
  43. {
  44.  
  45. // add case statements ...
  46. // for each county
  47. // and increment members (the counts) as needed
  48. // in the struct variable named myCountyTotals
  49. case 'C': case 'c':
  50. myCountyTotals.totalCorkCodes++;
  51. break;
  52. case 'D': case 'd':
  53. myCountyTotals.totalDublinCodes++;
  54. break;
  55. case 'G': case 'g':
  56. myCountyTotals.totalGalwayCodes++;
  57. break;
  58. case 'L': case 'l':
  59. myCountyTotals.totalLimerickCodes++;
  60. break;
  61. case 'T': case 't':
  62. myCountyTotals.totalTiperaryCodes++;
  63. break;
  64. case 'W': case 'w':
  65. myCountyTotals.totalWaterfordCodes++;
  66. break;
  67. default:
  68. myCountyTotals.totalInvalidCountryCodes++;
  69.  
  70. } // switch
  71. } // for
  72.  
  73. return (myCountyTotals);
  74.  
  75. } // freqOfIrishCounties
  76. int main(void) {
  77. // your code goes here
  78. char countyCodes[] = {'C', 'c', 'E', 'G', 'G', 'L', 'l', 'l', 'T', 'W', 'J', 'd'};
  79.  
  80. struct countyTotals result = freqOfIrishCounties(countyCodes, 12);
  81.  
  82. printf("Cork: %d\n", result.totalCorkCodes);
  83. printf("Dublin: %d\n", result.totalDublinCodes);
  84. printf("Galway: %d\n", result.totalGalwayCodes);
  85. printf("Limerick: %d\n", result.totalLimerickCodes);
  86. printf("Tipperary: %d\n", result.totalTiperaryCodes);
  87. printf("Waterford: %d\n", result.totalWaterfordCodes);
  88. printf("Invalid County Codes: %d\n", result.totalInvalidCountryCodes);
  89.  
  90. return 0;
  91. return 0;
  92. }
  93.  
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
Cork: 2
Dublin: 1
Galway: 2
Limerick: 3
Tipperary: 1
Waterford: 1
Invalid County Codes: 2