Hello,

I m doing an exercise i found in internet. The exercise is the following:

Pancake Glutton

Write a program that asks the user to enter the number of pancakes eaten for breakfast by 10 different people (Person 1, Person 2, ..., Person 10)
Once the data has been entered the program must analyze the data and output which person ate the most pancakes for breakfast.

★ Modify the program so that it also outputs which person ate the least number of pancakes for breakfast.

★★★★ Modify the program so that it outputs a list in order of number of pancakes eaten of all 10 people.
i.e.
Person 4: ate 10 pancakes
Person 3: ate 7 pancakes
Person 8: ate 4 pancakes
...
Person 5: ate 0 pancakes

I m doing the first part and am trying to output the biggest value entered in the program. Please dont do the exercise for me i just need some guidance. My program is outputing the last value enetered in the program not the biggest. What is the problem with that loop ?

This is my code:

#include <iostream>

using namespace std;

int main()
{
    int person[10];
    int tmp;

    for (int n = 0; n < 10; n++)    //Loop for user input
        {
            cout<<"How many pancakes did you eat Person"<<(n+1)<<endl;
            cin>>person[n];
        }

    for (int i = 0; i < 10; i++)    //Loop for comparing values and determinating the biggest value
        {
            for (int t = 0; t < 10; t++)
                {
                    if (person[i] > person[t])
                        {
                            tmp = person[i];    //Should save the biggest value in tmp (But it doesnt)
                        }
                }
        }
    cout<<"Biggest amount of pancakes eaten: "<<cout<<tmp<<endl; //outputs the biggest value
    
    return 0;
}

Thanks in advance.

you are replacing tmp every time you compare 2 values.

ie on the very last check if person[9]>person[8](or nine in this case, checking against itself) you replace the value. this doesn't mean it was the biggest of all. one loop should be sufficient for this task.

Edited 5 Years Ago by frogboy77: n/a

you need to loop through the array and find the largest number and also store which person this is attributed to. So have a max variable and a whichperson variable and update them when a larger value is found. set max to -1 to begin with.

Edited 5 Years Ago by frogboy77: n/a

This article has been dead for over six months. Start a new discussion instead.