Pancake Glutton (I prefer waffles, myself)

ok, i need a little help on the pancake glutton exercise, i have posted the instructions for it in my code. i am only doing the first "part" right now, i will try the other parts later. i dont want to make the program anymore complicated than it is, not because im afraid to, but i want to stick to the rules of the exercise (only using arrays, for, if, basic I/O, etc.)

ok, i know the program somewhat works, but i have two questions:


1) Why does the output always make the "person that ate the least" always call them "Person No. 1975606204"?

i know this is a problem with my coding, but i just dont understand _where_ the problem is.


2) I am new to C++, does my code look "pretty"? is there anything i can do to clean it up, or make it acceptable as far as cleanliness and organization? any criticism is welcome, even if you are just offering your opinion, or telling me how you would do it instead.


here is my code
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
/********************************************


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.

+ 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


********************************************/

#include <iostream>

using namespace std;

int main()
{
    int ate[10]; //array for recording data
    int p;       //var for most pancakes eaten
    int q;       //var for person that ate the most
    int x;       //var for least eaten
    int s;       //var for person that eat the least

    cout << "How many pancakes did each person eat?\n";
    for (int a=0; a<10; a++)
    {
        cout << "Person " << a+1 << ": ";
        cin >> ate[a];
    }

    p = ate[0];
    x = 0;

    for (int b=0; b<10; b++)
    {
        if (p<ate[b])
        {
            p = ate[b];
            q = b+1;
        }

        if (x>ate[b])
        {
            x = ate[b];
            s = b+1;
        }
    }

    cout << "Person No. " << q << " ate the most: " << p <<endl;
    cout << "Person No. " << s << " ate the least: " << x <<endl;

    for (int a=0; a<10; a++)
    {
        cout << ate[a] << ", ";
    }

    return 0;
}


output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
How many pancakes did each person eat?
Person 1: 5
Person 2: 4
Person 3: 3
Person 4: 2
Person 5: 1
Person 6: 10
Person 7: 9
Person 8: 8
Person 9: 7
Person 10: 6
Person No. 6 ate the most: 10
Person No. 1975606204 ate the least: 1
5, 4, 3, 2, 1, 10, 9, 8, 7, 6, 
If you'd named your variables better this wouldve been a lot easier to spot. Don't use single letters, you couldve called them minEaten, maxEaten, minPerson and maxPerson or something.

Anyway, you never initialise s to anything, and because you initialise x to 0, this if statement

1
2
3
4
5
if (x>ate[b])
{
    x = ate[b];
    s = b+1;
}


is never true, unless one of your people ate negative pies, so s (minPerson) is never given a value.

Also, you don't really need to store the max and min number of pies eaten, as long as you're storing the position in the array of the max and min, you can use that to print out the amount of pies as well as which number person it was.
Last edited on
thank you very much! i appreciate your help. it works much better now, and i cleaned up the variable names.


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


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.

+ 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


********************************************/

#include <iostream>

using namespace std;

int main()
{
    int ate[10]; //array for recording data
    int maxAte;       //var for most pancakes eaten
    int maxPer;       //var for person that ate the most
    int minAte;       //var for least eaten
    int minPer;       //var for person that eat the least

    cout << "How many pancakes did each person eat?\n";
    for (int a=0; a<10; a++)
    {
        cout << "Person " << a+1 << ": ";
        cin >> ate[a];
    }

    maxAte = ate[0];

    for (int b=0; b<10; b++)
    {
        if (maxAte<ate[b])
        {
            maxAte = ate[b];
            maxPer = b+1;
        }

        if (minAte>ate[b])
        {
            minAte = ate[b];
            minPer = b+1;
        }
    }

    cout << "Person No. " << maxPer << " ate the most: " << maxAte <<endl;
    cout << "Person No. " << minPer << " ate the least: " << minAte <<endl;

    for (int a=0; a<10; a++)
    {
        cout << ate[a] << ", ";
    }

    return 0;
}
Just one more thing that I should've said before, to avoid uninitialised variables, whenever possible you should initialise them at the same time as declaring them.
ok, thanks quirky, i will definitely keep that in mind. thanks again :)
Topic archived. No new replies allowed.