Hi all, I'm new here and was just doing the Pancake Glutton beginner exercise. Anyway, here's the code for my sorting algorithm (it's a bubble sort, I didn't want to copy anything that I didn't understand). It's supposed to rearrange the people in descending order along with the person number within the array itself (list[NUMBERPEOPLE][2]), so that I can simply do a for (x = 0; x<NUMBERPEOPLE; x++) loop after this in main() to output the ordered list.
Problem is, the list is not getting rearranged. The cout at the end just outputs a string of 10 sets of the data in list[0][x], while I expected it to change to reflect the rearrangement of the array.
So is there something wrong with my loops or is it a problem with my swap function?
void sortlist()
{
bool done = false;
while (!done)
for (int x = 0; x < NUMBERPEOPLE /*I used a #define so I could edit the number easily for testing*/; x++)
{
if (list[x][1] > list[x+1][1])//compares two adjacent values, from the bottom
{
int temp[2];
temp[0] = list[x][0];
temp[1] = list[x][1];
list [x][0] = list[x+1][0];
list [x][1] = list[x+1][1];
list[x+1][0] = temp[0];
list[x+1][1] = temp[1];
done = false;
}
else
{
done = true;
}
cout << list[0][0] << list[0][1];//testing to see if rearrangement has occurred
}
}
Oh, I just read to the end of the thread on the beginner exercises page, feel like an idiot now. In that case, I guess my question would be, is there any way to achieve the same function without using classes/structs?