### Output Problem

My program is supposed to read from an input file and output the minimum value, maximum value, the mean (sum of the values divided by the number of values) and the range (max - min +1) of every line. If the value of the first number in every line is less than or equal to zero, the program should terminate.

I have an input file:
 ``12345`` ``````5 2 3 0 2 5 4 4 1 1 -1 5 3 4 -4 5 3 6 6 6 6 6 6 6 -9``````

and I'm supposed to get this output:
0 5 2.4 6
-1 4 1.25 6
-4 5 2.2 10
6 6 6 1

what I'm getting though is this:
0 5 2.4 6
-1 4 1.25 6
-4 5 2.2 10
0 6 6 7 (trouble is in here)

I don't know where I'm wrong, everything looks fine until it reaches the last line of input where I get a 0 and 6 instead of 6 and 6.

 ``1234567891011121314151617181920212223242526272829303132333435`` ``````#include #include using namespace std; int main(){ ifstream file; file.open("test.in"); int count; while (file >> count){ if (count < 0) break; else{ int number[count], range = 0; double sum = 0; int min = 0, max = 0; for (int i=count;i>0;i--){ file >> number[i]; if(number[i] > max) max = number[i]; if(number[i] < min) min = number[i]; sum = sum + number[i]; range = (max - min) + 1; } cout << min << " " << max << " " << (sum/count) << " " << range; cout << endl; } } file.close(); return 0;} ``````
`int min = 0, max = 0;` try setting min to a high value. `min = 5000;`
Last edited on
As minimum starts at 0, the lines only contains 6, so it never triggers another value being lower than that. It works with every other line because the minimum is either 0 or less than 0.

in the for loop, after the file >> number[i] add a quick if
 ``1234`` ``````if (i == count) { int min = number[i], max = number[i]; }``````
Last edited on
I tried setting the min to the max value and now it works. Thanks for the help guys!

Edit: Uh nvm, I thought it did. Will try to fix it somehow, thanks still though!
Last edited on
Topic archived. No new replies allowed.