What should be used to determine which index of an array is greater than the rest?

In this exercise, the goal is to allow the user to input the number of pancakes eaten by 10 different people, and analyze which person ate the most.

My code was able to accomplish the first part, as for analyzing who ate the most; after 10 inputs it outputs any random person ate the most number of pancakes regardless of the fact who really did, although it does input correctly how many pancakes person 1 ate. However, it should have said, person 5 as for this I inputted a value of 10, which was greatest among others.

Please identify what's wrong with my code, and suggest me how I can fix it. And I want to accomplish result without using any algorithm library that automatically does it for me.

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
#include <iostream>
#include <limits>
using namespace std;

int main()
{
    int person=1, p[10], x;

    for (x=0; x< 10, person <11; x++, person++) {

        cout << "Input the # of pancakes eaten by person " << person << ": " << endl;
        cin >> p[x];} // store the inputted values of number of pancakes eaten by each person

        if (p[0] > (p[1] && p[2] && p[3] && p[4] && p[5] && p[6] && p[7] && p[8] && p[9])) {
            cout << "Person 1 ate the most pancakes." << endl << "Person 1 ate: " << p[0] << endl;
        }
        else if (p[1] > (p[0] && p[2] && p[3] && p[4] && p[5] && p[6] && p[7] && p[8] && p[9])) {
            cout << "Person 2 ate the most pancakes." << endl << "Person 2 ate: " << p[1] << endl;
        }
        else if (p[2] > (p[0] && p[1] && p[3] && p[4] && p[5] && p[6] && p[7] && p[8] && p[9])) {
            cout << "Person 3 ate the most pancakes." << endl << "Person 3 ate: " << p[2] << endl;
        }
        else if (p[3] > (p[0] && p[1] && p[2] && p[4] && p[5] && p[6] && p[7] && p[8] && p[9])) {
            cout << "Person 4 ate the most pancakes." << endl << "Person 4 ate: " << p[3] << endl;
        }
        else if (p[4] > (p[0] && p[1] && p[2] && p[3] && p[5] && p[6] && p[7] && p[8] && p[9])) {
            cout << "Person 5 ate the most pancakes." << endl << "Person 5 ate: " << p[4] << endl;
        }
        else if (p[5] > (p[0] && p[1] && p[2] && p[3] && p[4] && p[6] && p[7] && p[8] && p[9])) {
            cout << "Person 6 ate the most pancakes." << endl << "Person 6 ate: " << p[5] << endl;
        }
        else if (p[6] > (p[0] && p[1] && p[2] && p[3] && p[4] && p[5] && p[7] && p[8] && p[9])) {
            cout << "Person 7 ate the most pancakes." << endl << "Person 7 ate: " << p[6] << endl;
        }
        else if (p[7] > (p[0] && p[1] && p[2] && p[3] && p[4] && p[5] && p[6] && p[8] && p[9])) {
            cout << "Person 8 ate the most pancakes." << endl << "Person 8 ate: " << p[7] << endl;
        }
        else if (p[8] > (p[0] && p[1] && p[2] && p[3] && p[4] && p[5] && p[6] && p[7] && p[9])) {
            cout << "Person 9 ate the most pancakes." << endl << "Person 9 ate: " << p[8] << endl;
        }
        else if (p[9] > (p[0] && p[1] && p[2] && p[3] && p[4] && p[5] && p[6] && p[7] && p[8])) {
            cout << "Person 10 ate the most pancakes." << endl << "Person 10 ate: " << p[9] << endl;
        }

    return 0;
}
Last edited on
closed account (48T7M4Gy)
work out which person ate the most then you could do it as the number eaten is recorded

Last edited on
closed account (48T7M4Gy)
On its own because that is always true. Also the combinations aren't very quickly.
Last edited on
My code is now getting the input part and the lowest and greatest part right. The only thing is that it is not telling exactly which person ate the most or least.

Kindly help me how I can have the program identify the person who ate most and least.

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
#include <iostream>
#include <limits>
#include <cstdlib>
using namespace std;

