Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(LINUX) Assume the following text file foo.txt in your computer. The first line

ID: 3725688 • Letter: #

Question

 (LINUX) Assume the following text file foo.txt in your computer. The first line of the text file includes the field names of the following records.  # name:id:weight:height:gender Alice A:123:1.506E+2:5.00:F Bob B:456:1.807E+2:5.50:M Charlie C:789:0.892E+2:5.34:M  Make a C program to read data from the text file and then output the average weight and the average height to screen. Copy and paste your code in your report. The output data should be formated as below. You should NOT use spaces to align the data in output. Spaces in output are only for separating words. The average weight :    130.52 lb The average height :      5.35 ft a) You should NOT assume you know how many records in the text file. b) You do NOT need to worry about if a field is mis-formated in the text file. c) Use ONLY fscanf and fprintf functions to read and output data. d) Do not use "cout<<...<<endl;" or any C++ functions. 

Explanation / Answer

PROGRAM TO BE EXECUTED:

#include<stdlib.h>

#include<stdio.h>

#include<string.h>

int main()

{

char line [1000] ;

FILE *file;

float tweight=0, thieght = 0;

int cnt=0;

file =fopen("filename.txt","r");

if (!file)

return 1;

while (fgets(line,1000, file)!=NULL){

cnt++;

char *data;

int i=0;

data=strtok(line,":");

while(data!=NULL){

if(i==2){

tweight = tweight+atof(data);

}

if(i==3){

thieght=thieght+atof(data);

}

data=strtok(NULL,":");

i++;

}

}

printf(" Average weight : %.2f lb", (tweight/cnt));

printf(" Average height : %.2f ft", (thieght/cnt));  

return 0;

}