Pancake Glutton almost finished.

Hello, I have almost completed the excercise Pancake Glutton, but i can't manage to figure out how to make the program output which person was the one who ate more pancakes. It only shows the highest amount of pancakes eaten.

I also created another array with values from 1-10 and treated it the same way as the pancake array in the sorting part so it would output the number of the person and the amount of pancakes eaten simultaneously but I'm sure this is not the correct way to do it.

If you find any other improvement that i could make to the code please tell me. Thank you guys very much.

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>

using namespace std;


/*
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.

 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
*/

int main ()
{

    int const length = 10;
    int numbPerson = length;
    int person[length]= {1,2,3,4,5,6,7,8,9,10};
    int numberOfPancakes[length];
    int personHolder = -1;
    int pancakeHolder = -1;


//get number of pancakes eaten by 10 different people.
    for(int i=0;i<numbPerson;i++)
    {
        cout << "Enter the amount of pancakes person "<<person[i]<<" ate."<<endl;
        cin >> numberOfPancakes[i];
    }

//find min and max
    int min = numberOfPancakes[0];
    int max = numberOfPancakes[0];

    for (int i=1; i<length; i++)
    {
        if (numberOfPancakes[i] < min)
            min = numberOfPancakes[i];

        if (numberOfPancakes[i] > max)
            max = numberOfPancakes[i];

    }
    cout << "Min: " << min << endl;
    cout << "Max: " << max << endl;

//Sort using Bubble sort.
    for (int counter = length-1 ; counter > 0 ; counter--)
    {

        for  (int i = 0 ; i < numbPerson ; i++)
        {
            if (numberOfPancakes[i] > numberOfPancakes[i+1])
            {
                pancakeHolder = numberOfPancakes[i];
                numberOfPancakes[i] = numberOfPancakes[i+1];
                numberOfPancakes[i+1] = pancakeHolder;


                personHolder = person[i];
                person[i] = person[i+1];
                person [i+1] = personHolder;
            }
        }
        numbPerson--;
    }

    for  (int i=0;i<length; i++)
    {
        cout<< person[i] << ") " << numberOfPancakes[i]<< endl;
    }


  return 0;
}
Hi,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
for (int counter = length-1 ; counter > 0 ; counter--)
    {
        for  (int i = 0 ; i < numbPerson ; i++)
        {
            if (numberOfPancakes[i] > numberOfPancakes[i+1])
            {
                pancakeHolder = numberOfPancakes[i];
                numberOfPancakes[i] = numberOfPancakes[i+1];
                numberOfPancakes[i+1] = pancakeHolder;
                personHolder = person[i];
                person[i] = person[i+1];
                person [i+1] = personHolder;
            }
        }
        numbPerson--;
    }



==>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
for (int counter = 0; counter < length; counter++)
    {
        for  (int counter2 = 0; counter2 < length - 1; counter2++)
        {
            if (numberOfPancakes[counter2+1] > numberOfPancakes[counter2])
  {
        pancakeHolder = numberOfPancakes[counter2];
       numberOfPancakes[counter2] = numberOfPancakes[counter2+1];
      numberOfPancakes[counter2+1] = pancakeHolder;
      personHolder = person[counter2];
      person[counter2] = person[counter2+1];
       person [counter2+1] = personHolder;
            }
        }
}      
Does that help you? :)
No not really, sorry, i dont get what you did. It seems you just change the name of some of my variables and made the sorting less efficient by deleting the " numbPerson-- " as it will continue to compare all of the values even the ones that are already sorted out.
> Made the sorting less efficient by deleting the " numbPerson-- " as it will continue to compare all of the values even the ones that are already sorted out.
No, that is just the nature of the Bubble sort's algorithm.
http://mathbits.com/MathBits/CompSci/Arrays/Bubble.htm

Anyway, what is your current program output now?
The first time the algorithm runs, it will analyze all of the components of the array, and because of the nature of this algorith, no matter where the biggest number is, it will always be sorted first movint it to the end in the first try, thus making it unnecesary to continue to analyse that last component.
Also the amount of times it needs to run is length-1 because it doesn't need to move the smaller number as it will be sorted automatically by sorting all the other numbers. What I am no sure about is if length is even affected by that -1 as it is a constant. I was having trouble in the output when length was not declared a constant. I dont understand why but it seemed to be solved that way.
At the moment, the program is running as intended, i figured out how to make the program output the number of the person, but i am still not sure if the sorting part is completely correct. The sorting itself is ok but, is that the only way to know the number of the person and the amount of pancakes eaten at the same time?
> At the moment, the program is running as intended
Ok, good to hear.
Does that mean your problem is solved now?
It would be better to sort FIRST. Then max would be the numberOfPancakes[0] and min would be numberOfPancakes[9].
> No, that is just the nature of the Bubble sort's algorithm.

No, the OP was trying to code modified Bubble Sort, which does NOT visit the already sorted part of the array, and hence is more efficient.
Topic archived. No new replies allowed.