Need help with Pancake Glutton exercise

I learned about arrays a couple days ago and just now started practicing them because I didn't know what I could use them for until now. I'm really struggling with this exercise. the part i'm stuck on now is "Once the data has been entered the program must analyze the data and output which person ate the most pancakes for breakfast." I know it's probably really simple, but for some reason this is the only way I thought of doing it, and it doesn't even work. What's the best way of doing this, and why doesn't this way work as intended?

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
#include <iostream>
#include <string>
#include<conio.h>
#include <cstdlib>
#include <ctime>
#include<iomanip>
#include <Windows.h>
using namespace std;

int main()
{
int ate[10];
int y;


        cout << "Enter a number of pancakes each person ate" << endl;
        cout << "\n \n";



    for(int x = 0; x<10; x++){
        cout << "Enter the number person " << x << " ate: ";
        cin >> ate[x];

}
    if (ate[0] > ate[1] && ate[2] && ate[3] && ate[4] && ate[5] && ate[6] && ate[7] && ate[8] && ate[9]){
        cout << "Person 0 ate the most" << endl;
}
    if (ate[1] > ate[0] && ate[2] && ate[3] && ate[4] && ate[5] && ate[6] && ate[7] && ate[8] && ate[9]){
        cout << "Person 1 ate the most" << endl;
}
    if (ate[2] > ate[1] && ate[0] && ate[3] && ate[4] && ate[5] && ate[6] && ate[7] && ate[8] && ate[9]){
        cout << "Person 2 ate the most" << endl;
}
    if (ate[3] > ate[1] && ate[2] && ate[0] && ate[4] && ate[5] && ate[6] && ate[7] && ate[8] && ate[9]){
        cout << "Person 3 ate the most" << endl;
}
    if (ate[4] > ate[1] && ate[2] && ate[3] && ate[0] && ate[5] && ate[6] && ate[7] && ate[8] && ate[9]){
        cout << "Person 4 ate the most" << endl;
}
    if (ate[5] > ate[1] && ate[2] && ate[3] && ate[4] && ate[0] && ate[6] && ate[7] && ate[8] && ate[9]){
        cout << "Person 5 ate the most" << endl;
}
    if (ate[6] > ate[1] && ate[2] && ate[3] && ate[4] && ate[5] && ate[0] && ate[7] && ate[8] && ate[9]){
        cout << "Person 6 ate the most" << endl;
}
    if (ate[7] > ate[1] && ate[2] && ate[3] && ate[4] && ate[5] && ate[6] && ate[0] && ate[8] && ate[9]){
        cout << "Person 7 ate the most" << endl;
}
    if (ate[8] > ate[1] && ate[2] && ate[3] && ate[4] && ate[5] && ate[6] && ate[7] && ate[0] && ate[9]){
        cout << "Person 8 ate the most" << endl;
}
    if (ate[9] > ate[1] && ate[2] && ate[3] && ate[4] && ate[5] && ate[6] && ate[7] && ate[8] && ate[0]){
        cout << "Person 9 ate the most" << endl;
}
    return 0;
}
Last edited on
The task is to find the maximum element in an array. There is standard algorithm std::max_element that resolves the task. You can write a similar function yourself

1
2
3
4
5
6
7
8
int max = 0;

for ( int i = 1; i < 10; i++ )
{
   if ( ate[max] < ate[i] ) max = i;
}

cout << "Person " << max + 1 << " ate the most" << endl;
Last edited on
Thanks for the help again
I didn't want to make a new topic since it's about the same thing kind of. I don't understand the 2nd and 3rd for loops don't end, it keeps looping them over and over. Also i'm still in the process of figuring out how to sort the pancakes so don't tell me if that part is wrong, I just want to know why the loop won't stop.


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
#include <iostream>
#include <string>
#include<conio.h>
#include <cstdlib>
#include <ctime>
#include<iomanip>
#include <Windows.h>
using namespace std;

int main()
{
int ate[10];
int most = 0;
int least = 0;
int ho;

        cout << "Enter a number of pancakes each person ate" << endl;
        cout << "\n \n";



    for(int x = 0; x<10; x++){
        cout << "Enter the number person " << x + 1<< " ate: ";
        cin >> ate[x];
}


    for(int s = 0; s < 10; s++){
    for (int h = 1; h < 10; h++){
        if(ate[s] > ate[h]){
            ho = s;
            s = h;
            h = ho;

}
}
}

    for(int t = 0; t<10; t++){
        cout << ate[t] << endl;

}







    return 0;
}
I just want to know why the loop won't stop.


You are modifying the variables that control the loop execution in the body of the loop in such a way that they never reach the end condition. I suspect the if body was meant to look like:

1
2
3
4
5
6
            if ( ate[s] > ate[h] )
            {
                ho = ate[s] ;
                ate[s] = ate[h] ;
                ate[h] = ho ;
            }
Well now I feel dumb, I should have seen that. Thanks for the help
Topic archived. No new replies allowed.