Program problem

Well, I'm trying to make this program but its giving me a hard time. Can anyone please make this so that i could understand it. Then I will make it from scratch again. It is one of the practice exercises on the site.


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
This is how I'd do it. Please note that there are many ways to attempt this problem for e.g. using the algorithm library: #include <algorithm>.

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
#include <iostream>
#include <vector>

using namespace std;

int max(vector<int> cakes)
{
    int index = 0;
    int maximum = 0;
    for(int i = 0; i < cakes.size(); i++)
    {
        if(cakes[i] > maximum)
        {
            maximum = cakes[i];
            index = i;
        }
    }
    return index;
}

int min(vector<int> cakes)
{
    int index = 0;
    int minimum = cakes[max(cakes)];
    for(int i = 0; i < cakes.size(); i++)
    {
        if(cakes[i] < minimum)
        {
            minimum = cakes[i];
            index = i;
        }
    }
    return index;
}

int main()
{
    vector<int> pancakes;
    const int persons = 10;

    for(int i = 1; i <= persons; i++)
    {
        int vari;
        cout << "Enter pancakes eaten by Person " << i << ": ";
        cin >> vari;
        pancakes.push_back(vari);
    }

    cout << "\nMost pancakes was eaten by Person " << max(pancakes)+1 << " - " << pancakes[max(pancakes)] << " pancakes." << endl;
    cout << "Least pancakes was eaten by Person " << min(pancakes)+1 << " - " << pancakes[min(pancakes)] << " pancakes." << endl;
    cout << "\n----List----\n" << endl;
    for(int i = 0; i < persons; i++)
    {
        cout << "Person " << max(pancakes)+1 << ": ate " << pancakes[max(pancakes)] << " pancakes" << endl;
        pancakes[max(pancakes)] = 0;
    }

    return 0;
}
Thank you so much. Now I'm going to understand how you made it and then start from scratch in my own way. Once again. Thank you !
//Ok so I made this now. Still working on it. but I'm stuck now.I don't know //how to make the program use the values of 'y' I entered in the list.
//It only shows the last value of 'y' I entered in the list.

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

#include <iostream>

using namespace std;

int main()
{
    int x;
    int y;

    for(x=1 ; x<=10; x++)
    {
        cout << "Enter the number of pancakes eaten by "<< x << endl;
        cin >> y;
    }

    cout << "Person    -    Pancakes\n";

    for (x=1 ;x<=10; x++)
    {
        cout << x << "    -    "<< y << endl;
    }

return 0;
}

you need 'y' to be a vector or an array. You can NOT input multiple values into an integer alone. Each iteration of you for loop changes the value of 'y'. Try:
1
2
3
4
5
6
7
8
9
10
const int MAX = 10; 
int y[MAX];

//now in your for loop to enter the values for each element of 'y' use

cin >> y[x];

//but you also need to start the value of x in your for loop to 0
//when you want to output 1 when x = 0 just output x+1
@ OUIJ: Can you please make these changes in my program which you just told me? I can't seem to figure it out.

This is mine atm. Its not working properly.

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

#include <iostream>

using namespace std;

int main()
{
    int x;
    const int MAX = 10;
    int y[MAX];

    for(x=1 ; x<=10; x++)
    {
        cout << "Enter the number of pancakes eaten by "<< x << "." << endl;
        cin >> y[x];
    }

    cout << "Person    -    Pancakes\n";

    for (x=1 ;x<=10; x++)
    {
        cout << x << "    -    "<< y << endl;
    }

return 0;
}

Do cin >> y[x-1];.
This is because array index starts at 0. And ends at MAX-1. So you'll get an error when x reaches 10.

For printing the results do (in your for-loop):

cout << x << " - "<< y[x-1] << endl;

You need to access each element in the array y.

Refer to this: http://www.cplusplus.com/doc/tutorial/arrays/
It explains array in depth. :)
Last edited on
Topic archived. No new replies allowed.