array

Pages: 12
Sorry, I mean code
Ahh fair enough.
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
28
29
30
31
			myfilein.clear(); // set state to good
			myfilein.seekg(0); // go to the beginning of the file

	while (myfilein >> i)

		{
			if (i > N)
				max = i;

		}

			myfilein.clear(); // set state to good
			myfilein.seekg(0); // go to the beginning of the file

	while (myfilein >> i)

		{
			if (i < N)
				min = i;
		}

	cout << endl << endl;
	cout << "The sum of the data is: " << sum << endl;
	cout << "The average value of the data is: " << mean << endl;
	cout << "The biggest number is: " << max << endl;
	cout << "The smallest number is: " << min << endl;

		myfilein.close();           // close file

  return 0;
}

Hope thats what you wanted to see.
You are still using 'N' here...
1
2
if (i > N) // check i > max instead
    max = i;

And also you should initialize 'max' with a really low value and 'min' with a really high value so that will work fine with any input from the file
eg: if you initialize mix with 0 ( as you are doing now ) and the minimum value in the file is 1, you will get 0 as minimum value even if it isn't present in the file. To find the maximum or minimum value for a numeric type, use numeric_limits: http://www.cplusplus.com/reference/std/limits/numeric_limits/
Thanks that worked a treat :)
My only problem is with the average. I need it to count the amount of numbers in the file so i can use that to divide the sum of the data.
You can do something like this:
1
2
3
4
5
6
7
8
9
10
11
int number_of_numbers = 0;
while (myfilein >> i)
{
     number_of_numbers++; // increase number_of_numbers by one
     sum += i;
}

if ( number_of_numbers ) // don't divide by zero
    average = sum / number_of_numbers; // sum and average should be doubles to get a good result
else
    // you didn't read any number 
Thanks for all your help :) It's working now. :)
Last edited on
Topic archived. No new replies allowed.
Pages: 12