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:
1
2
3
4
5
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.

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
32
33
34
35
#include <iostream>
#include <fstream>
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
1
2
3
4
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.