Problem with ranking

Hello,

I am doing these beginner exercises: http://www.cplusplus.com/forum/articles/12974/

The exercise I am doing now is the '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

___________
First of all, I changed the amount of persons to 5.

It all worked fine untill I reached the last part where I had to do the ranking.
Since this was kind of my first exercise with arrays I had to search for help.
The problem is that I only find ways to sort the valuables stored in the arrays.

So I can display the amount of pancakes in the wright order, but not the persons.
Could you guys help me with this problem?


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
45
    for (int j = 0; j < 5; j++) // In this function, I am looking for whom ate the most
    {
        if(person[j] > most)
        {
            most = person[j];
            v = j + 1;
        }
    }

    least = most;

    for (int k = 0; k < 5; k++) // In this function, I am looking for whom ate the least
    {
        if(person[k] < least)
        {
            least = person[k];
            z = k + 1;
        }
    }

    for(length - 1; length > 0; length--) // This is the extra part, where I am stuck
    {
        for(int index = 0; index < end; index++)
        {
            if(person[index] > person[index + 1])
            {
                opslag = person[index + 1];
                person[index + 1] = person[index];
                person[index] = opslag;
            }
        }
        end--;
    }

    cout << "Person " << v << " ate the most pancakes." << endl;
    cout << "Person " << z << " ate the least pancakes." << endl; // end of first part
    cin.get();
    cout << "Here is the ranking:" << endl;
    cout << "1) Person " << v << " with " << most << " pancakes" << endl;       
    cout << "2) Person " << w << " with " << person[3] << " pancakes" << endl;
    cout << "3) Person " << x << " with " << person[2] << " pancakes" << endl;
    cout << "4) Person " << y << " with " << person[1] << " pancakes" << endl;
    cout << "5) Person " << z << " with " << least << " pancakes" << endl;
    cin.get();
}
Last edited on
closed account (48T7M4Gy)
which person ate the most pancakes for breakfast


Not quite what you asked for but you are not quite right with the first two parts.

You seem to have correctly worked out the most ( and least ) pancakes eaten but not who the person responsible for that feat was in each case. Here, what u need to do is record the most and least at each stage as well as the value of j or k respectively. You could also just record jMax and kLeast.
I worked out who the person was with v (j + 1) and z (k + 1), and when I test it, that works.
Though in the way I did it, I'm not able to find the other persons (w, x, and y)
closed account (48T7M4Gy)
Well done.

For the next step you might like to look up the term 'bubblesort' which extends what you have done so far. Just remember that the least->greatest is just greatest->least in reverse order so you really only have to sort once.
Topic archived. No new replies allowed.