int main()
{
    int person=1, p[10], x;
    int smallest =0, largest = 0;


    for (x=0; x< 10, person <11; x++) {
        cout << "Input the # of pancakes eaten by person " << person++ << ": " << endl;
        cin >> p[x];
    }

    smallest = p[0];
    largest = p[0];

    for (x=1; x<10; x++) {

        if (p[x] < smallest)
            smallest = p[x];

        if (p[x] > largest)
            largest = p[x];
    }

    cout << "The person who ate the most pancakes is person " << person << " who ate: " << largest << endl;
    cout << "The person who ate the least # of pancakes is person " << person << " who ate: " << smallest << endl;
    return 0;
}
Last edited on
closed account (48T7M4Gy)
if pancakes [1] i > maxEaten, therein lies the answer.
Last edited on
you can't just have p[??] on its own because that is always true.

When p[??] is 0, it will evaluate to false.

Just keep track of the index of the largest number encountered so far. If you encounter a number higher than the one you're storing, update that and continue until you've looped through the entire thing.

It looks like you might be printing how MUCH that person ate instead of which person it was. Store the index instead of the value at that index. If you want to see what value is at that index, just index into the array.

1
2
3
4
5
6
7
8
9
10
int index_of_smallest = 0;
int index_of_largest = 0;

for (int i = 1; i < 10; i++)
{
  if (p[i] > p[index_of_largest])
    ...
  if (p[i] < p[index_of_smallest])
    ...
}
closed account (48T7M4Gy)
It is necessary to store the minimum no.
Last edited on
I would tend to disagree. If you know the index of the most eaten, you can obtain that value simply by indexing into that array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  int eaten[10] = { 2, 6, 7, 1, 5, 12, 8, 5, 7, 13 };

  int index_of_smallest = 0;
  int index_of_largest = 0;

  for (int i = 0; i < 10; i++)
  {
    if (eaten[i] > eaten[index_of_largest])
      index_of_largest = i;
    if (eaten[i] < eaten[index_of_smallest])
      index_of_smallest = i;
  }

  std::cout << "Index of smallest: " << index_of_smallest << ", smallest amount eaten: " << eaten[index_of_smallest] << std::endl;
  std::cout << "Index of largest : " << index_of_largest << ", largest amount eaten: " << eaten[index_of_largest] << std::endl;


produces:
Index of smallest: 3, smallest amount eaten: 1
Index of largest : 9, largest amount eaten: 13
Press any key to continue . . .


You shouldn't need to also store the largest/smallest amount eaten if you're storing the index at which that value is stored, that's just wasted space. Just index into the array to retrieve the value (which I'm doing here for the comparisons).

There's no way to carefully select the max/min eaten, because we have no way of knowing what they are prior to looping through the array. How would you propose carefully selecting them? You can't make them arbitrarily large or small - they must be some value already in the array. Making them both initially equal to the first item in the array is fine, because any values smaller than the initial value will be found and replace the index of the smallest being stored, and likewise for the index of the largest.
closed account (48T7M4Gy)
You can because it hasn't the progressive option.
Last edited on
is a bit of a giveaway to what's actually going on here with the key word being 'progressively'.

Yes but that is not what the code does that OP most recently gave.

In the method that has been coded above, the array is filled completely prior to finding the min/max values. Yes, the original description has them checked as they are entered, but that isn't what the OP gave as their most recent code, and my comments are reflective on that:

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
#include <iostream>
#include <limits>
#include <cstdlib>
using namespace std;

int main()
{
    int person=1, p[10], x;
    int smallest =0, largest = 0;


    for (x=0; x< 10, person <11; x++) {
        cout << "Input the # of pancakes eaten by person " << person++ << ": " << endl;
        cin >> p[x];
    }

    smallest = p[0];
    largest = p[0];

    for (x=1; x<10; x++) {

        if (p[x] < smallest)
            smallest = p[x];

        if (p[x] > largest)
            largest = p[x];
    }

    cout << "The person who ate the most pancakes is person " << person << " who ate: " << largest << endl;
    cout << "The person who ate the least # of pancakes is person " << person << " who ate: " << smallest << endl;
    return 0;
}


