I'm doing a grading report and I have trouble with my average function. This is what I have so far.
At the moment it prints the first record of the file correctly if i'm not wrong, then it prints that same record repeatably for the remaining records. I was looking at my averageHomework function, the others have the same problem. And i'm not sure if i'm in the right path but i was thinking i need an array there, but then i have to return it and i'm not sure how to do that. So hopefully someone can give me some guidance on what approach I can use. Sorry for the messy code, I was trying different things so you might notice unused variables.Code:# include <stdio.h>
# include <stdlib.h>
typedef struct GRADEREPORT
{
int id;
char name[30];
double homework1;
double homework2;
double homework3;
double exam;
double final;
} Student;
int data(Student s[]);
double studentAverage(Student s[], int count);
double classAverage(Student s[], int count);
double homeworkAverage(Student s[], int count);
void output_Data(Student s[], int count,double aveHomework, double aveStu, double aveClass);
int main()
{
int count;
double aveHomework, aveStudent, aveClass;
Student nam[30];
count = data(nam);
aveStudent = studentAverage(nam, count);
aveClass = classAverage(nam, count);
aveHomework = homeworkAverage(nam,count);
output_Data(nam, count,aveHomework, aveStudent, aveClass);
return 0;
}
int data(Student s[])
{
int c, i = 0;
FILE *infile;
infile = fopen("p4.dat", "r");
if (infile == NULL)
{
printf("Problem opening file \"p4.dat\" \n");
exit(1);
}
c = fscanf(infile, "%d %s %lf %lf %lf %lf %lf", &s[i].id, &s[i].name, &s[i].homework1,
&s[i].homework2,&s[i].homework3, &s[i].exam, &s[i].final);
while (c != EOF)
{
i++;
c = fscanf(infile, "%d %s %lf %lf %lf %lf %lf", &s[i].id, &s[i].name, &s[i].homework1,
&s[i].homework2,&s[i].homework3, &s[i].exam, &s[i].final);
}
fclose(infile);
return i;
}
double homeworkAverage(Student s[], int count)
{
double ave, sum = 0;
int i,j;
j= 0;
for(i=0; i < count; i++)
{
sum = sum + s[i].homework1 + s[i].homework2 + s[i].homework3;
count = i+ 1;
ave = (sum/100) * 100;
}
return ave;
}
double studentAverage(Student s[], int count)
{
double ave, sum = 0;
int i;
for (i=0; i < count; i++)
{
sum = sum + s[i].homework1 + s[i].homework2 + s[i].homework3 + s[i].exam + s[i].final;
count = i+ 1;
ave = (sum/300) * 100;
}
return ave;
}
double classAverage(Student s[], int count)
{
double ave, sum = 0;
int i;
for (i= 0; i < count; i++)
{
sum = sum + s[i].homework1 + s[i].homework2 + s[i].homework3 + s[i].exam + s[i].final;
count = i+ 1;
ave = sum/count;
}
return ave;
}
void output_Data(Student s[], int count, double aveHomework, double aveStu, double aveClass)
{
int i = 0;
double average,homeworkAverage;
FILE *outfile;
outfile = fopen("StudentReport.txt", "w");
if (outfile == NULL)
{
printf("Problem opening file \"StudentReport.txt\" \n");
exit(1);
}
fprintf(outfile," CSCI 110 Grade Report\n\n");
fprintf(outfile, "ID: Name Homework Exam Final Average Grade\n");
while(i < count)
{
average = aveStu;
homeworkAverage = aveHomework;
fprintf(outfile, "%d %-20s %6.1lf %9.1lf %9.1lf %9.1lf\n", s[i].id , s[i].name, homeworkAverage, s[i].exam ,
s[i].final, average);
i = i + 1;
}
}