Modified Pancake Glutton Exercise - Need help

So I modified the pancake glutton problem to output all the persons who at the maximum or minimum just in case there was a tie for the max or min.

To did this, I made a function to count and return the # of max's and a function to count and return the # mins. I then called the functions through main and assigned them to a variable "max" and "min".

Now, I want a very specific format for the output.

IF there is no tie in the max or min (i.e. if max or min=1), then I want the output to be in a single line like below:
max # of pancakes eaten: 20 by person 7

However, if there is a tie in the max or min, I want each person to be on a separate line separated by semi-colon, like below:
min # of pancakes eaten: 2
By person 2;
By person 9;
By person 10;


Here is where the problem arises: the single line format never works. Even if max or min equal 1, it still moves to a new line and adds a semi-colon at the end.

I can't figure out how to fix this.

See my code below:

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <iostream>

using namespace std;

//Prototype functions
int maximum(int pancakes[], int elements);
int minimum(int pancakes[], int elements);
int countmax(int pancakes[], int elements);
int countmin(int pancakes[], int elements);
void sort(int pancakes[], int elements);
int printArray(int pancakes[], int person[], int elements);

int main()
{
    int pancakes[10];
    int min, max, personmax, personmin;

cout << "Person #\tPancakes" << "\n\n"; // Header

    for(int i=0; i<10; i++)
    {
        pancakes[i]=0; //initializes all elements to 0
        cout << "Person " << i+1 << "\t"; //Person number starts at 1
        cin >> pancakes[i];
    }

    max = maximum(pancakes, 10);
    min = minimum(pancakes, 10);
    int cntmax = countmax(pancakes, 10);
    int cntmin = countmin(pancakes, 10);

cout << cout << "\n\nMaximum # of pancakes eaten: " << max << " ";
for(int m=0; m<10; m++) // Finds person number who ate maximum pancakes
{
    if(pancakes[m]==max)
    {
        personmax = m+1;
        if(cntmax==1)
            cout << "By person " << personmax;
        else  cout << "\nBy person " << personmax << "; ";
    }

}

cout << "\n\nMinimum # of pancakes eaten: " << min << " ";
for(int n=0; n<10; n++) // Finds person number who ate minimum pancakes
{
    if(pancakes[n]==min)
    {
        personmin = n+1;
        if(cntmin==1)
             cout << "By person " << personmin;
        else  cout << "\nBy person " << personmin << "; ";
    }

}

cout << "\n\n\n";

sort(pancakes, 10);

cin.get();
}

int maximum(int pancakes[], int elements)
{
    int max = pancakes[0];

    for(int i=0; i<elements; i++)
       if(pancakes[i]>max)
           max = pancakes[i];

    return max;

}

int minimum(int pancakes[], int elements)
{
    int min = maximum(pancakes, 10);
    for(int i=0; i<elements; i++)
       if(pancakes[i]<min)
           min = pancakes[i];

    return min;
}

int countmax(int pancakes[], int elements) //calculates # of persons who ate max
{
int cntmax=0;
int max = (pancakes, 10);

    for(int m=0; m<elements; m++)
    {
        if(pancakes[m]==max)
        cntmax++;
    }
    return cntmax;
}

int countmin(int pancakes[], int elements) // calculates # of person who at min
{
int cntmin=0;
int min = (pancakes, 10);

    for(int m=0; m<elements; m++)
    {
        if(pancakes[m]==min)
        cntmin++;
    }
    return cntmin;
}

void sort(int pancakes[], int elements)
{
    int temp1, temp2;

    int person[10];

    for(int n=0; n<elements; n++)
        person[n] = n;

    for(int pass=0; pass<=elements-1; pass++) // Bubble sort
    {
        for(int i=0; i<elements-1; i++)
            if(pancakes[i]<pancakes[i+1])
            {
                temp1=pancakes[i];
                pancakes[i]= pancakes[i+1];
                pancakes[i+1] = temp1;

                temp2=person[i];
                person[i]=person[i+1];
                person[i+1]=temp2;

            }
    }

    printArray(pancakes, person, 10); // Calls function printArray
}

int printArray(int pancakes[], int person[], int elements)
{
    for(int i=0;i<elements; i++)
    {
        cout << "Person " << person[i]+1 << ": ate " << pancakes[i] << " pancakes" << endl;
    }
}


Below is the most relevant part of the 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
cout << cout << "\n\nMaximum # of pancakes eaten: " << max << " ";
for(int m=0; m<10; m++) // Finds person number who ate maximum pancakes
{
    if(pancakes[m]==max)
    {
        personmax = m+1;
        if(cntmax==1)
            cout << "By person " << personmax;
        else  cout << "\nBy person " << personmax << "; ";
    }

}

cout << "\n\nMinimum # of pancakes eaten: " << min << " ";
for(int n=0; n<10; n++) // Finds person number who ate minimum pancakes
{
    if(pancakes[n]==min)
    {
        personmin = n+1;
        if(cntmin==1)
             cout << "By person " << personmin;
        else  cout << "\nBy person " << personmin << "; ";
    }

}


Im also for some reason getting a ridiculous string of numbers (and one letter x) after I cin the pancakes for person #10. Why is this?

Thank you
line 90 int max = (pancakes, 10); left operand of comma operator has no effect.
You are doing int max = 10;


> Im also for some reason getting a ridiculous string of numbers (and one letter x)
> after I cin the pancakes for person #10. Why is this?
Because in line 32 you've got cout << cout
Last edited on
Oh wow, these were really stupid mistakes :(

Thanks bro, it works perfectly now.
Topic archived. No new replies allowed.