Question: #include #include #include #define NAME _ LENGTH 2 0 typedef struct studentRecord { int studentID; char name [ NAME _ LENGTH + 1 ] ;

#include
#include
#include
#define NAME_LENGTH 20
typedef struct studentRecord {
int studentID;
char name[NAME_LENGTH +1];
int exam1, exam2, exam3;
double examAvg;
char letterGrade;
} RECORD_TYPE;
int menu();
void loadRoster(FILE *infile, RECORD_TYPE **gradeRoster, int numStudents);
void processChoice(FILE *infile, FILE *outfile, int choice, RECORD_TYPE **gradeRoster, int numStudents);
void displayGradeInfo(RECORD_TYPE **gradeRoster, int numStudents);
void searchRoster(RECORD_TYPE **gradeRoster, int numStudents);
void writeGradeReport(FILE *outfile, RECORD_TYPE **gradeRoster, int numStudents);
char determineLetterGrade(double average);
int main(){
char *infilename = "roster.txt";
char *outfilename = "gradeReport.txt";
FILE *infile;
FILE *outfile;
int numStudents;
RECORD_TYPE **gradeRoster;
infile = fopen(infilename,"r");
if (infile == NULL){
printf("Error: Unable to open input file.
");
return 1;
}
outfile = fopen(outfilename,"w");
if (outfile == NULL){
printf("Error: Unable to open output file.
");
fclose(infile);
return 1;
}
fscanf(infile,"%d", &numStudents);
gradeRoster =(RECORD_TYPE **)malloc(numStudents * sizeof(RECORD_TYPE *));
if (gradeRoster == NULL){
printf("Error: Memory allocation failed.
");
fclose(infile);
fclose(outfile);
return 1;
}
loadRoster(infile, gradeRoster, numStudents);
int choice;
do {
choice = menu();
processChoice(infile, outfile, choice, gradeRoster, numStudents);
} while (choice !=4);
fclose(infile);
fclose(outfile);
for (int i =0; i < numStudents; i++){
free(gradeRoster[i]);
}
free(gradeRoster);
return 0;
}
void loadRoster(FILE *infile, RECORD_TYPE **gradeRoster, int numStudents){
int i, studentID, exam1, exam2, exam3;
char studentName[NAME_LENGTH +1];
for (i =0; i < numStudents; i++){
fscanf(infile,"%d %s %d %d %d", &studentID, studentName, &exam1, &exam2, &exam3);
gradeRoster[i]=(RECORD_TYPE *)malloc(sizeof(RECORD_TYPE));
if (gradeRoster[i]== NULL){
printf("Error: Memory allocation failed.
");
exit(1);
}
gradeRoster[i]->studentID = studentID;
strcpy(gradeRoster[i]->name, studentName);
gradeRoster[i]->exam1= exam1;
gradeRoster[i]->exam2= exam2;
gradeRoster[i]->exam3= exam3;
gradeRoster[i]->examAvg =(exam1+ exam2+ exam3)/3.0;
gradeRoster[i]->letterGrade = determineLetterGrade(gradeRoster[i]->examAvg);
}
}
int menu(){
int choice;
printf("
1) Display contents of the roster.
");
printf("2) Search the roster.
");
printf("3) Write a grade report.
");
printf("4) Quit.
");
printf("Enter your choice: ");
scanf("%d", &choice);
return choice;
}
void processChoice(FILE *infile, FILE *outfile, int choice, RECORD_TYPE **gradeRoster, int numStudents){
switch (choice){
case 1:
displayGradeInfo(gradeRoster, numStudents);
break;
case 2:
searchRoster(gradeRoster, numStudents);
break;
case 3:
writeGradeReport(outfile, gradeRoster, numStudents);
break;
case 4:
printf("Exiting...
");
break;
default:
printf("Invalid choice. Please try again.
");
}
}
void displayGradeInfo(RECORD_TYPE **gradeRoster, int numStudents){
printf("
Grade Information:
");
printf("ID\tName\tExam1\tExam2\tExam3\tAvg\tGrade
");
for (int i =0; i < numStudents; i++){
printf("%d\t%s\t%d\t%d\t%d\t%.2f\t%c
", gradeRoster[i]->studentID, gradeRoster[i]->name,
gradeRoster[i]->exam1, gradeRoster[i]->exam2, gradeRoster[i]->exam3,
gradeRoster[i]->examAvg, gradeRoster[i]->letterGrade);
}
}
void searchRoster(RECORD_TYPE **gradeRoster, int numStudents){
printf("
Search functionality:
Enter student ID to search: ");
int searchId[+1];
scanf("%s", searchName);
int found =0;
for (int i =0; i < numStudents; i++){
if (strcmp(gradeRoster[i]->name, searchName)==0){
printf("Student found:
");
printf("ID\tName\tExam1\tExam2\tExam3\tAvg\tGrade
");
printf("%d\t%s\t%d\t%d\t%d\t%.2f\t%c
", gradeRoster[i]->studentID, gradeRoster[i]->name,
gradeRoster[i]->exam1, gradeRoster[i]->exam2, gradeRoster[i]->exam3,
gradeRoster[i]->examAvg, gradeRoster[i]->letterGrade);
found =1;
break;
}
}
if (!found){
printf("Student not found.
");
}
}
void writeGradeReport(FILE * searchroster should be student ID not by name, this is 6th attempt

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!