Create a C program that will read a file with data similar to the one below: Jac
ID: 3631595 • Letter: C
Question
Create a C program that will read a file with data similar to the one below:Jack 10 8 9 5 6 4 6 9 1 3 4 6 4 6 5 6 7 7 5 6 7 8 10 1 6 6 7 8 9 1
Tim 8 7 6 6 10 7 10 6 7 9 10 1
Andrew 9 2 5 6 3 8 9 4 5 6 3 4 7 4 5 3 4 9
the output of the program should list the name of each line, the average (as an integer) of each line, and the number of sets. Meanwhile it should average every 3rd number of each line. at the end, it should print out the person with the highest third number average. sample output:
Jack , 5 sets, average of 6
tim ,2 sets, average 7
andrew ,3 sets, average 5
tim has average of 6.5 for third score.
Explanation / Answer
#include<stdio.h>
void main()
{ //Start main function
int count,num,sum,average; //Variable declaration
char name[20];
FILE *infile;
infile =fopen("input.txt","r");
fscanf(infile,"%s", name);
//Logic
while(infile)
{
sum=0;
count=0;
int thrid=0,sumth=0;
fscanf(infile,"%d", &num);
while(num!=-999) //Loop to sum numbers in aline
{
sum=sum+num;
count++;
fscanf(infile,"%d", &num);
}
if(count!=0)
average=sum/count;
else
average=0;
printf("%s,%d sets, average of %f",name,count/6,average);
fscanf(infile,"%s", name);
}
fclose(infile);//closing file
getch();
}//End of main
Hope this will help you