C++ Exercise II

Aug 16, 2019 at 4:17pm
Hi Guys...

I tried to do the following exercise:

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.

And I failed. Here is the code that I tried:

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
  #include <iostream>
#include <string>
using namespace std;

int main() {
    int x = 0;
    int a = 1;
    int b = 2;
    int c = 3;
    int d = 4;
    int e = 5;
    int f = 6;
    int g = 7;
    int h = 8;
    int i = 9;
    int j = 10;
    cout << "How many pancakes has Person " << a << " eaten? - Write the nr. here: ";
    cin >> x;
    cout << "How many pancakes has Person " << b << " eaten? - Write the nr. here: ";
    cin >> x;
    cout << "How many pancakes has Person " << c << " eaten? - Write the nr. here: ";
    cin >> x;
    cout << "How many pancakes has Person " << d << " eaten? - Write the nr. here: ";
    cin >> x;
    cout << "How many pancakes has Person " << e << " eaten? - Write the nr. here: ";
    cin >> x;
    cout << "How many pancakes has Person " << f << " eaten? - Write the nr. here: ";
    cin >> x;
    cout << "How many pancakes has Person " << g << " eaten? - Write the nr. here: ";
    cin >> x;
    cout << "How many pancakes has Person " << h << " eaten? - Write the nr. here: ";
    cin >> x;
    cout << "How many pancakes has Person " << i << " eaten? - Write the nr. here: ";
    cin >> x;
    cout << "How many pancakes has Person " << j << " eaten? - Write the nr. here: ";
    cin >> x;
    if (a >= b && a >= c && a >= d && a >= e && a >= f && a >= g && a >= h && a >= i && a >= j)
    {
    cout << "The person who ate the most pancakes for breakfast is Person: " << a;
    }
    if (b >= a && b >= c && b >= d && b >= e && b >= f && b >= g && b >= h && b >= i && b >= j)
    {
        cout << "The person who ate the most pancakes for breakfast is Person: " << b;
    }
    if (c >= a && c >= b && c >= d && c >= e && c >= f && c >= g && c >= h && c >= i && c >= j)
    {
    cout << "The person who ate the most pancakes for breakfast is Person: " << c;
    }
    if (d >= a && d >= b && d >= c && d >= e && d >= f && d >= g && d >= h && d >= i && d >= j)
    {
        cout << "The person who ate the most pancakes for breakfast is Person: " << d;
    }
    if (e >= a && e >= b && e >= c && e >= d && e >= f && e >= g && e >= h && e >= i && e >= j)
    {
        cout << "The person who ate the most pancakes for breakfast is Person: " << e;
    }
    if (f >= a && f >= b && f >= c && f >= d && f >= e && f >= g && f >= h && f >= i && f >= j)
    {
        cout << "The person who ate the most pancakes for breakfast is Person: " << f;
    }
    if (g >= a && g >= b && g >= c && g >= d && g >= e && g >= f && g >= h && g >= i && g >= j)
    {
        cout << "The person who ate the most pancakes for breakfast is Person: " << g;
    }
    if (h >= a && h >= b && h >= c && h >= d && h >= e && h >= f && h >= g && h >= i && h >= j)
    {
        cout << "The person who ate the most pancakes for breakfast is Person: " << h;
    }
    if (i >= a && i >= b && i >= c && i >= d && i >= e && i >= f && i >= g && i >= h && i >= j)
    {
        cout << "The person who ate the most pancakes for breakfast is Person: " << i;
    }
    if (j >= a && j >= b && j >= c && j >= d && j >= e && j >= f && j >= g && j >= h && j >= i)
    {
        cout << "The person who ate the most pancakes for breakfast is Person: " << j;
    }
    return 0;


Any suggestions? :(
Aug 16, 2019 at 4:20pm
Any suggestions?

How about a loop or two and an array or two?

Aug 16, 2019 at 4:29pm
Ok! Hold on, I'll try with that, don't give me an answer yet. Give me 10 min, I need to think this :S
Aug 16, 2019 at 4:40pm
Hi...

:(

I tried using arrays and loops, this is the best I could come up with:

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
#include <iostream>
#include <string>
using namespace std;

int main() {
    string Persons[10] = {"Person 1", "Person 2", "Person 3", "Person 4", "Person 5", 
                        "Person 6", "Person 7", "Person 8", "Person 9", "Person 10"};
    for (int y = 0; y < 10; y++) {
    int x;
    cout << "How many pancakes has Person 1 eaten? - Write the nr. here: ";
    cout << x;
    cout << "How many pancakes has Person 1 eaten? - Write the nr. here: ";
    cout << x;
    cout << "How many pancakes has Person 1 eaten? - Write the nr. here: ";
    cout << x;
    cout << "How many pancakes has Person 1 eaten? - Write the nr. here: ";
    cout << x;
    cout << "How many pancakes has Person 1 eaten? - Write the nr. here: ";
    cout << x;
    cout << "How many pancakes has Person 1 eaten? - Write the nr. here: ";
    cout << x;
    cout << "How many pancakes has Person 1 eaten? - Write the nr. here: ";
    cout << x;
    cout << "How many pancakes has Person 1 eaten? - Write the nr. here: ";
    cout << x;
    cout << "How many pancakes has Person 1 eaten? - Write the nr. here: ";
    cout << x;
    cout << "How many pancakes has Person 1 eaten? - Write the nr. here: ";
    cout << x;
    cout << Persons[y];
    }
    return 0;
}


So, how can I put a stop to the loop from running automatically while using arrays?

That code doesn't give me the option to input a number, it runs by itself. Using BREAK could help? or CONTINUE, but these are only to be used in switches. Could you guys give me hints?
Aug 16, 2019 at 4:45pm
How about something more like:

1
2
3
4
for (int i = 0; i < 10; i++) {
    cout << "How many pancakes has Person " << i + 1 << " eaten? - Write the nr. here: ";
    cout << person[i];
}


Of course you'll need to use the proper type of variable for person[].


Edit:
Using BREAK could help? or CONTINUE, but these are only to be used in switches

What? The break and continue statements also work inside loops.

Last edited on Aug 16, 2019 at 4:47pm
Aug 16, 2019 at 4:55pm
I see, Ok so following your example, I got to loop each person by their corresponding number.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
using namespace std;

int main() {
    string Persons[10] = {"Person 1", "Person 2", "Person 3", "Person 4", "Person 5", 
                        "Person 6", "Person 7", "Person 8", "Person 9", "Person 10"};
    for (int y = 0; y < 10; y++) {
    int x;
    cout << "How many pancakes has Person " << y + 1 << " eaten? - Write the nr. here: ";
    cout << Persons[y];
    }
    return 0;
}


Now I still have to find out how to stop the loop 'for' from running on its own. Then, I have to find a way to make the user input the number of pancakes eaten by each person. That comes second thoough, first I need to stop the loop from running automatically... any hints for this?
Aug 16, 2019 at 5:02pm
That comes second thoough, first I need to stop the loop from running automatically... any hints for this?

What are you talking about? You told the program to loop 10 times, it is doing exactly what you told it to do.

Why are you trying to print the "person's name" in that loop instead of asking the user to enter the number of pancakes eaten?

Did you misunderstand something about the instructions?
Part 1:
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)


Part 2:
Once the data has been entered the program must analyze the data and output which person ate the most pancakes for breakfast.


What is the purpose of your current array? Don't you realize that you can print the "person number" without any array?

Aug 16, 2019 at 6:27pm
I feel like being an ass and pointing out that this problem can be solved without using arrays (or any collections). It just needs one (for) loop. That's not the most helpful advice from me, though, since you'll want to use some kind of collection (arrays, or a priority_queue) to solve one of the extra challenges:

★★★★ Modify the program so that it outputs a list in order of number of pancakes eaten of all 10 people.


@LuigiIstratescu: You seem to be trying random stuff and seeing what sticks. Please slow down, and think about what your overall program should look like.

Here's a skeleton for ya. Try solving this problem by replacing each TODO comment with the appropriate block of code.

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <array> //For converting to idiomatic C++11 later.

int main() {
    int pancakes_eaten[10];
    //TODO: For loop to read into each element of pancakes_eaten.
    int most_eaten_index = 0;
    //TODO: For loop to find the index of the largest value in pancakes_eaten.
    //Hint: Compare pancakes[i] and pancakes[most_eaten_index].
    //TODO: Print who ate the most pancakes (based on most_eaten_index).
}


For the first modification to your program:
★ Modify the program so that it also outputs which person ate the least number of pancakes for breakfast.

Try writing up a skeleton like that one before you start writing any logic, with just comments on what your program should do at each stage, and maybe a few variables that you know for sure you'll need.

-Albatross
Last edited on Aug 16, 2019 at 6:30pm
Aug 16, 2019 at 6:29pm
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
#include <iostream>
#include <vector>

int main()
{
   // create a vector (array) that can store 10 elements, fill with zero
   std::vector<unsigned int> pancake_eaters(10, 0);

   for (size_t loop = 0; loop < pancake_eaters.size(); loop++)
   {
      std::cout << "Person " << loop + 1 << " had how many pancakes? ";
      std::cin >> pancake_eaters[loop];
   }
   std::cout << '\n';

   size_t       index  = 0;
   unsigned int person = 0;

   // the following logic presumes there is only one person
   // who ate the most pancakes
   for (size_t loop = 0; loop < pancake_eaters.size(); loop++)
   {
      if (pancake_eaters[loop] > person)
      {
         person = pancake_eaters[loop];
         index  = loop + 1;
      }
   }

   std::cout << "The person who ate the most pancakes was #" << index << '\n';
}
Person 1 had how many pancakes? 1
Person 2 had how many pancakes? 3
Person 3 had how many pancakes? 5
Person 4 had how many pancakes? 7
Person 5 had how many pancakes? 9
Person 6 had how many pancakes? 8
Person 7 had how many pancakes? 6
Person 8 had how many pancakes? 4
Person 9 had how many pancakes? 2
Person 10 had how many pancakes? 4

The person who ate the most pancakes was #5

Why use unsigned int instead of int? No one can eat a negative number of pancakes.
Aug 16, 2019 at 7:18pm
No one can eat 4294967295 pancakes, either.
Person 1 had how many pancakes? -1
Person 2 had how many pancakes? 0
Person 3 had how many pancakes? 0
Person 4 had how many pancakes? 0
Person 5 had how many pancakes? 0
Person 6 had how many pancakes? 0
Person 7 had how many pancakes? 0
Person 8 had how many pancakes? 0
Person 9 had how many pancakes? 0
Person 10 had how many pancakes? 0

The person who ate the most pancakes was #1
Last edited on Aug 16, 2019 at 7:20pm
Aug 16, 2019 at 8:14pm
No one can eat 4294967295 pancakes, either.

1. You've not met some of my relatives, they'd take that as a challenge.

2. No mention in the assignment criteria of how big or small the pancakes are, nor what the length of time for the consumption happens to be.

So......it is theoretically possible to consume a large quantity of very, very, very small pancakes in several decades vs. a negative amount. :Þ

All joking aside, this brings up the issue of data integrity, and using "properly sized" data type for the storage requirements.

Admittedly not something that is of much concern to beginning programmers simply trying to learn the language. It is something that can matter after gaining experience.

Trade-offs, ain't they grand.
Last edited on Aug 16, 2019 at 8:17pm
Topic archived. No new replies allowed.