displaying the largest value

How do you display the largest value? I think I either need to use an array or have more variables but I'm at a loss.

Thanks.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace std;

int main()
{
    int pan;
    int a = 1;


    cout << "How many pancakes did they eat?" << endl << endl;
    for (int i = 0; i < 10; i++)
    {

        cout << "How many pancakes did person [" << a << "] eat? ";
        cin >> pan;
        a++;
    }
    cout << "The most anyone ate is: " << pan << endl;
    return 0;
}
four million, six hundred and eighty-seven thousand, forty-three.

In all seriousness, you need another pancake variable to measure against the last one:

1
2
3
4
5
6
7
8
9
10
11
12
13
int pan = 0;
int highcakeamount = 0;
for (int i = 0; i < 10; i++)
    {

        cout << "How many pancakes did person [" << a << "] eat? ";
        cin >> pan;
        if(pan > highcakeamount)
           highcakeamount = pan;
        a++;
    }

   cout<< "The most anyone ate WAS: " << highcakeamount << endl;

You only need one more variable which will hold the largest value found so far. Each time a new value is input, compare it with the stored value, if the new value is bigger, then update the stored value with the new biggest number.
Thanks, much simpler than I thought.
Topic archived. No new replies allowed.