Pancake Glutton

The question http://www.cplusplus.com/forum/articles/12974/ pancake glutton,
I'm currently in the 4 star modification

this is what I have got it solves the problem I just want to improve it:

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
    using namespace std;
     

    int persons_pancakes_eaten[10];
    int persons_pancakes_copy[10];
    int persons_ordered_by_pancakes[10];
    int greatest_val = 0;
    const int GREATEST = 0;

    for (int i = 0; i < 10; ++i) {
        cout << "Please enter person " << i + 1 << " pancakes eaten: ";

        cin >> persons_pancakes_eaten[i];
        persons_pancakes_copy[i] = persons_pancakes_eaten[i];
    }



    for (int CURRENT_GREATEST_PERSON = 0; CURRENT_GREATEST_PERSON < 10; CURRENT_GREATEST_PERSON++) {
        greatest_val = 0;
        for (int i = 0; i < 10; ++i) {
            
            if (persons_pancakes_eaten[i] == -1)continue;

            if (persons_pancakes_eaten[i] > greatest_val) {
                greatest_val = persons_pancakes_eaten[i];
                persons_ordered_by_pancakes[CURRENT_GREATEST_PERSON] = i;
            }

        }
        int LAST_GREATEST = persons_ordered_by_pancakes[CURRENT_GREATEST_PERSON];
        persons_pancakes_eaten[LAST_GREATEST] = -1;
    }

    for (int i = 0; i < 10; ++i) {
        cout << "Person " << persons_ordered_by_pancakes[i]+1 << " ate " << persons_pancakes_copy[persons_ordered_by_pancakes[i]]<<'\n';
    }


how I can improve this code, for example how should I name more appropriately variables, and how I can remove the second copy of the array I'm using there?
and how I can avoid the -1 for not valid array cell?
hopefully this is readable code and thanks for the help.
I'm a beginner.
You seem to be doing two things at once. The code would be much clearer if you did one loop to find the person who ate the most (and fewest) pancakes, and then did a separate loop to sort by the number of pancakes eaten.
Thanks, I think I see what you're saying... ill go to improve it right now...
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
    int Persons_Data[10];
    int Persons_Data_Copy[10];
    for (int i = 0; i < 10; ++i) {
        cout << "Please enter person " << i + 1 << " number of pancakes eaten: ";
        cin >> Persons_Data[i];
        Persons_Data_Copy[i]=Persons_Data[i];
    }

    int Greatest_Pancake_Person = 0;
    int Fewest_Pancake_Person = INT16_MAX;
    for (int i = 0; i < 10; ++i) {
        if (Persons_Data[i] > Greatest_Pancake_Person) {
            Greatest_Pancake_Person = Persons_Data[i];
        }
        if (Persons_Data[i] < Fewest_Pancake_Person) {
            Fewest_Pancake_Person = Persons_Data[i];
        }
    }

    // Sorting
    for (int i = 0; i < 10; ++i) {
        int Greatest = 0;
        int Greatest_Pancake_Number = 0;

        for (int j = i; j < 10; ++j) {
            if (Persons_Data[j] > Greatest_Pancake_Number) {
                Greatest = j;
                Greatest_Pancake_Number = Persons_Data[j];
            }
        }

        int temp = Persons_Data[i];
        Persons_Data[i] = Persons_Data[Greatest];
        Persons_Data[Greatest] = temp;
    }
    
    // Printing
    for (int i = 0; i < 10; ++i) {
        for (int j = 0; j < 10; ++j) {
            if (Persons_Data[i] == Persons_Data_Copy[j]) {
                cout << "Person number " << j+1 << " ate " << Persons_Data[i] << " pancakes\n";
                break;
            }
        }
    }


OK!, this is my code, buy still I have a copy of the array, can I get rid of it? is this code better?
Last edited on
Topic archived. No new replies allowed.