In: Computer Science
write a program in C
Write a function that is passed an array of characters containing letter grades of A, B, C, D, and F, and returns the total number of occurrences of each letter grade. Your function should accept both lower and upper case grades, for example, both 'b' and 'B' should be bucketed into your running total for B grades. Any grade that is invalid should be bucketed as a grade of 'I' for Incomplete. You must use a switch statement, and your function should accept an array of any size. Feel free to pass in the array size as a parameter so you know how many grade values you'll need to check in your loop. For example, if you passed a function the following array: char grades [ ] = {'A', 'b', 'C', 'x', 'D', 'c', 'F', 'B', 'Y', 'B', 'B', 'A'}; It would return the following information within a structure (gradeTotals) to the calling function. Grade Total --------- ------ A 2 B 4 C 2 D 1 F 1 I 2 Hint: Design the function as: struct gradeTotals gradeCount (char gradeArray [ ], int arraySize)
//C code
#include<stdio.h>
//struct
struct gradeTotals
{
char gradeA, gradeB, gradeC,
gradeD, gradeF, gradeI;
int countA=0, countB=0, countC=0,
countD=0, countF=0, countI =
0;
};
//Fucntion prototype
struct gradeTotals gradeCount(char gradeArray[], int
arraySize);
//Function defintion
struct gradeTotals gradeCount(char gradeArray[], int
arraySize)
{
gradeTotals gt;
for (int i = 0; i < arraySize; i++)
{
// match both lower and upper case
grades
switch (gradeArray[i])
{
case 'a':
case 'A':
gt.gradeA =
'A';
gt.countA++;
break;
case 'b':
case 'B':
gt.gradeB =
'B';
gt.countB++;
break;
case 'c':
case 'C':
gt.gradeC =
'C';
gt.countC++;
break;
case 'd':
case 'D':
gt.gradeD =
'D';
gt.countD++;
break;
case 'F':
case 'f':
gt.gradeF =
'F';
gt.countF++;
break;
default:
gt.gradeI =
'I';
gt.countI++;
break;
}
}
return gt;
}
//main function
int main()
{
char grades[] = { 'A', 'b', 'C', 'x', 'D', 'c', 'F',
'B', 'Y', 'B', 'B', 'A' };
//call function
gradeTotals gt = gradeCount(grades, 12);
//print info
printf("Grade Total------%c%d %c%d %c%d %c%d %c%d
%c%d\n", gt.gradeA, gt.countA,
gt.gradeB, gt.countB, gt.gradeC,
gt.countC, gt.gradeD, gt.countD, gt.gradeF, gt.countF, gt.gradeI,
gt.countI);
return 0;
}
//Output
//If you need any help regarding this solution........ please leave a comment......... thanks