/* Reads the grades of 10 students in 3 exams form an input file and prints the grades and average in a tabular form in another file also computes the overall average. Author: Bashir Ghandi Date: 18-09-1999 */ #include main() { long int id; int i; float ex1, ex2, ex3, avg, sum1=0,sum2=0,sum3=0; FILE *inp, *outp; char in_file[25]; do { printf ("Enter the name of input file >") ; scanf("%s", in_file); inp=fopen(in_file, "r"); if (inp == NULL) printf ("Cannot open %s for input\n", in_file) ; } while (inp==NULL); outp=fopen("results.dat","w"); fprintf(outp,"\nID\tEXAM1\tEXAM2\tEXAM3\tAVERAGE\n"); for (i=1; i<=10; i++) { fscanf(inp, "%ld%f%f%f",&id, &ex1, &ex2, &ex3); avg=(ex1+ex2+ex3)/3; fprintf(outp,"%ld\t%.2f\t%.2f\t%.2f\t%.2f\n", id,ex1,ex2,ex2,avg); sum1+=ex1; sum2+=ex2; sum3+=ex3; } fprintf(outp,"AVERAGE\t %.2f\t%.2f\t%.2f\n",sum1/10, sum2/10, sum3/10); fprintf(outp, "OVERALL AVERAGE: %.2f\n",(sum1+sum2+sum3)/30); fclose(inp); fclose(outp); return 0; }