#include <stdio.h>
struct countyTotals{
int totalCorkCodes;
int totalDublinCodes;
int totalGalwayCodes;
int totalLimerickCodes;
int totalTiperaryCodes;
int totalWaterfordCodes;
int totalInvalidCountyCodes;
};
struct countyTotals freqOfIrishCounties (char cntyCodes[], int size);
int main(void) {
char cntyCodes[]= {'C', 'c', 'E', 'G', 'G', 'L', 'l', 'l', 'T', 'W', 'J', 'd'};
struct countyTotals countyFreqs = freqOfIrishCounties(cntyCodes,12);
printf("Cork County: %i\n", countyFreqs.
totalCorkCodes); printf("Dublin County: %i\n", countyFreqs.
totalDublinCodes); printf("Galway County: %i\n", countyFreqs.
totalGalwayCodes); printf("Limerick County: %i\n", countyFreqs.
totalLimerickCodes); printf("Tiperary County: %i\n", countyFreqs.
totalTiperaryCodes); printf("Waterford County: %i\n", countyFreqs.
totalWaterfordCodes); printf("InvalidCountyCodes: %i\n", countyFreqs.
totalInvalidCountyCodes);
return 0;
}
//**************************************************************
// Function: freqOfIrishCounties
//
// Purpose: Count the total number of occurrences of each county code
//
// Parameters:
//
// cntyCodes - character array that holds the list of county codes
// size - the number of elements in the cntyCodes array
//
// Returns: a structure that holds the total number of occurrences of each county code
//
//**************************************************************
struct countyTotals freqOfIrishCounties(char cntyCodes[], int size){
struct countyTotals myCountyTotals = {0,0,0,0,0,0,0}; // holds the country counts
// and initializes them to zero
for (int i = 0; i < size; i++) {
switch (cntyCodes[i]) { /*looping through the county codes in the list and using the cntycode as the basis for the switch statement */
case 'C':
case 'c':
myCountyTotals.totalCorkCodes++; /* if the cnty code is 'C' or 'c', then incrementing the count associated with total Cork county codes*/
break;
case 'D':
case 'd':
myCountyTotals.totalDublinCodes+=1;
break;
case 'G':
case 'g':
myCountyTotals.totalGalwayCodes+=1;
break;
case 'L':
case 'l':
myCountyTotals.totalLimerickCodes+=1;
break;
case 'T':
case 't':
myCountyTotals.totalTiperaryCodes+=1;
break;
case 'W':
case 'w':
myCountyTotals.totalWaterfordCodes+=1;
break;
default:
myCountyTotals.totalInvalidCountyCodes+=1; /*if none of the other conditions were met, this is an invalid county code and the variable
holding the total occurrences of invalid county codes should be incremented by 1 */
break;
}
}
// struct countyTotals specificCountyTotals = {totalCorkCodes, totalDublinCodes, totalGalwayCodes,
// totalLimerickCodes, totalTiperaryCodes,
// totalWaterfordCodes, totalInvalidCountyCodes};
/*populating the specificCountyTotals structure with the information on total occurrences of each county code*/
return myCountyTotals;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CgpzdHJ1Y3QgY291bnR5VG90YWxzewogICAgICAgIGludCB0b3RhbENvcmtDb2RlczsKICAgICAgICBpbnQgdG90YWxEdWJsaW5Db2RlczsKICAgICAgICBpbnQgdG90YWxHYWx3YXlDb2RlczsKICAgICAgICBpbnQgdG90YWxMaW1lcmlja0NvZGVzOwogICAgICAgIGludCB0b3RhbFRpcGVyYXJ5Q29kZXM7CiAgICAgICAgaW50IHRvdGFsV2F0ZXJmb3JkQ29kZXM7CiAgICAgICAgaW50IHRvdGFsSW52YWxpZENvdW50eUNvZGVzOyAgIAp9OwoKc3RydWN0IGNvdW50eVRvdGFscyBmcmVxT2ZJcmlzaENvdW50aWVzIChjaGFyIGNudHlDb2Rlc1tdLCBpbnQgc2l6ZSk7CgppbnQgbWFpbih2b2lkKSB7CgoJY2hhciBjbnR5Q29kZXNbXT0geydDJywgJ2MnLCAnRScsICdHJywgJ0cnLCAnTCcsICdsJywgJ2wnLCAnVCcsICdXJywgJ0onLCAnZCd9OwogICAgc3RydWN0IGNvdW50eVRvdGFscyBjb3VudHlGcmVxcyA9IGZyZXFPZklyaXNoQ291bnRpZXMoY250eUNvZGVzLDEyKTsKICAgIHByaW50ZigiQ29yayBDb3VudHk6ICVpXG4iLCBjb3VudHlGcmVxcy50b3RhbENvcmtDb2Rlcyk7CiAgICBwcmludGYoIkR1YmxpbiBDb3VudHk6ICVpXG4iLCBjb3VudHlGcmVxcy50b3RhbER1YmxpbkNvZGVzKTsKICAgIHByaW50ZigiR2Fsd2F5IENvdW50eTogJWlc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IGZyZXFPZklyaXNoQ291bnRpZXMoY2hhciBjbnR5Q29kZXNbXSwgaW50IHNpemUpewoKICAgIHN0cnVjdCBjb3VudHlUb3RhbHMgbXlDb3VudHlUb3RhbHMgPSB7MCwwLDAsMCwwLDAsMH07ICAvLyBob2xkcyB0aGUgY291bnRyeSBjb3VudHMKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAvLyBhbmQgaW5pdGlhbGl6ZXMgdGhlbSB0byB6ZXJvCgogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBzaXplOyBpKyspIHsgCiAgICAgICAgc3dpdGNoIChjbnR5Q29kZXNbaV0pIHsgLypsb29waW5nIHRocm91Z2ggdGhlIGNvdW50eSBjb2RlcyBpbiB0aGUgbGlzdCBhbmQgdXNpbmcgdGhlIGNudHljb2RlIGFzIHRoZSBiYXNpcyBmb3IgdGhlIHN3aXRjaCBzdGF0ZW1lbnQgKi8KICAgICAgICBjYXNlICdDJzoKICAgICAgICBjYXNlICdjJzoKICAgICAgICAgICAgbXlDb3VudHlUb3RhbHMudG90YWxDb3JrQ29kZXMrKzsgLyogaWYgdGhlIGNudHkgY29kZSBpcyAnQycgb3IgJ2MnLCB0aGVuIGluY3JlbWVudGluZyB0aGUgY291bnQgYXNzb2NpYXRlZCB3aXRoIHRvdGFsIENvcmsgY291bnR5IGNvZGVzKi8KICAgICAgICAgICAgYnJlYWs7CgogICAgICAgIGNhc2UgJ0QnOgogICAgICAgIGNhc2UgJ2QnOgogICAgICAgICAgICBteUNvdW50eVRvdGFscy50b3RhbER1YmxpbkNvZGVzKz0xOwogICAgICAgICAgICBicmVhazsKCiAgICAgICAgY2FzZSAnRyc6CiAgICAgICAgY2FzZSAnZyc6CiAgICAgICAgICAgIG15Q291bnR5VG90YWxzLnRvdGFsR2Fsd2F5Q29kZXMrPTE7CiAgICAgICAgICAgIGJyZWFrOwoKICAgICAgICBjYXNlICdMJzoKICAgICAgICBjYXNlICdsJzoKICAgICAgICAgICAgbXlDb3VudHlUb3RhbHMudG90YWxMaW1lcmlja0NvZGVzKz0xOwogICAgICAgICAgICBicmVhazsKICAgICAgICAKICAgICAgICBjYXNlICdUJzoKICAgICAgICBjYXNlICd0JzoKICAgICAgICAgICAgbXlDb3VudHlUb3RhbHMudG90YWxUaXBlcmFyeUNvZGVzKz0xOwogICAgICAgICAgICBicmVhazsKICAgICAgICAKICAgICAgICBjYXNlICdXJzoKICAgICAgICBjYXNlICd3JzoKICAgICAgICAgICAgbXlDb3VudHlUb3RhbHMudG90YWxXYXRlcmZvcmRDb2Rlcys9MTsKICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgCiAgICAgICAgZGVmYXVsdDoKICAgICAgICAgICAgbXlDb3VudHlUb3RhbHMudG90YWxJbnZhbGlkQ291bnR5Q29kZXMrPTE7IC8qaWYgbm9uZSBvZiB0aGUgb3RoZXIgY29uZGl0aW9ucyB3ZXJlIG1ldCwgdGhpcyBpcyBhbiBpbnZhbGlkIGNvdW50eSBjb2RlIGFuZCB0aGUgdmFyaWFibGUgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGhvbGRpbmcgdGhlIHRvdGFsIG9jY3VycmVuY2VzIG9mIGludmFsaWQgY291bnR5IGNvZGVzIHNob3VsZCBiZSBpbmNyZW1lbnRlZCBieSAxICovCiAgICAgICAgICAgIGJyZWFrOwogICAgICAgIH0KCiAgICB9CgogICAgLy8gc3RydWN0IGNvdW50eVRvdGFscyBzcGVjaWZpY0NvdW50eVRvdGFscyA9IHt0b3RhbENvcmtDb2RlcywgdG90YWxEdWJsaW5Db2RlcywgdG90YWxHYWx3YXlDb2RlcywKICAgIC8vICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgdG90YWxMaW1lcmlja0NvZGVzLCB0b3RhbFRpcGVyYXJ5Q29kZXMsCiAgICAvLyAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHRvdGFsV2F0ZXJmb3JkQ29kZXMsIHRvdGFsSW52YWxpZENvdW50eUNvZGVzfTsgCiAgICAvKnBvcHVsYXRpbmcgdGhlIHNwZWNpZmljQ291bnR5VG90YWxzIHN0cnVjdHVyZSB3aXRoIHRoZSBpbmZvcm1hdGlvbiBvbiB0b3RhbCBvY2N1cnJlbmNlcyBvZiBlYWNoIGNvdW50eSBjb2RlKi8KICAgIAogICAgcmV0dXJuIG15Q291bnR5VG90YWxzOwoKfQo=