arrays on Pancake Glutton

http://www.cplusplus.com/forum/articles/12974/
Thats the Exercise I am trying to do. #4 on the list "Pancake Glutton"
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
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{   // [0]='14',[1]='9',[2]='12',[3]='19',[4]='5',[5]='3',[6]='31',[7]='16',[8]='25',[9]='1'//
    int person [] = {1,2,3,4,5,6,7,8,9,10};
    int p = 1;
    int pNum = 1;
    int persson = 0;
    int anotherP = 0;
    int max = 666;
    int loop = 1;
    int a = 0;
    int b = 1;

    while (p <= 10)
    {
        cout << "Enter the number of pancakes eaten by Person "<< p <<": ";
        cin >> person [persson];
        persson++;
        p++;
        system("cls");

    }

system("Pause");
system("cls");

    if (person [10] >= 0)
        while(pNum <= 10)
        {
            cout << "Person "<< pNum <<" is: " << person[anotherP] << endl;
            pNum++;
            anotherP++;
        }

system("Pause");
        //Look into the input buffer! makes cin.ignore work bad!
        //cin.ignore();
system("cls");



    do
    {
        if (person [a] >= person [b])
        {
            person [a] = max;
            a++;
            b++;
        }

        else if (person [b] >= person [a])
        {
            person [b] = max;
            a++;
            b++;
        }

        loop++;
    } while(loop <= 8);



    cout << "The number of most eaten pancakes is: " << max << endl;
    cout << person[a] << " " << person[b];



    return 0;
}


Now I know this wont work correctly But the main issue i am having is setting a variable to the max integer. Whenever the program is done It always comes out as the constant instead of being one of the two arrays i try to set it to...

Thanks in advance if you guys can help me out!


EDIT: Sorry if its a bit messy and stuff on the amount of variables. I will clean it out once i can get the basics working
Last edited on
On line 13, you check person[10], which is out of bounds of your array.

You should probably start cleaning it up now. The number of variables (not even worrying about scoping problems) and the general uselessness of their names makes it hard to follow the program. You also use random constants throughout the program instead of simply having the size of the array in a constant and using that.
Okay so i cleaned up all my variables like you said sir.

But it still isnt working properly!! /cry heres my 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
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    //THESE ARE MY RANDOM PANCAKE NUMBERS
    // [0]='14',[1]='9',[2]='12',[3]='19',[4]='5',[5]='3',[6]='31',[7]='16',[8]='25',[9]='1'//

    int person [10] = {1,2,3,4,5,6,7,8,9,10};
    int maxNum = 0;
    int a = 0;
    int b;

    while (a <= 9)
    {
        cout << "Enter the number of pancakes eaten by Person "<< person [a] <<": ";
        cin >> person [a];
        a++;
        system("cls");
        //Try getting a better method than system("cls");
        //cout << string(50, '\n');
    }

system("Pause");
system("cls");

    a = 1;

    if (person [10] >= 0)
        while(a <= 10)
        {
            cout << "Person "<< a <<" is: " << person [a-1] << endl;
            a+=1;
        }

system("Pause");
        //Look into the input buffer! makes cin.ignore work bad!
        //cin.ignore();
system("cls");

    b = 0;
    a = 1;

    if (person [b] > person [a])
    {
        person [b] = maxNum;
    }

    else if (person [b] < person [a])
    {
        person [a] = maxNum;
    }

    cout << maxNum;

    return 0;
}


If i enter my random pancake numbers then the maxNum should be 14 but it still insists on displaying the constant of 0..

EDIT: Oh okay.. so i seem to have a problem with element 0 even displaying.. if cout that it sais 0. however if i cout person[1] it correctly sais 9. I have no clue why this is.

EDIT: Lol I got it! i was declaring what person 0 was instead of the other way around!! Thanks for the help though Zhuge... i should really get accustomed to cleaning up my crap. Is it clean enough now or is there more i could do?


Last edited on
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
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    //THESE ARE MY RANDOM PANCAKE NUMBERS
    // [0]='14',[1]='9',[2]='12',[3]='19',[4]='5',[5]='3',[6]='31',[7]='16',[8]='25',[9]='1'//

    int person [10] = {1,2,3,4,5,6,7,8,9,10};
    int maxNum = 0;
    int a = 0;
    int b = 1;

    while (a <= 9)
    {
        cout << "Enter the number of pancakes eaten by Person "<< person [a] <<": ";
        cin >> person [a];
        a++;
        system("cls");
        //Try getting a better method than system("cls");
        //cout << string(50, '\n');
    }

system("Pause");
system("cls");

    a = 1;

    if (person [10] >= 0)
        while(a <= 10)
        {
            cout << "Person "<< a <<" is: " << person [a-1] << endl;
            a+=1;
        }

system("Pause");
        //Look into the input buffer! makes cin.ignore work bad!
        //cin.ignore();
system("cls");


// [0]='14',[1]='9',[2]='12',[3]='19',[4]='5',[5]='3',[6]='31',[7]='16',[8]='25',[9]='1'//
    a = 0;
    b = 1;
    do
    {
        if (person [a] > person [b])
            {
                maxNum = person [a];
                b++;
            }

            else if (person [b] > person [a])
            {
                maxNum = person [b];
                a++;
            }

            else if (person [a] == person [b])
                a++;

    } while (a <= 9);


    cout << "The most pies eaten were: " << maxNum;

    return 0;
}


Finished! I dont think there should be any bugs i checked it a few times
1
2
//line 31
if (person [10] >= 0) //out of bounds 

Check the algorithm for finding the maximum. What if b exceeded 9? (you could improve your algorithm too, just checking every number once)
Although you can't eat a negative number, you should not initialise your maxNum with 0, but with the first element (what if it doesn't exist an absolute minimum?)
Wait what do you mean? Ive tested the program with more than just 1- 10... Infact my original set of testing numbers were the commented out RANDOM PANCAKE NUMBERS they arent 1 -10
Arrays goes from 0 to size-1. Anything besides that is out of bound, and could cause unexpected behaviour (like crash).

lines 47-64 (maximum algorithm):
Suppose that the maximum number is in the first position (person[0]).
Then
1
2
3
4
5
if (person [a] > person [b])
  {
     maxNum = person [a];
     b++;
  }
is executed every time. b it will keep incrementing till reach 10, then you will be working out of bounds

About improving your algorithm:
1
2
3
4
5
if( person[K] > maxNum )
  maxNum = person[K];
//or
if( person[K] > person[maxIndex] )
  maxIndex = K;
This way you do n comparisons, against 3*n
Hmm I think you are referring to something i have been wondering about my self. But if you try running the code it actually works, and stops after 10 itterations (or 9 whatever). I have been curious as to why it works also and my best guess is that it just stop because it knows that there cant be more than 10...

Try the code and see for yourself. I will fit what you added in as well and play around with it. Thanks!
Yeah i do like your suggestion. even though mine worked I dont like that i dont know why it worked.. this seems to do exactly what i tell it to.. well anyway here is my new solution And it got rid of another variable lol Thank you!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    do
    {
        if (maxNum > person [b])
            b++;

        else if (maxNum <= person [b])
        {
            maxNum = person [b];
            b++;
        }



    } while (b <= 9);
Topic archived. No new replies allowed.