I would tend to disagree
should really be
I disagree


I don't see any particular advantage to storing both the index and the value, nor do I see an issue with assigning the largest/smallest known index to 0 to start with.
Which careful values would you suggest using to initialize with?
If you initialize with a value larger than anything in the array, you'll never find the largest value. Likewise for the smallest. Your statement is too vague here on this.
closed account (48T7M4Gy)
Please add thread.
Last edited on
I provided code to the thread that addressed the issue the OP was having, as well as an explanation of the code I supplied. Not everything I wrote was a critique.

Your use of the word petty implies you find my contribution useless, which is fine, but critiques of other contributions can provide meaningful input to the thread. I've seldom seen a problem done that does not garner input/feedback from other developers.
closed account (48T7M4Gy)
Address the suggestions?
Last edited on
> critiques of other contributions can provide meaningful input to the thread.

Yes.

This site does not proscribe answers which offer alternatives, point out defects or suggest improvements to contributions from members other than the original poster. The quality of the forum is greatly enhanced because of this.
Kemort, you changed your posts from what were previously rather rude and somewhat degrading. It's appreciated, but I hope that we can have a discussion about an implementation next time without it getting defensive. Not sure why JLBorges got reported here, but I'm sure the admins just ignore those reports now.

I did address suggestions, as well as provide alternative suggestions, and reasoning/explanations for them.

In any case, it is up to the OP now to decide how they wish to implement the solution. I see any performance difference being negligible in this situation, so I encourage the OP to use whatever implementation makes the most sense to them. If there is a specific implementation specified on their assignment, use that. On these forums, you're likely to get a variety of different solutions/opinions, some of which may or may not be at your level of comprehension, which is to be expected. JLBorges can give complex solutions, for example, but they encourage me to deepen my understanding of the language.

And sometimes we will disagree as to which implementation is better. This is not a bad thing, necessarily. It fosters discussion, critical thinking, and can be a learning process for all.
Hehe it finally works!!!

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

int main()
{
    int person=1, p[10], x;
    int smallest =0, largest = 0;
    int max_person = 1;
    int min_person = 1;


    for (x=0; x< 10, person <=10; x++, person++) {
        cout << "Input the # of pancakes eaten by person " << person << ": " << endl;
        cin >> p[x];
    }

    smallest = p[0];
    largest = p[0];

    for (x=1; x<10; x++) {
        if (p[x] < smallest){
            smallest = p[x];
            min_person++;}
        if (p[x] > largest){
            largest = p[x];
            max_person++;}

    }

    cout << "The person who ate the most pancakes is person " << max_person << " who ate: " << largest << endl;
    cout << "The person who ate the least # of pancakes is person " << min_person << " who ate: " << smallest << endl;
    return 0;
}
I made my this code work too, although it has one shortcoming which is that if I add the less than part of the code it will be quite long, which is why the for loop is a better solution.
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
#include <iostream>
#include <limits>
using namespace std;

