Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2) 42] Write a program to manage the grades of students in a class. To represent

ID: 3605358 • Letter: 2

Question

2) 42] Write a program to manage the grades of students in a class. To represent the information of each student define a structure type named student. The members of this structure should be: 1. an integer that represents the student's ID number, 2. an array of 15 characters storing the first name, 3. an array of 15 characters storing the last name, 4. an integer that represents the project 1 grade, 5. an integer that represents the project 2 grade, 6. a floating point representing the final course grade To store the list of students use an array of pointers of type student*. Each pointer in the array has to point to a structure variable of type student representing the information of one student. The array has to be SORTED in increasing order of students' ID numbers Define the following functions student **create_class list( char *filename, int *sizePtr) Function create_class list reads the students' ID numbers and names from the input file (a text file), allocates the memory necessary to store the list of students and initializes the students ID's and names

Explanation / Answer

Given below is the code with output and sample input files. It also generates the output in the output file specified. Please do rate the answer if it helped. Thank you.

#include <stdio.h>
#include <stdlib.h>
typedef struct student
{
int ID;
char fname[15];
char lname[15];
int prj1Grade;
int prj2Grade;
float finalGrade;
}student;


student **create_class_list(char *filename, int *sizePtr);
int find(int idNo, student **list, int size);
void input_grades(char *filename, student **list, int size);
void compute_final_course_grades(student **list, int size);
void output_final_course_grades(char *filename, student **list, int size);
void print_list(student **list, int size);
void withdraw(int idNo, student **list, int *sizePtr);
void destroy_list(student **list, int *sizePtr);

int main()
{
char namesFile[30], gradesFile[30], outFile[30];
student **list = NULL;
int size, id;
printf("Enter the input filename containing names: ");
scanf("%s", namesFile);
list = create_class_list(namesFile, &size);
printf("Enter the filename containing grades: ");
scanf("%s", gradesFile);
input_grades(gradesFile, list, size);

printf("Calculating final grades ");
compute_final_course_grades(list, size);

printf("Enter the output filename where to write final grades: ");
scanf("%s", outFile);
output_final_course_grades(outFile, list, size);

printf("Displaying the student list ");
print_list(list, size);

while(1)
{
printf("Enter id of student to withdraw (enter -1 to exit): ");
scanf("%d", &id);
if(id == -1)
break;
withdraw(id, list, &size);
print_list(list, size);
}
destroy_list(list, &size);
}

student **create_class_list(char *filename, int *sizePtr)
{
FILE *fp = fopen(filename, "r");
int i, n;
student** s;
if(fp == NULL)
{
printf("Error: Could not open file %s ", filename);
exit(1);
}

fscanf(fp, "%d", &n); //get no . of records
*sizePtr = n;
s = (student**)malloc(n * (sizeof(student*)));
for(i = 0; i < n; i++)
{
s[i] = (student*) malloc(sizeof(student));
fscanf(fp, "%d %s %s", &s[i]->ID, s[i]->fname, s[i]->lname);
s[i]->prj1Grade = 0;
s[i]->prj2Grade = 0;
s[i]->finalGrade = 0;
}
fclose(fp);
return s;
}


int find(int idNo, student **list, int size)
{
int i;
for(i = 0; i < size; i++)
{
if(list[i]->ID == idNo)
return i;
}

return -1;
}


void input_grades(char *filename, student **list, int size)
{
FILE *fp = fopen(filename, "r");
int i, index, ID;
int g1, g2;
if(fp == NULL)
{
printf("Error: Could not open file %s ", filename);
exit(1);
}
while(fscanf(fp, "%d", &ID) == 1)
{
fscanf(fp, "%d %d", &g1, &g2);
index = find(ID, list, size);
if(index != -1)
{
list[index]->prj1Grade = g1;
list[index]->prj2Grade = g2;
}
}
fclose(fp);
}


void compute_final_course_grades(student **list, int size)
{
int i;
for(i = 0; i < size; i++)
{
list[i]->finalGrade = (list[i]->prj1Grade + list[i]->prj2Grade) / 2.0;
}
}


void output_final_course_grades(char *filename, student **list, int size)
{
FILE *fp = fopen(filename, "w");
int i;
if(fp == NULL)
{
printf("Error: Could not open file %s ", filename);
return;
}
fprintf(fp, "%d ", size);
for(i = 0; i < size; i++)
{
fprintf(fp, "%d %.2f ", list[i]->ID, list[i]->finalGrade);
}
fclose(fp);
}


void print_list(student **list, int size)
{

int i;

for(i = 0; i < size; i++)
{
printf("ID:%d, Name: %s %s, Project1 Grade: %d, Project2 Grade: %d, Final Grade: %.2f ",
list[i]->ID, list[i]->fname, list[i]->lname, list[i]->prj1Grade, list[i]->prj2Grade, list[i]->finalGrade);
}
}

void withdraw(int idNo, student **list, int *sizePtr)
{
int i;
int found = 0;
for(i = 0; i < *sizePtr; i++)
{
if(list[i]->ID == idNo)
{
found = 1;
break;
}
}
if(found)
{
free(list[i]);
for(i = i + 1; i < *sizePtr; i++)
list[i -1] = list[i];
(*sizePtr) = (*sizePtr) - 1;
printf("Student with Id : %d was withdrawn ", idNo);
}
else
printf("Student with Id : %d was not found ", idNo);
}

void destroy_list(student **list, int *sizePtr)
{
int i;
for( i = 0; i < *sizePtr; i++)
free(list[i]);
free(list);
*sizePtr = 0;
}

input file: names.txt

3
1200 Isaac Newton
4580 Alan Turing
9000 Elvis Presley

input file: grades.txt

4580 90 86
9000 80 76
1200 90 95

output file: final.txt

3
1200 92.50
4580 88.00
9000 78.00

sample run:

Enter the input filename containing names: names.txt
Enter the filename containing grades: grades.txt
Calculating final grades
Enter the output filename where to write final grades: final.txt
Displaying the student list
ID:1200, Name: Isaac Newton, Project1 Grade: 90, Project2 Grade: 95, Final Grade: 92.50
ID:4580, Name: Alan Turing, Project1 Grade: 90, Project2 Grade: 86, Final Grade: 88.00
ID:9000, Name: Elvis Presley, Project1 Grade: 80, Project2 Grade: 76, Final Grade: 78.00
Enter id of student to withdraw (enter -1 to exit): 1000
Student with Id : 1000 was not found
ID:1200, Name: Isaac Newton, Project1 Grade: 90, Project2 Grade: 95, Final Grade: 92.50
ID:4580, Name: Alan Turing, Project1 Grade: 90, Project2 Grade: 86, Final Grade: 88.00
ID:9000, Name: Elvis Presley, Project1 Grade: 80, Project2 Grade: 76, Final Grade: 78.00
Enter id of student to withdraw (enter -1 to exit): 4580
Student with Id : 4580 was withdrawn
ID:1200, Name: Isaac Newton, Project1 Grade: 90, Project2 Grade: 95, Final Grade: 92.50
ID:9000, Name: Elvis Presley, Project1 Grade: 80, Project2 Grade: 76, Final Grade: 78.00
Enter id of student to withdraw (enter -1 to exit): 1200
Student with Id : 1200 was withdrawn
ID:9000, Name: Elvis Presley, Project1 Grade: 80, Project2 Grade: 76, Final Grade: 78.00
Enter id of student to withdraw (enter -1 to exit): -1

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote