0

I'm working through some beginner exercises and have got to this question, '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've managed to output the most and least number of pancakes eaten, but I'm stuck on the last bit!

I can output the number of pancakes eaten in descending order i.e. 6,5,4,3,2 etc. but I'm struggling to work out how to assign each number of pancakes the user who ate them?

I have tried using the same technique that I used with the other two for loops, but I know it isn't correct, as this method doesn't assign the user to their number of pancakes eaten.

I feel like I'm missing something pretty simple here! Thanks for any help!

#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;
    int res = -1;

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

    int smallest = array[0];
    int res2 = 0;

    for (int i = 1; i < 5; i++)
    {
        if (array[i] < smallest)
        {
             smallest = array[i];
             res2 = i;
        }
    }
    cout << "The least pancakes eaten was " << smallest << " by Person " << (res2+1) << endl;

    int temp3 = 0;
    int res3 = 0;

    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 5; j++)
        {
            if (array[i] > array[j])
            {
                temp3 = array[i];
                array[i] = array[j];
                array[j] = temp3;
                res3 = i;
            }
        }
    }

    for (int i = 0; i < 5; i++)
    {
        cout << "Person " << res3 << " ate " << array[i] << " pancakes" << endl;
    }
}
soumya
  • 3,801
  • 9
  • 35
  • 69
calvin2011
  • 317
  • 3
  • 9

2 Answers2

2

Your problem is that you lose the relation between the index and the number. Seemingly, you need to work with a data structure that contains both the person's id and the number of pancakes he ate.

struct PancakeEater {
   int numberOfPancakes;
   int personId;
};

If I were you, I would abandon the raw arrays, and try to use an std::vector<PancakeEater> and use algorithms like std::sort. This is described in another question.

Community
  • 1
  • 1
xtofl
  • 40,723
  • 12
  • 105
  • 192
1

A common trick for this kind of problems is to sort indexes instead of sorting the actual values.

Here is the idea: make an array of indexes idx and populate it with numbers zero through N, where N in your case is five. Then do your sorting with two modifications:

  • Instead of comparing array[i] to array[j], compare array[idx[i]] to array[idx[j]], and
  • Instead of swapping array[i] and array[j], swap idx[i] and idx[j]

Once the sort is complete, array idx represents indexes of pancake eaters, sorted on the number of pancakes they have eaten. For example, if you entered

3 5 6 2 4

then your idx array would look like this after sorting:

2 1 4 0 3

You can print this array, along with the content of array[], to produce the desired output:

for (int i = 0 ; i != N ; i++) {
    cout << "Person " << (idx[i]+1) << " ate " << array[idx[i]] << " pancakes" << endl;
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523