minimum, average, and maximum of a file array

I've been given this task at University:
Write a program that reads a column of numbers from a file and then prints the
minimum, average, and maximum of those values. An example of the file contents is:
23.1
36.9
44.8
83.6
12.5
-1.3
109.4
-0.6
I have never been taught to programme and am teaching myself! Please help!!
I've written this so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  #include <stdio.h>
  #include <math.h>
int main() {

FILE *inf;
 float n, min=0, max=0, Average=0, sum=0, i;
 double number[8];
 /* open file for input */
 inf=fopen("Wk5doc2.txt","r");
 /* continue only if file opened successfully */
	if (inf != NULL) {
	/* keep reading to end of file */
		while (feof(inf)==0) {
		/* read data from file */
		fscanf(inf,"%f",&number[8]);
		/* print the data I just read */
		printf("Number is: %f\n", n);
		} 	
	/* close the input file */
	fclose(inf);	
	} 
	else {
		/* print error message */
		printf("Could not open input file.\n");
	}
 return(0);
}


Thank you
The following is accessing your array out of bounds.
fscanf(inf,"%f",&number[8]);
You probably want to use a "counter" that starts at zero and increases until you reach seven.
Thank you for replying but I don't understand what you've written! Can you write what you mean please?
Thanks again.
You can't use the number 8 inside the brackets, you need a variable, that starts as zero and stops when it reaches eight.

How do you write that? A variable in an array that changes?
Thanks
You don't need that array at all.

At start the count of values is 0 and (an integer). The sum is 0.0. The min is as large as possible. The max is as small as possible.

When you read in a new value, it will increase the sum and add one to the count.
If the value is larger than max, then max is updated.
If the value is smaller than min, then min is updated.

At the end you calculate the average and show the max and min.


PS. Is there a reason why you write C rather than C++?
Topic archived. No new replies allowed.