Finding sum, min, max values.

What am I doing wrong?

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>
using namespace std;

int main()
{
    int n, value, sum=0;
    cout << "Please enter positive number greater than 0, less than or equal to 100." << endl;
    cin >> n;
    int max = 0;
    int min = 1;
    while(n<0 || n>100)
    {
        cout << "Incorrect" << endl;
        cout << "Please try again." << endl;
        cin >> n;
    }
    for (int i=0; i<n; i++)
    {
        cout << "Enter value number " << i+1 << endl;
        cin >> value;
        if(max<value)
        {
            max = value;
        }
        else if (min<value)
        {
            min = value;
        }
        sum += value;
    }
    cout << "The total sum is: " << sum << endl;
    cout << "The max is: " << max << endl;
    cout << "The min is: " << min << endl;
    return 0;
}
closed account (SECMoG1T)
1
2
 if(max<value);
 else if (min<value)//compare this 
Line 10: initialize min to 100.
I initialized min to 100 and it just printed out the min as 100....
closed account (SECMoG1T)
If you did intialize it to 100, you should also change this condition
else if (min<value)// to use > operator
and also change
1
2
if (max > value)
max = value;
I did this but when I put for example 3, 1, 2, 3...

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>
using namespace std;

int main()
{
    int n, value, sum=0;
    cout << "Please enter positive number greater than 0, less than or equal to 100." << endl;
    cin >> n;
    int max = 0;
    int min = 100;
    while(n<0 || n>100)
    {
        cout << "Incorrect" << endl;
        cout << "Please try again." << endl;
        cin >> n;
    }
    for (int i=0; i<n; i++)
    {
        cout << "Enter value number " << i+1 << endl;
        cin >> value;
        if(max<value)
        {
            max = value;
        }
        else if (min>value)
        {
            min = value;
        }
        sum += value;
    }
    cout << "The total sum is: " << sum << endl;
    cout << "The max is: " << max << endl;
    cout << "The min is: " << min << endl;
    return 0;
}


It outputs this:

Please enter positive number greater than 0, less than or equal to 100.
3
Enter value number 1
1
Enter value number 2
2
Enter value number 3
3
The total sum is: 6
The max is: 3
The min is: 100
The max and min are independent properties.

In your code they have a dependency; the min can change only if value<=max. Your monotonously ascending test input never has such case.

1
2
3
4
5
if ( max < value ) {
}

if ( value < min ) {
}
Topic archived. No new replies allowed.