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

Within this C program change the input to a file instead of individual input fro

ID: 3665933 • Letter: W

Question

Within this C program change the input to a file instead of individual input from the user.

You must take the input file name on the command line.
Be sure to have simple error checking to be sure the file exists and was successfully opened.
The input file will have the following format:

First line: Number of students
Second Line: Number of grades
Third Line: Student Names, space delimited
Fourth+ Line(s): Grades for all students for one assignment, space delimited.

Example:

2
3
Cody Jenny

100 100

90 80
70 60

Your program should be able to handle a file of any reasonable size. (I will not use excessively long student names, but there may be MANY students and grades.)

Once the file is inputed and read, run it through the program and produce an output that looks like this

Cody Jenny

100 100

90 80

70 60

B B

Here is the program that needs to be reformatted:

#include <stdio.h>

#include <string.h>

int NUMSTUS;

int NUMGRADES;

void get_students(char stuNames[NUMSTUS][10]);

void get_grades(int grades[NUMGRADES][NUMSTUS], char stuNames[NUMSTUS][10]);

void calc_grades(int grades[NUMGRADES][NUMSTUS], char finalGrades[NUMSTUS]);

void print_report(char stuNames[NUMSTUS][10], int grades[NUMGRADES][NUMSTUS], char finalGrades[NUMSTUS]);

int main()

{

char junk;

// Ask the user how many students, how many grades

printf("How many students? ");

scanf("%d", &NUMSTUS);

printf("How many assignments? ");

scanf("%d", &NUMGRADES);

scanf("%c", &junk);

char stuNames[NUMSTUS][10];

int grades[NUMGRADES][NUMSTUS];

char finalGrades[NUMSTUS];

  

get_students(stuNames);

  

get_grades(grades, stuNames);

  

calc_grades(grades, finalGrades);

  

print_report(stuNames, grades, finalGrades);

  

return 0;

  

}

void get_students(char stuNames[NUMSTUS][10])

{

int i;

int len;

for(i=0; i < NUMSTUS; i++)

{

printf("Enter name for Student %d: ", i);

fgets(stuNames[i], 10, stdin);

len = strlen(stuNames[i]);

stuNames[i][len-1] = '';

}

return;

}

void calc_grades(int grades[NUMGRADES][NUMSTUS], char finalGrades[NUMSTUS])

{

int i, j;

int sum, avg;

  

  

for(i=0; i < NUMSTUS; i++)

{

sum=0;

for(j = 0; j < NUMGRADES; j++)

{

sum += grades[j][i];

}

avg = sum / NUMGRADES;

  

//Convert from numberical average to letter grade

if(avg >= 90)

finalGrades[i] = 'A';

else if(avg < 90 && avg >= 80)

finalGrades[i] = 'B';

else if(avg < 80 && avg >= 70)

finalGrades[i] = 'C';

else if(avg < 70 && avg >= 60)

finalGrades[i] = 'D';

else

finalGrades[i] = 'F';

}

return;

}

void get_grades(int grades[NUMGRADES][NUMSTUS], char stuNames[NUMSTUS][10])

{

int i, j;

  

for(i = 0; i < NUMGRADES; i++)

{

for(j = 0; j < NUMSTUS; j++)

{

printf("Enter grade for Assignment %d for %s: ", i, stuNames[j]);

scanf("%d", &grades[i][j]);

}

}

return;

}

void print_report(char stuNames[NUMSTUS][10], int grades[NUMGRADES][NUMSTUS], char finalGrades[NUMSTUS])

{

int i, j;

//Print students names

for(i = 0; i < NUMSTUS; i++)

{

printf("%10s ", stuNames[i]);

}

printf(" ");

  

//Print grades

for(i = 0; i < NUMGRADES; i++)

{

for(j = 0; j < NUMSTUS; j++)

{

printf("%10d", grades[i][j]);

}

printf(" ");

}

  

//Print final grade

for(i = 0; i < NUMSTUS; i++)

{

printf("%10c ", finalGrades[i]);

}

printf(" ");

  

return;

}

Explanation / Answer

Have made necessary changes and the same has been highlighted with bold characters.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int NUMSTUS;
int NUMGRADES;

FILE * get_students(char stuNames[NUMSTUS][10], FILE *);
void get_grades(int grades[NUMGRADES][NUMSTUS], FILE *);

void calc_grades(int grades[NUMGRADES][NUMSTUS], char finalGrades[NUMSTUS]);
void print_report(char stuNames[NUMSTUS][10], int grades[NUMGRADES][NUMSTUS], char finalGrades[NUMSTUS]);

int main(int argc, char *argv[])
{
FILE *file = fopen( argv[1], "r" );
/* fopen returns 0, the NULL pointer, on failure */
if ( file == 0 ){
printf( "Could not open file " );
exit(0);
}

fscanf(file,"%d %d",&NUMSTUS,&NUMGRADES);
char junk;
char stuNames[NUMSTUS][10];
int grades[NUMGRADES][NUMSTUS];
char finalGrades[NUMSTUS];

file = get_students(stuNames,file);

get_grades(grades, file);

calc_grades(grades, finalGrades);

print_report(stuNames, grades, finalGrades);

return 0;

}

FILE * get_students(char stuNames[NUMSTUS][10],FILE * file)
{
int i;
for(i=0; i < NUMSTUS; i++)
fscanf(file,"%s",stuNames[i]);
return file;
}

void calc_grades(int grades[NUMGRADES][NUMSTUS], char finalGrades[NUMSTUS])
{
int i, j;
int sum, avg;


for(i=0; i < NUMSTUS; i++)
{
sum=0;
for(j = 0; j < NUMGRADES; j++)
{
sum += grades[j][i];
}
avg = sum / NUMGRADES;

/*Convert from numberical average to letter grade*/
if(avg >= 90)
finalGrades[i] = 'A';
else if(avg < 90 && avg >= 80)
finalGrades[i] = 'B';
else if(avg < 80 && avg >= 70)
finalGrades[i] = 'C';
else if(avg < 70 && avg >= 60)
finalGrades[i] = 'D';
else
finalGrades[i] = 'F';
}
return;
}

void get_grades(int grades[NUMGRADES][NUMSTUS], FILE * file)
{
int i, j;

for(i = 0; i < NUMGRADES; i++)
for(j = 0; j < NUMSTUS; j++)
fscanf(file,"%d", &grades[i][j]);

fclose(file);
return;
}

void print_report(char stuNames[NUMSTUS][10], int grades[NUMGRADES][NUMSTUS], char finalGrades[NUMSTUS])
{
int i, j;
/*Print students names*/
for(i = 0; i < NUMSTUS; i++)
{
printf("%10s ", stuNames[i]);
}
printf(" ");

/*Print grades*/
for(i = 0; i < NUMGRADES; i++)
{
for(j = 0; j < NUMSTUS; j++)
{
printf("%10d", grades[i][j]);
}
printf(" ");
}

/*Print final grade*/
for(i = 0; i < NUMSTUS; i++)
{
printf("%10c ", finalGrades[i]);
}
printf(" ");

return;
}

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