Pancake Glutton Code

I want to know... What's bad in this Pancake exercise code? am i on right way of logical thinking or not ?
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
#include <iostream>

using namespace std;
const int SZ = 10;
void Sort_cakes(int *p, int *c)
{
    int temp, temp2;
    for (int i = 0; i < SZ-1; i++)
    {
        if (c[i] > c[i+1])
        {
            temp = c[i+1];
            c[i+1] = c[i];
            c[i] = temp;
            temp2 = p[i+1];
            p[i+1] = p[i];
            p[i] = temp2;
        }
    }
    for (int j = 0; j < SZ-1; j++)
    {
        if (c[j] > c[j+1])
            Sort_cakes(p, c);
    }
}

int main()
{
    int cakes[SZ] = {6, 2, 8, 9, 4, 1, 3, 7, 15, 10};
    int pers[SZ] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    Sort_cakes(pers, cakes);
    for(int i = 0; i < SZ; i++)
    {
        cout << "Person " << pers[i] << " ate - " << cakes[i] << " cakes." << endl;
    }
    return 0;
}
Last edited on
First of all, you should really use struct to hold data and avoid paralel arrays.
Second, what is your sotr function? A recursive bubble sort? Change it at least to selection or insertion sort.

And third, you might want to move to the standard library implementation later, when you will learn everything from this excersize.

Edit:here is an example of program using standard library:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <algorithm>
#include <array>
#include <iomanip>
#include <iostream>

int main()
{
    std::array<std::pair<int, int>, 10> pancakes =
        {{{6, 1}, {2, 2}, {8, 3}, {9,  4}, {4,  5},
          {1, 6}, {3, 7}, {7, 8}, {15, 9}, {10, 10}}};
    std::sort(pancakes.begin(), pancakes.end());
    for(const auto& p: pancakes)
        std::cout << "Person " << std::setw(2) << p.second <<
                     " ate "   << std::setw(2) << p.first  << " cakes" << '\n';
}
Person  6 ate  1 cakes
Person  2 ate  2 cakes
Person  7 ate  3 cakes
Person  5 ate  4 cakes
Person  1 ate  6 cakes
Person  8 ate  7 cakes
Person  3 ate  8 cakes
Person  4 ate  9 cakes
Person 10 ate 10 cakes
Person  9 ate 15 cakes
Last edited on
Thanks for really helpful answer
Topic archived. No new replies allowed.