Pancake Glutton exercise

Im trying to do this exercise:

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.

This is what i got so far

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main()
{
    int person[10];         //stores how many pancakes eaten
    int numberOfPeople = 0; //used to count from person 1-10 when inputting

    cout << "Enter the number of pancakes eaten by the 10 people\n" << endl;
    for(int i = 0; i < 10; i++)
    {
        numberOfPeople++;
        cout << "Person " << numberOfPeople << ": ";
        cin >> person[i];
    }

    return 0;
}



I don't know how i find the biggest number in the array.
Would really appreciate some help.
closed account (zb0S216C)
This piece of code will help you (I'll explain it):

1
2
3
4
int Largest( 0 );
for( short i( 0 ); i < 10; i++ )
    if( Person[i] > Largest )
        Largest = Person[i];

Here, a for loop is created that is set to loop 10 times. With each pass of the loop, the ith element of Person is compared to the current value stored in Largest. If the value within Largest is less than the value within the ith element of Person, Largest is assigned the value of the ith element of Person. At the end of the loop, Largest will contain the largest value within the Person array.

@Vlad: :-)P

Wazzak
Last edited on
do a for loop after that that checks person[i] against person[i+1]

so like your array is: [5, 9, 2, 3, 8, 10, 4, 1, 7, 6] {This is person 0- 9 and how many pancakes they ate... what the user would have inputted randomly or something}

you say something like int biggest; if person[0 or i] > p[i+1] then person 0 = biggest. and check it against all of them and you should have the largest number.


Oh and i highly recommend the next star as well - the one to sort them all from least to greatest or vice. It might be tough but well worth it. :)

EDIT: Damn you wazzaak!
Last edited on
Thank you very much both of you :).

So this is what i have now:
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
#include <iostream>
using namespace std;

int main()
{
    int person[10];         //stores how many pancakes eaten
    int numberOfPeople = 0; //used to count from person 1-10 when inputting

    cout << "Enter the number of pancakes eaten by the 10 people\n" << endl;
    for(int i = 0; i < 10; i++)
    {
        numberOfPeople++;
        cout << "Person " << numberOfPeople << ": ";
        cin >> person[i];
    }

    int largest = 0;

    for(int i = 0; i < 10; i++)
    if(person[i] > largest)
        largest = person[i];

    cout << largest << endl;

    return 0;
}


I now have the largest number. But i also have to figure which element(person) in the array that has that number.
Is there any way to do this?
When you find the largest number, just put whatever index that person was in a separate variable.
closed account (zb0S216C)
I'm not quite sure what you mean. Do you mean this: Once the largest number is found, print the ith index to the screen? If so, then you may want to consider this:

1
2
3
4
5
6
7
8
9
10
11
12
int largest = 0;
int LargestIndex( 0 );

for(int i = 0; i < 10; i++)
if(person[i] > largest)
{
    largest = person[i];
    LargestIndex = i;
}

std::cout << "The largest value entered was " << Largest << "." << std::endl;
std::cout << "The largest number is at index: " << LargestIndex << "." << std::endl;

Here, LargestIndex store the index of the element which contains the largest value.

Wazzak
Well jeez framework, how is he going to learn stuff if you just feed him copypasta =P
Thank you :).

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
36
37
38
39
40
41
42
43
44
#include <iostream>
using namespace std;

int main()
{
    int person[10];         //stores how many pancakes eaten
    int numberOfPeople = 0; //used to count from person 1-10 when inputting

    cout << "Enter the number of pancakes eaten by the 10 people\n" << endl;
    for(int i = 0; i < 10; i++)
    {
        numberOfPeople++;
        cout << "Person " << numberOfPeople << ": ";
        cin >> person[i];
    }

    int largest = 0;
    int largestPerson = 0;

    for(int i = 0; i < 10; i++)
    if(person[i] > largest)
    {
        largest = person[i];
        largestPerson = i;
    }

    int smallest = 100000;
    int smallestPerson = 0;

    for(int i = 0; i < 10; i++)
    if(person[i] < smallest)
    {
        smallest = person[i];
        smallestPerson = i;
    }

    largestPerson++; //to get index 0 = person 1
    smallestPerson++;

    cout << "\nPerson " << largestPerson << " ate the most pancakes. Which was: " << largest << endl;
    cout << "\nPerson " << smallestPerson << " ate the least pancakes. Which was: " << smallest << endl;

    return 0;
}


Don't know if it is the best way to find the smallest number, but couldn't think of any other way.
http://www.cplusplus.com/reference/algorithm/max_element/

Use the resources in this website rather than posting ....
Pretty bad advice IMO Rambo... I think Writing his own code and struggling will surely give him experience and confidence.
Topic archived. No new replies allowed.