C PROGROMMING NEED HELP!!! - TechRepublic
Question
January 4, 2009 at 11:09 AM
billwhite16

C PROGROMMING NEED HELP!!!

by billwhite16 . Updated 17 years, 5 months ago

Hi, i need to write a c program, that reads a list of integers from a text file, and then prints these list of integers. then prints the average of the integers, the minimum value, and the maximum value! i dont know how to do this, as when you read from a file, you only have one variable, so how can they be added together, do i need a while or a for loop!
this is what i have so far, and all it does is read the list of integers, and prints them onto the screen! the program must also adapt so if i change the number of integers, then the average will change, and if i change the lowest or highest integer, then the min and max output will change. i think i need an if statement that reads the current value to check if it is lower than the previous value…ahhh!! please help!!

#include

int main(){

//input from a file//
FILE *inf;

//declare variables//
float data,sum,min,max;
int ii;

//read from that file//
inf=fopen(“textfile.txt”,”r”);

//exercise safe file opening//
if (inf!=NULL){

//loop till end of file//
while (feof(inf)==0){

//read the data from the file//
fscanf(inf,”%f”,&data);

//print the data//
printf(“%.2f\n”,data);

}

fclose(inf);

}

//print error message if file does not exist//
else {
printf(“file does not exist\n”);
}

return(0);

}

This discussion is locked

All Comments