Pancakes Glutton

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
// Pancakes Glutton

/*
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.h>
#include <conio.h>


void main()
{

int i,min,max,pers[10];

cout<<"How many pancakes did the following persons ate?\n";

for(i=1;i<=5;i++)
   {
   cout<<"Person "<<i<<":";
   cin>>pers[i];
   }

min=pers[1];
max=pers[i];

for(i=2;i<=5;i++)
   {
   if(min>pers[i])  // calculez min
      min=pers[i];
   if(max<pers[i])  // calculez max
      max=pers[i];
   }

for(i=1;i<=5;i++)
   {
   if(min==pers[i])
      cout<<"\nPersoan "<<i<<" ate the least pancakes! ("<<min<<")";
   if(max==pers[i])
      cout<<"\nPersoan "<<i<<" ate the most pancakes! ("<<max<<")";
   }


for(i=1;i<=5;i++)
   for(int j=i+1;j<=5;j++)
      if(pers[j]<pers[i])
         {
         int aux=pers[j];
         pers[j]=pers[i];
         pers[i]=aux;
         }

for(i=1;i<=5;i++)
   cout<<pers[i]<<" ";



   getch();
}


hi everyone, my first post here :) .. my question is why is it when i started sorting the "number of pankes ate" the max stopped showing on execution ? been sitting here for an 1h now and cant figure it out , tnx for your time. (if i set the sorting part as a comment the max works fine)

PS: any ideea on how i can keep the number of the person so i can print it at the end ? i was thining of making another array (pers[20][20]) to keep the number aswell, but that sounds like alot of work :D any easier way ?
What do you mean the "max stopped showing on execution"?

As for your second question;
The number of the person should be their index in the array; for example, person 10 should be pers[9] (because array indexes go from 0-n).
You may want to consider using a record to hold your two pieces of information, in C++ that's struct. Your two data items are person number and number of pancakes. You could then hold an array of those.

When sorting, you'd compare the number of pancakes only.

This would allow you to use zero based arrays. You're using 1 based at the moment. It isn't really a logical error (yet), it's more a matter of convention.

Plus one hint:
1
2
3
4
5
for(i=1;i<=5;i++)
   {
   if(min==pers[i])
      cout<<"\nPersoan "<<i<<" ate the least pancakes! ("<<min<<")";
   }


is equivalent to:
cout<<"\nPersoan "<<pers[min]<<" ate the least pancakes! ("<<min<<")"; // **EDIT**
Last edited on
in line 38, i had to check pers[1] or max didn't work right sometimes.
for(i=1;i<=5;i++)

ps:
if your teacher won't let you use struct, you can use another array to represent your pers array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// create the array to sort
int sortedpers[10];

// use numbers inside sortedpers[] to represent pers[]
// initialize index. sortedpers = pers number.
for(i = 1; i <= 5; i++) sortedpers[i] = i;

// your sort is same except you sort sortedpers[] through pancakes in pers[].
// sortedpers[i] is person (location within array).
// pers[sortedpers[i]] is still pancakes. sorted pancakes.
for(i=1;i<=5;i++)
   for(int j=i+1;j<=5;j++)
      if(pers[sortedpers[j]]<pers[sortedpers[i]])
         {
         int aux=sortedpers[j];
         sortedpers[j]=sortedpers[i];
         sortedpers[i]=aux;
         }

// then you output the sorted list
for(i=1;i<=5;i++) cout << "person " << sortedpers[i] << ": ate " << pers[sortedpers[i]]<< endl;

note: your sorted results are backwards. your teacher wants the list to go max to min. you are going to get a ruler across the knuckles for sure.

whoops. fixed backwards comment in code. ha ha. this is confusing, isn't it?
Last edited on
tnx for all the info guys .. will try to use struct now , and YES it is kinda confusing !

as for this for(i=1;i<=5;i++) in my code i started with 2 cause i initialized both min and max with pers[1];

PS: this isnt a homework or something .. this is one of the Begginger Exercises on the C++ Forum :)
Topic archived. No new replies allowed.