Problem 1 Write an interactive program that maintains a clas
Problem 1
Write an interactive program that maintains a class grade sheet. The program should maintain the following information: the sum of all grades entered so far, the number of grades entered so far, the number of each type of grade (how many As, Bs, Cs, Ds, Fs) and should support 6 different options. At the beginning the program should output the following information:
This program maintains a class grade sheet. Options:
1 – enter new grade
2 – print current average
3 – clear grade sheet
4 – print total number of grades
5 – print grade report
6 – quit the program
Below is a description of what the program should do for each of the above options:
Add a new grade (values allowed are: A, B, C, D, and F). For this option the program should ask the user to enter a grade, as follows:
Enter a new grade (allowed values: A, B, C, D, F): A
If the user enters an incorrect grade, the program should check and prompt the user for a new grade again (use the same prompt as above).
Print current average. The program should print the current average as follows:
Current average is: <value printed here>
If no grades have yet been entered, the program should output:
No grades have yet been entered.
Clear grade sheet. For this option, the program should clear all currently stored information regarding the grades and print out the following message:
Grade sheet has been cleared.
Print total number of grades. For this option the program should print the total number of grades that have been entered as follows:
Current number of grades entered is: <value printed here>
Print an individual grade report (i.e., how many grades have been entered for a specific grade). For this option the program should ask the user for what grade should the report be:
Enter the grade for the report (allowed values: A, B, C, D, F): C
Total number of C grades is: <value printed here>
Quit the program. For this option, the program should end.
In order to compute the average of the current grades, your program should assume the following values: A = 4, B = 3, C = 2, D = 1, F = 0.
Save your program in a file called grades.c.
Challenge 1 for Problem 1 (5 extra credit points): Modify option 1 in the previous program to ask the user for how many grades will be entered and then will read as many grades from the input. The grades will be separated by spaces. For option 1 the program should now behave as follows:
Enter number of grades: 5
Enter 5 new grades (allowed values: A, B, C, D, F): A B A C D
Save your program in a file called grades_challenge.c.
Solution
#include <stdio.h>
#include <stdlib.h>
struct GradeManager{
int gradeA;
int gradeB;
int gradeC;
int gradeD;
int gradeF;
} gradeManager;
void printMenu();
void executeUserSelection(int option);
void initializeGrades();
void addNewGrade();
void printCurrentAverage();
void totalNumberOfGrades();
void printGradeReport();
int main()
{
int userSelection;
initializeGrades();
while(1)
{
printMenu();
scanf(\"%d\",&userSelection);
if(userSelection == 6)
break;
executeUserSelection(userSelection);
}
return (EXIT_SUCCESS);
}
void initializeGrades()
{
gradeManager.gradeA = 0;
gradeManager.gradeB = 0;
gradeManager.gradeC = 0;
gradeManager.gradeD = 0;
gradeManager.gradeF = 0;
}
void printMenu()
{
printf(\"1 - Enter new grade\ \");
printf(\"2 - Print current average\ \");
printf(\"3 - Clear grade sheet\ \");
printf(\"4 - Print total number of grades\ \");
printf(\"5 - Print grade report\ \");
printf(\"6 - Quit the program\ \");
}
void executeUserSelection(int option)
{
switch(option)
{
case 1:
addNewGrade();
break;
case 2:
printCurrentAverage();
break;
case 3:
initializeGrades();
break;
case 4:
totalNumberOfGrades();
break;
case 5:
printGradeReport();
break;
}
}
void addNewGrade()
{
char grade = \' \';
printf(\"Enter new grade: \");
scanf(\"%c\", &grade);
switch(grade)
{
case \'a\':
case \'A\':
gradeManager.gradeA++;
break;
case \'b\':
case \'B\':
gradeManager.gradeB++;
break;
case \'c\':
case \'C\':
gradeManager.gradeC++;
break;
case \'d\':
case \'D\':
gradeManager.gradeD++;
break;
case \'f\':
case \'F\':
gradeManager.gradeF++;
break;
}
}
void printCurrentAverage()
{
int totalCount = 0,temp;
int totalValue = 0;
temp = gradeManager.gradeA;
totalValue += temp*4;
totalCount += temp;
temp = gradeManager.gradeB;
totalValue += temp*3;
totalCount += temp;
temp = gradeManager.gradeC;
totalValue += temp*2;
totalCount += temp;
temp = gradeManager.gradeD;
totalValue += temp*1;
totalCount += temp;
temp = gradeManager.gradeF;
totalValue += temp*0;
totalCount += temp;
if(totalCount == 0)
{
printf(\"No grade have yet been entered.\ \");
}
}
void totalNumberOfGrades()
{
int totalCount = gradeManager.gradeA + gradeManager.gradeB + gradeManager.gradeC + gradeManager.gradeD + gradeManager.gradeF;
printf(\"Current number of grades entered is:%d\",totalCount);
}
void printGradeReport()
{
char grade;
printf(\"Enter the grade for the report(allowed values: A,B,C,D,F):\");
scanf(\"%c\",&grade);
switch(grade)
{
case \'a\':
case \'A\':
printf(\"Total number of A grades is:%d\",gradeManager.gradeA);
break;
case \'b\':
case \'B\':
printf(\"Total number of B grades is:%d\",gradeManager.gradeB);
break;
case \'c\':
case \'C\':
printf(\"Total number of C grades is:%d\",gradeManager.gradeC);
break;
case \'d\':
case \'D\':
printf(\"Total number of D grades is:%d\",gradeManager.gradeD);
break;
case \'f\':
case \'F\':
printf(\"Total number of F grades is:%d\",gradeManager.gradeF);
break;
}
}



