Pancakes glutton

I know that this problem has been solved, but I ran into a little issue here.

I displayed the pancakes in descending order. So my problem is almost done, except for one detail. I want to display the person's number corresponding to the pancakes they ate, but I don't know how.


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
#include <iostream>
#include <conio.h>
#include <string>

using namespace std;

int main()
{
	 
	int  person[100], i, j, t;
	int pancakes[100];
	for(i=1; i<11; i++) 
	{
		cout << "How many pancakes ate person "<<i<< "?\n";
		cin >> pancakes[i];
	}

	
	//Sorting descending order
	for(i=1; i<10; i++)
	{
		for(j=i+1; j<11; j++)
		{
			if(pancakes[i] < pancakes[j])
			{
				t=pancakes[i]; 
			    pancakes[i]=pancakes[j]; 
				pancakes[j]=t;
			}
		}
	}

	//Displaying the pancakes number
		for(i=1; i<=10; i++)
		{
             //How to display the person's number?
			cout<<"Person: "<<   " ate "<<pancakes[i]<<"\n";  
		}

	_getch();
}
@Victor89
Well you could do it this way..

1
2
3
4
5
6
7
for(i=1; i<11; i++) 
	{
		cout << "How many pancakes ate person "<<i<< "?\n";
		cin >> pancakes[i];
                // Add this
                person[i] = i;// Assign 1 to 10 into person array.
	}


1
2
3
4
5
6
7
8
9
if(pancakes[i] < pancakes[j])
{
	t=pancakes[i];
        int x = person[i]; // Add this 
       pancakes[i]=pancakes[j]; 
	pancakes[j]=t;
       person[i] = person[j]; // Add this
       person[j] = x; // Add this
}


And lastly
cout<<"Person: "<< person[i] << " ate "<<pancakes[i]<<"\n";


Last edited on
Thank you very much, I like your solution
@Victor89

Thank you. I'm sorry to tell you, but your bubble sort is wrong. I hadn't noticed it when I was adding in the person[] checking.
It should be like this..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//Sorting descending order
 for(i=1; i<10; i++)
 {
	for(j=0; j<9; j++) // 1 less than how many array locations filled
	{
	 if(pancakes[j] < pancakes[j+1]) // Check if this location is less than next location
	 {
               // Switch the two locations values, for pancakes[] and person[]
		t=pancakes[j];
		int diner=person[j]; 
		pancakes[j]=pancakes[j+1];
		person[j] = person[j+1]; 
		pancakes[j+1]=t;
		person[j+1]=diner;
	 }
	}
 }
I used select sort, and it works like it is supposed to work.
I don't think it's wrong or am I missing something?
@Victor89

Sorry. When I put the file into my compiler, I had changed your for(j=i+1; j<11; j++) to for(j=1; j<11; j++) which changed what was being checked. Your way was fine, I made the goof.
Topic archived. No new replies allowed.