Unsure whether to use array or map?

Hey,

I'm trying to work through the beginner exercises from this site, and I've got to 'Pancake Glutton'.

"Requires:
variables, data types, and numerical operators
basic input/output
logic (if statements, switch statements)
loops (for, while, do-while)
arrays

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."

I'm unsure on how to get the program to call out the person which enters the most number of pancakes eaten? Surely this would need to be done with a key and value, but the requirements state 'arrays' but not 'maps'?

Below is the code I have come up with, but this only outputs the maximum number of pancakes eaten, so not really answering the question!

Thanks so much for any help!

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

int main()
{
    cout << "how many pancakes did you eat for breakfast?" << endl;

    int person1, person2, person3, person4, person5;
    cout << "Person 1: ";
    cin >> person1;

    cout << "Person 2: ";
    cin >> person2;

    cout << "Person 3: ";
    cin >> person3;

    cout << "Person 4: ";
    cin >> person4;

    cout << "Person 5: ";
    cin >> person5;

    int array[5] = {person1, person2, person3, person4, person5};
    int temp = 0;

    for (int i = 0; i<5; i++)
    {
        if (array[i] > temp)
        {
        temp = array[i];
        }
    }
    cout << "The most pancakes eaten was " << temp << "by " <<  endl;

}


*** I've only used 5 people here to quicken the process until I know how to do it properly***
Last edited on
Just assume that for array[i], the i specifies which person (person1 is array[0], etc).
I'm unsure on how to get the program to call out the person which enters the most number of pancakes eaten?

At line 31, you need to save both the number of pancakes and the person # (i).

Line 8: There is no need to have individual variables for each person. That's what your array is for.

An array is just fine for this problem. A map would be useful if you asked each person's name. You could then use the person's name as the key to the map.

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

int main()
{   int pancakes[5];  // Note that array is a template in the std:: namespace (C++11)
    int temp = 0, person = 0;
    cout << "how many pancakes did you eat for breakfast?" << endl;
    for (int i=0; i<5; i++)
    {   cout << "Person " << i << ": ";
        cin >> pancakes[i];
    }
    for (int i = 0; i<5; i++)
    {   if (pancakes[i] > temp)
        {   temp = pancakes[i];
            person = i;
        }
    }
    cout << "The most pancakes eaten was " << temp << "by " <<  person << endl;
}

Topic archived. No new replies allowed.