#include<iostream>
usingnamespace std;
int main() {
int pancake[5];
int swap = 0;
int temp;
for (int i = 0;i < 5;i++)
{
cout << "how many pancakes did person " << i << " eat?" << endl;
cin >> pancake[i];
}
for (int i = 0;i < 5;i++) {
if ( pancake[i] > pancake[i + 1]) {
temp = pancake[i];
pancake[i] = pancake[i + 1];
swap = 1;
}
}
for (int i = 0;i < 5;i++) {
cout <<pancake[i] << " " << endl;
}
system("pause");
return 0;
}
I'm kinda having problem with bubble sort.The program is desgined so that it can sort the numbers out in the order,from most to least.But no.The program prints out garbage and does not work.any help will be appreciated.
Bubble sorts usually have two loops in them and they are usually nested. What I see is not a bubble sort and I am not sure what you are trying to do since most sorts have two loops unless I am building the with a insertion sort.
Thank you, and I'm sorry for making you put up with alot of patience on me.However,I do have ONE LAST question.I modified the code a little bit like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
pancake[z] = temp; // end of the sort
int person = i; // You'll later(or probaly already) know why I put this here.
}
} // end of inner loop.
} // end of outer loop.
for (int i = 1;i < 5;i++)
{
cout << "person" << person << " ate " << pancake[i] << " pancake(s)" << endl;
} // it's supposed to output the number of the person and how many pancakes they ate in order.
problem is the output on the screen.Let's say I chose 4,3,2,and 1.
the output:
person4 ate 1 pancake(s)
person4 ate 2 pancake(s)
person4 ate 3 pancake(s)
person4 ate 4 pancake(s)
it just outputs the same person.I tried to fix this problem,but it doesnt seem to work.Why is this,if you have any idea?
I already tried that,and this is how it prints out,
//lets say I chose 1,2,3, and 4 pancakes.
person 1 ate 1 pancakes
person 2 ate 2 pancakes
person 3 ate 3 pancakes
person 4 ate 4 pancakes,,,
at the start of program it asks the user what person 1 and so on ate,
but the output completly ignores this and just prints out in descending order.Hope you know what I mean.
Thank you for your reply.Can you write the code for me?as my dad is in my computer and I'm replying from my phone.Thank you.I would much appreciate it.