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.
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";
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;
}
}
}
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.