By billwhite16 ·
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 <stdio>

int main(){

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

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

//read from that file//

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

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

//read the data from the file//

//print the data//




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



This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Answers

Collapse -

Two options

by Tony Hopkinson In reply to C PROGROMMING NEED HELP!! ...

One you can read it in to a list and then process that, or uick nasty and probably waht you are after.

Four varaiables to hold min,max, sum and count.
Intiialise them, biggest, smallest, 0 and 0.

Inside the the loop test the current value against min and max, add it to sum, and increment count
After the loop print out min and max and work out the average print that.

You might want to think about the impact of negatives, and for Cthulu's sake test for count = 0 before you divide by it.


Related Discussions

Related Forums