Answer:
See explaination
Step-by-step explanation:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
FILE *fp;
char section[10][2], score[10][10],buf[10][4], buf1[4];
int i=0, j = 0, k = 0, l = 0, c1 = 0, c2 = 0, c3 = 0, sec;
float avg1 = 0 ,avg2 = 0, avg3 = 0 ,avg[3] ,max;
fp = fopen("scores.dat","r");
/* read data from scores.dat file and store into buf */
while(fscanf(fp, "%s", buf[i++])!=EOF);
/* separate scores and sections */
for(j=1; j<i; j++){
if(j%2!=0){
strcpy(section[k++], buf[j-1]);
}
else{
strcpy(score[l++], buf[j-1]);
}
}
/* calculate avgerage */
for(i=0;i<7;i++){
if(strcmp(section[i], "1")){
avg1 = avg1 + atof(score[i]);
c1++;
}
if(strcmp(section[i], "2")){
avg2 = avg2 + atof(score[i]);
c2++;
}
if(strcmp(section[i], "1")){
avg3 = avg3 + atof(score[i]);
c3++;
}
}
avg[0] = avg1/c1;
avg[1] = avg2/c2;
avg[2] = avg3/c3;
max = avg[0];
for(i=0;i<3; i++)
if(avg[i] > max){
max = avg[i];
sec = i;
}
printf("Sectrion 1 Avg: %f\\Section 2 Avg: %f\\Section 3 Avg: %f\\Highest Avg by:Section %s\\",avg[0],avg[1],avg[2],section[sec]);
}