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;
double aveH;
double aveS;
} 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 = s[i].homework1 + s[i].homework2 + s[i].homework3;
ave = (sum/100) * 100;
s[i].aveH = ave;
}
return ave;
}
double studentAverage(Student s[], int count)
{
double ave, sum = 0;
int i,j;
j=0;
for (i=0; i < count; i++)
{
sum = s[i].homework1 + s[i].homework2 + s[i].homework3 + s[i].exam + s[i].final;
ave = (sum/300) * 100;
s[i].aveS = ave;
}
return ave;
}
double classAverage(Student s[], int count)
{
double ave, sum = 0;
int i;
for (i= 0; i < count; i++)
{
sum = s[i].homework1 + s[i].homework2 + s[i].homework3 + s[i].exam + s[i].final;
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)
{
fprintf(outfile, "%d %-20s %6.1lf %9.1lf %9.1lf %9.1lf\n", s[i].id , s[i].name, s[i].aveH, s[i].exam ,
s[i].final, s[i].aveS);
i = i + 1;
}
}
Seems to be working now, i'll go back and verify the answers. thanks again for the help, and also since I use the struct to store the ave I dont need the functions to return anything right?