Write a program in C which will open a text file (Scorcs.dat) containing section
ID: 3571910 • Letter: W
Question
Write a program in C which will open a text file (Scorcs.dat) containing section number and total scores of students of CSCI1320. You need to create the file Scorcs.dat using any text editor like Notepad etc and make sure the file is in the same directory of your c program. Your program needs to open the fdc and based on section number, compute the average score for each section. Finally, you need to print section number along with average score and find the section with highest average score. Consider there are only 3 sections for the course CSCI1320.Explanation / Answer
#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]);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.