23.5k views
3 votes
Write a program in C which will open a text file (Scores.txt) containing section number and total scores of students of CSCI1320. You need to create the file Scores.txt 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 file 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 the highest average score. Consider there are only 3 sections for the course CSCI1320. [Note: use have to use pointers and user-defined functions wherever necessary. You need to demonstrate both pass-by value and pass-by-reference in this program]

User Argent
by
3.5k points

1 Answer

2 votes

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]);

}

User Burhan Uddin
by
3.6k points