Here's my problem: I need a loop that goes through a char array and calculates how many people had the same grade, the amount of stars for each grade (one for each student with the same mark) and a percentage (please ignore it for this question):
void displayReport(char grade[][MAX_LETTER_SIZE]) {
//Declare variables
int i = 0;
int A=0, a=0, B=0, b=0, C=0, c=0, D=0, d=0, F=0;
char sstar[2] = "*";
char star[9][10] = {"", "", "", "", "", "", "", "", "", "", "", ""};
char letterg[9][3] = {"A+", "A", "B+", "B", "C+", "C", "D+", "D", "F"};
float bperecentage = (1/12)*100;
float percentage[12];
/*This loops though the grade array and it counts the number of students who got the same mark,
The number of stars to display and the percentage of the people who had the same mark
*/
for (i = 0; i < 12; i++) {
if (grade[i] == "A+") {
A++;
strcat(star[0], sstar);
percentage[i] = percentage[i] + bperecentage;
break;
}
else if (grade[i] == "A") {
a++;
strcat(star[1], sstar);
percentage[i] = percentage[i] + bperecentage;
break;
}
else if (grade[i] == "B+") {
B++;
strcat(star[2], sstar);
percentage[i] = percentage[i] + bperecentage;
break;
}
else if (grade[i] == "B") {
b++;
strcat(star[3], sstar);
percentage[i] = percentage[i] + bperecentage;
break;
}
else if (grade[i] == "C+") {
C++;
strcat(star[4], sstar);
percentage[i] = percentage[i] + bperecentage;
break;
}
else if (grade[i] == "C") {
c++;
strcat(star[5], sstar);
percentage[i] = percentage[i] + bperecentage;
break;
}
else if (grade[i] == "D+") {
D++;
strcat(star[6], sstar);
percentage[i] = percentage[i] + bperecentage;
break;
}
else if (grade[i] == "D") {
d++;
strcat(star[7], sstar);
percentage[i] = percentage[i] + bperecentage;
break;
}
else {
F++;
strcat(star[8], sstar);
percentage[i] = percentage[i] + bperecentage;
break;
}
}
//This loop in needed to round the persentages
for (i = 0; i < 9; i++) {
float roundf(float percentage[i]);
}
//This prints all the collected data in this function
printf("BTP100 Class size:12\n");
printf("%s %s %d (%f%%)\n", letterg[0], star[0], A, percentage[0]);
printf("%s %s %d (%f%%)\n", letterg[1], star[1], a, percentage[1]);
printf("%s %s %d (%f%%)\n", letterg[2], star[2], B, percentage[2]);
printf("%s %s %d (%f%%)\n", letterg[3], star[3], b, percentage[3]);
printf("%s %s %d (%f%%)\n", letterg[4], star[4], C, percentage[4]);
printf("%s %s %d (%f%%)\n", letterg[5], star[5], c, percentage[5]);
printf("%s %s %d (%f%%)\n", letterg[6], star[6], D, percentage[6]);
printf("%s %s %d (%f%%)\n", letterg[7], star[7], d, percentage[7]);
printf("%s %s %d (%f%%)\n", letterg[8], star[8], F, percentage[8]);
}
when I out put I only get 1 star and one student with a grade of f, which is wrong because they are suppose to be two. But my main concern is why it does not calculate anything for the other grades.
Please note that grade is a string of arrays and it has the final grade of each student for a class of 12 student.
Aucun commentaire:
Enregistrer un commentaire