int main()
{
    int person=1, p[10], x;

    for (x=0; x< 10, person <11; x++, person++) {

        cout << "Input the # of pancakes eaten by person " << person << ": " << endl;
        cin >> p[x];} // store the inputted values of number of pancakes eaten by each person

        if (p[0] > p[1] && p[0] > p[2] && p[0] > p[3] && p[0] > p[4] && p[0] > p[5] && p[0] > p[6] && p[0] > p[7] && p[0] > p[8] && p[0] > p[9]) {
            cout << "Person 1 ate the most pancakes." << endl << "Person 1 ate: " << p[0] << endl;
        }
        else if (p[1] > p[0] && p[1] > p[2] && p[1] > p[3] && p[1] > p[4] && p[1] > p[5] && p[1] > p[6] && p[1] > p[7] && p[1] > p[8] && p[1] > p[9]) {
            cout << "Person 2 ate the most pancakes." << endl << "Person 2 ate: " << p[1] << endl;
        }
        else if (p[2] > p[0] && p[2] > p[1] && p[2] > p[3] && p[2] > p[4] && p[2] > p[5] && p[2] > p[6] && p[2] > p[7] && p[2] > p[8] && p[2] > p[9]) {
            cout << "Person 3 ate the most pancakes." << endl << "Person 3 ate: " << p[2] << endl;
        }
        else if (p[3] > p[0] && p[3] > p[1] && p[3] > p[2] && p[3] > p[4] && p[3] > p[5] && p[3] > p[6] && p[3] > p[7] && p[3] > p[8] && p[3] > p[9]) {
            cout << "Person 4 ate the most pancakes." << endl << "Person 4 ate: " << p[3] << endl;
        }
        else if (p[4] > p[0] && p[4] > p[1] && p[4] > p[2] && p[4] > p[3] && p[4] > p[5] && p[4] > p[6] && p[4] > p[7] && p[4] > p[8] && p[4] > p[9]) {
            cout << "Person 5 ate the most pancakes." << endl << "Person 5 ate: " << p[4] << endl;
        }
        else if (p[5] > p[0] && p[5] > p[1] && p[5] > p[2] && p[5] > p[3] && p[5] > p[4] && p[5] > p[6] && p[5] > p[7] && p[5] > p[8] && p[5] > p[9]) {
            cout << "Person 6 ate the most pancakes." << endl << "Person 6 ate: " << p[5] << endl;
        }
        else if (p[6] > p[0] && p[6] > p[1] && p[6] > p[2] && p[6] > p[3] && p[6] > p[4] && p[6] > p[5] && p[6] > p[7] && p[6] > p[8] && p[6] > p[9]) {
            cout << "Person 7 ate the most pancakes." << endl << "Person 7 ate: " << p[6] << endl;
        }
        else if (p[7] > p[0] && p[7] > p[1] && p[7] > p[2] && p[7] > p[3] && p[7] > p[4] && p[7] > p[5] && p[7] > p[6] && p[7] > p[8] && p[7] > p[9]) {
            cout << "Person 8 ate the most pancakes." << endl << "Person 8 ate: " << p[7] << endl;
        }
        else if (p[8] > p[0] && p[8] > p[1] && p[8] > p[2] && p[8] > p[3] && p[8] > p[4] && p[8] > p[5] && p[8] > p[6] && p[8] > p[7] && p[8] > p[9]) {
            cout << "Person 9 ate the most pancakes." << endl << "Person 9 ate: " << p[8] << endl;
        }
        else if (p[9] > p[0] && p[9] > p[1] && p[9] > p[2] && p[9] > p[3] && p[9] > p[4] && p[9] > p[5] && p[9] > p[6] && p[9] > p[7] && p[9] > p[8]) {
            cout << "Person 10 ate the most pancakes." << endl << "Person 10 ate: " << p[9] << endl;
        }

    return 0;
}
Hehe it finally works!!!

No it doesn't. Consider this output:

Input the # of pancakes eaten by person 1:
4 5 1 2 3 6 7 8 9 10
Input the # of pancakes eaten by person 2:
Input the # of pancakes eaten by person 3:
Input the # of pancakes eaten by person 4:
Input the # of pancakes eaten by person 5:
Input the # of pancakes eaten by person 6:
Input the # of pancakes eaten by person 7:
Input the # of pancakes eaten by person 8:
Input the # of pancakes eaten by person 9:
Input the # of pancakes eaten by person 10:
The person who ate the most pancakes is person 7 who ate: 10
The person who ate the least # of pancakes is person 2 who ate: 1


Person 7 ate 7 pancakes which is not the greatest amount.
Person 2 at 5 pancakes, which is not the least amount.
Topic archived. No new replies allowed.