Pancake glutton

I am fully aware the fact that this might be already posted.However,I just wanted to improve further on my codes,so I ask your help.

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

#include<iostream>
using namespace std;

int main() {
    
int pancake[5];
int i;
int z;
int max = 1;
int min = 1;
int max_person = 1;
int min_person = 1;



for (i = 1;i < 5;i++) {
    cout << "how many pancakes did person " << i << " eat?" << endl;
    cin >> pancake[i];
}

for (z = 1;z < 5;z++) {
    
    for (i = 1;i < 5;i++)
    {
        if (pancake[z] < pancake[i])  {
        max = pancake[i];
        max_person = i;
        }
        
        else if (pancake[i] < pancake[z]){
             
        min = pancake[i];
        min_person = i;
        }
       
        }
}




cout << max << " is the most pancake eaten by person " << max_person <<  endl;

cout << min << " is the most pancake eaten by person " << min_person <<  endl;


    
    
    
 
         
    
         
    









system("pause");
return 0;
}


The code work perfectly,but I just wanted to ask,how do I improve further?and also,how do I get the program to list the numbers in the order?(so, one who ate the most pancakes to least pancakes.)

Thank you.
Why not make pancake[4] and start from 0? That saves memory. Otherwise it isn't bad.

To sort everything you need to implement a sorting algorithm. Something like this would work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool sorted = true;
while (sorted_occurred)
{
  sorted_occurred = false;
  for (int i = 1; i < 5; i++)
    for (int j = 1; j < 5; j++)
      if (array[i] < array[j])
      {
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
        sorted_occurred = true;
      }
}
Hi

Well, to start with, the loop which finds the min and max can be simplified. It's doing a good bit more work than it needs to.

To list the values in order, I would sort the them. But while sorting the numbers would be trivial, it's a bit harder so sort them while remembering the original number, so you can say (eg) person 3 ate the most pancakes.

A cheap solution would be to use the min and max values in a for loop and hunt for the entries that matched each value (does that make sense?). It's not the best solution, but if you're not familar with either stl or qsort yet, it might not be such a bad idea.

And I would sort out the formatting and variable names. The cplusplus.com editor is not the best, but good layout (whatever style you choose) can make code a lot easier to follow. And variable names need to be complete! e.g. pancakes_eaten[].

You could extend your program to remember the names of the pancake eaters, while you're at it.

If you do, consider using a struct to store the data about a person.

Oh, and remember C++ arrays start at 0. You're not using the first array element at the moment.

Andy
Last edited on
A few comments/suggestions:

1)
Your pancake array is big enough to hold 5 numbers, but you're only using 4 of them
1
2
3
for (z = 1;z < 5;z++) {
    
    for (i = 1;i < 5;i++) 

Arrays start from index zero, but at the moment, your for loops all begin at 1, so you're missing out on your first (Zero'th) element.


2)
Many C++ programmers follow a principle known as "declare on first use" - which simply means waiting to declare/define variables until they're actually needed - this can be a good habit to get in to. instead of having a big lump of variables all declared at the top main, you can create them at the same time as they're first needed. e.g.
1
2
3
for (int z = 0;z < 5;z++) {
    
    for (int i = 0;i < 5;i++) 
Doing this limits the scope of 'i' and 'z' to the block of code where they're needed and nowhere else - this reduces the chance that you might slip up somewhere else by using 'i' or 'z' for something which you didn't intend (mistakes are very easy to make, especially in more complicated code when you've perhaps got a lot of variables)


3)
Try to be consistent with your placement of braces - consistent code is easier to read for everyone. Some people prefer to align them horizontally, other people prefer to put them at the end of the previous line. It doesn't matter "style" you prefer (and the style you choose is a personal preference), just so long as you stick to it


- As for putting your pancake array into order, you could either write your own sorting code (Have a google search for bubble sort and selection sort ) or you can use the quick-and-easy way which C++ provides for you out-the-box, called 'sort'. (I'd suggest you try out both bubble sort and selection sort though)

#include <algorithm> // Contains useful tools for handling arrays and other sets of data
...
1
2
3
4
5
6
7
8
int pancake[5];

for (int i = 0;i < 5;i++) {
    cout << "how many pancakes did person " << i << " eat?" << endl;
    cin >> pancake[i];
}

sort( pancake, pancake+5 ); 

Don't be put-off by the 'pancake+5' - sort needs to know the start and end of pancake in order to sort it. in plain english, pancake+5 translates into "after 5 elements of the pancake array"

(Side Note: the + uses a mechanism called pointer arithmetic - but you don't need to worry about that for now.)
Last edited on
One tweak...

If you're going to the trouble of using STL, etc. then I'd eliminate the magic number in passing.

1
2
3
4
5
6
7
8
9
10
const int num_people = 5;

int pancake[num_people]

for (int i = 0; i < num_people; i++) {
    cout << "how many pancakes did person " << i << " eat?" << endl;
    cin >> pancake[i];
}

sort( pancake, pancake + num_people );
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

#include<iostream>
using namespace std;

int main() {
    
int pancake[5];
int max = 1;
int min = 1;
int max_person = 1;
int min_person = 1;



for (int i = 0;i < 5;i++) { // used array from the zeroth element,not wasting my 'resources.'
    cout << "how many pancakes did person " << i << " eat?" << endl;
    cin >> pancake[i];
}

for (int z = 0;z < 5;z++) { 
    
    for (int i = 1;i < 5;i++) // used the so called 'declare on the spot.'
    {
        if (pancake[z] < pancake[i])  {
        max = pancake[i];
        max_person = i;
        }
        
        else if (pancake[i] < pancake[z]){
             
        min = pancake[i];
        min_person = i;
        }
       
        }
}




cout << max << " is the most pancake eaten by person " << max_person <<  endl;

cout << min << " is the least pancake eaten by person " << min_person <<  endl;


    
    
    



system("pause");
return 0;
}



I took your advices.Well,there very good advices,but it's causing problems.What the problem is,that when I start with zeroth element,the program ignores the zeroth element.So,for example,,,,

num of pancake eaten by person 0:
10
num of pancake eaten by person 1:
1
num of pancake eaten by person 2:
2
num of pancake eaten by person 3:
3
num of pancake eaten by person 4:
4

max number of pancake was eaten by,,,person 4(should have been person 0.)
Here, you are using 'i' to set min and max...
1
2
3
4
5
6
7
8
9
10
        if (pancake[z] < pancake[i])  {
        max = pancake[i];
        max_person = i;
        }
        
        else if (pancake[i] < pancake[z]){
             
        min = pancake[i];
        min_person = i;
        }



Except that 'i' will never point you at the zeroth element, since it starts at 1 (from your inner loop) :
1
2
3
for (int z = 0;z < 5;z++) { 
    
    for (int i = 1;i < 5;i++) 
Last edited on
Congratulations on the best topic title I've seen in a long time.


Here, you are using 'i' to set min and max...

1
2
3
4
5
6
7
8
9
10
if (pancake[z] < pancake[i]) {
max = pancake[i];
max_person = i;
}

else if (pancake[i] < pancake[z]){

min = pancake[i];
min_person = i;
}





Except that 'i' will never point you at the zeroth element, since it starts at 1 (from your inner loop) :
1
2
3
for (int z = 0;z < 5;z++) {

for (int i = 1;i < 5;i++)









sorry for the late reply,but I'm afraid that doesn't work.It still ignores the zeroth element.
Topic archived. No new replies allowed.