some exercise

Hi, I was trying to do a beginner's exercise from here and needed some help.

the user puts like how many pancakes he ate, and as usual i took the index of the array to display the specific person who ate that much of pancake.

lets say only 3 person is there who ate 3 different numbers of pancakes.

user puts, 6 10 and 2,

i display the original order,
person 1 6
person 2 10
person 3 2.

my question is, the exercise told me to do a descending order list sth like this. from high to low with correct person.

person 2 ate 10 pancakes
person 1 ate 6 pancakes
person 3 ate 2 pancakes

how will i sort the array in a way that i can obtain the previous index/person who ate that much of pancake.

further explanation, i sorted with a bubble sort. i swap the elements of array from high to low but i am confused how to show the correct person who actually ate the high to low pancakes,sorry if i am a bit confusing but if its still unclear i will try to explain again.

this was from a biginner's exercise and didnt want to use sorting arrays, if theres a simple r way that i missed i would also like to know that.
Thank you!

below are my codes,

#include <iostream>

using namespace std;

//function pro
void sortarrays(int pancake[],int size);
void showarray(int pancake[],int size);


int main()
{
int pancake[3]{};

int G;
int i = 0;
int person ;


do {

cout << "Enter the number of pancakes you have eaten, person " << i+1 <<":"<<endl;
cin >> pancake [i];
i++;
}while (i<3);

//callshowarray
cout << "the original array" <<endl;

showarray(pancake,3);
cout << endl;

//sortedarrays
sortarrays(pancake,3);


//call again
cout << "most to least" << endl;

showarray(pancake,3);
cout << endl;



return 0;


}


void sortarrays(int pancake[],int size){
bool swap;
int temp;

do
{
swap=false;
for (int count= 0; count<size-1 ;count++){
if(pancake[count]<pancake [count + 1]){
temp=pancake[count];
pancake[count]=pancake[count+1];
pancake[count+1]=temp;
swap=true;
}


}
}while (swap);
}

void showarray(int pancake[],int size){

for(int count=0; count<size; count++){

cout<< pancake[count] << " ";
}
}

One way to do this is to use parallel arrays. Make two arrays of the same size: one holds person #'s and the other holds the corresponding # of pancakes eaten by that person.

When doing bubble sort, when you swap two elements in the pancakes array, swap the same two elements in the person's array. This will keep the person # and pancakes eaten together even after the sort.
Last edited on
Thank you so much!
Topic archived. No new replies allowed.