Bubble Sort issue

Jun 5, 2011 at 5:54am
I'm doing the 'Pancake Glutton' exercise listed here:
http://www.cplusplus.com/forum/articles/12974/

Actually, I'm working on the ★★★★ modify, trying to sort by number of pancakes eaten. My loops to populate the two arrays work. My loop to display the data works. The issue is with the sort. After the data input, the program just stops. Here is my sort:
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
    //Loop to sort arrays
    int flag = 1;
    int swap, swap2;
    int counter;

    while (flag = 1)
    {

        flag = 0;
        for (counter = 0; counter < 10; counter++)
        {

            if (numPancakes[counter] < numPancakes[counter + 1])
            {
                swap = numPancakes[counter];
                numPancakes[counter] = numPancakes[counter + 1];
                numPancakes[counter + 1] = swap;

                swap2 = gluttons[counter];
                gluttons[counter] = gluttons[counter + 1];
                gluttons[counter + 1] = swap2;

                flag = 1;
            }
        }
    }
Jun 5, 2011 at 6:04am
Two things I've noticed:

1. while (flag = 1) should have a double equals sign. With the code as is, it will do the while loop over and over.

2. The question says there are 10 people, yet you do numPancakes[counter + 1] when the counter is at 9. This is out of bounds of the array (assuming numPancakes was declared as int numPancakes[10];).
Jun 5, 2011 at 6:29am
shacktar,
Thanks for your reply. Changing the while loop's condition to == worked, to a point.

Now, the whole program only works if I use numbers 1 through 10. It actually puts them in order, well if I enter them in reverse order. If I enter them in a random order, it will almost get them in the right order, but not quite. if I use numbers over 10, the output is still only numbers 1 to 10.

Here's the entire code:
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    //Declare array
    int gluttons[10];
    int numPancakes[9];
    int tmpPancakes;

    //Populate gluttons array
    for (int counter = 0; counter < 10; counter++)
    {
        gluttons[counter] = counter + 1;
    }

    //Populate numPancakes array
    for (int counter = 0; counter < 10; counter++)
    {
        cout << "Enter the number of pancakes eaten by glutton number " << counter + 1<< ":" << endl;
        cin >> numPancakes[counter];
    }
    //Loop to sort arrays
    int flag = 1;
    int swap, swap2;
    int counter;

    while (flag == 1)
    {

        flag = 0;
        for (counter = 0; counter < 10; counter++)
        {

            if (numPancakes[counter] < numPancakes[counter + 1])
            {
                swap = numPancakes[counter];
                numPancakes[counter] = numPancakes[counter + 1];
                numPancakes[counter + 1] = swap;

                swap2 = gluttons[counter];
                gluttons[counter] = gluttons[counter + 1];
                gluttons[counter + 1] = swap2;

                flag = 1;
            }
        }
    }

    //Display results
    cout << setw(10) << "Glutton:" << "   " << setw(10) << "#pancakes" << endl;
    for (int counter = 0; counter < 10; counter++)
    {
        cout << setw(6) << counter + 1 << setw(10) << gluttons[counter] << endl;
    }

    return 0;

}
Jun 5, 2011 at 6:31am
shacktar,
Also, when I change the loop conditional to 9, it only outputs 1 to 9. The tenth array position is not entered or output. Like this, it works.
Jun 5, 2011 at 7:58am
It is the loop in the sort that needs to be < 9. So the loop on line 33.

The output is only ever going to print the numbers 1-10 (when you've corrected the error on line 33) regardless of what you enter, because you only print the gluttons array, not the pancakes array.
Jun 5, 2011 at 8:38am
Thanks, kev82. That got it.
Topic archived. No new replies allowed.