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.
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.
#include <iostream>
#include <limits>
#include <cstdlib>
usingnamespace 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;
}
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])
...
}
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.
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:
#include <iostream>
#include <limits>
#include <cstdlib>
usingnamespace 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.
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.
> 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.
#include <iostream>
usingnamespace 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.
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.