What I don't understand is how to find the person who ate the minimum amount of pancakes. Every time I run the code and input the number of pancakes in the order: 1,2,3...10, person 10 should have eaten the most pancakes, while person 1 should have eaten the least. But I think the code used to find the person with the least number of pancakes eaten is wrong, as they return the 9th person as the one who ate the least pancakes.
#include <iostream>
usingnamespace std;
int main ()
{
int Pancakes[10];
int person;
int max_person;
int min_person;
int max;
int min;
for (person=1;person<=10;person++)
{
cout << "Enter the number of pancakes person " << person << " ate.\n";
cin >> Pancakes[person];
}
for (int z=1; z<=10; z++)
{
for (person=1; person<=10; person++)
{
if (Pancakes[person] > Pancakes[z])
{
max = Pancakes[person];
max_person = person;
}
elseif (Pancakes[z] > Pancakes[person])
{
min = Pancakes[person];
min_person = person;
}
}
}
cout << "The person who ate the most pancake is person " << max_person << endl;
cout << "The person who ate the least pancakes is person " << min_person << endl;
return 0;
}
You should invest into a lambda for compare instead of that nested loop. Also there is this really neat function in the library algorithm called min_element and max_element.
1 2 3 4
#include <algorithm>
min = std::min_element( Pancakes , Pancakes + 10 );
max = std::max_element( Pancakes , Pancakes + 10 );
This will tell you the min and max pancakes ate. Then you can see who ate that many. By using the iterator or You can pair the "Pancakes" with a person. You can also just sort the array and then the first element will be the min or max based on how you sort and the last element will be the other one.
If you want to do it the hard way like you are then I guess you could do something like
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
int main()
{
int pancakes[ 10 ]{ 10 , 2, 8, 4, 6 , 5, 7, 3 , 9 , 1 } , //c++11 way to initailize arrays you may need to fix if you do not have c++11 enabled.
min = pancakes[ 0 ] , max = pancakes[ 0 ];
for( int i = 0; i < 10; ++i ) //This is the standard way to write for loops...
{
if( min > pancakes [ i ] ) min = pancakes[ i ];
if( max < pancakes[ i ] ) max = pancakes[ i ];
}
std::cout << min << " " << max << std::endl;
}
*Edit
A lambda is basically what I did to compare. You start at the lhs ( left hand side) and compare to the right hand side of it.
I've taken your advice to add in the function from the library algorithm but when I run the code all it returns me is values of memory addresses. I don't know what went wrong but here was my code. (I think I'm using min_element and max_element wrongly here)
#include <iostream>
#include <algorithm>
usingnamespace std;
int main ()
{
int Pancakes[10];
int person;
for (person=1;person<=10;person++)
{
cout << "Enter the number of pancakes person " << person << " ate.\n";
cin >> Pancakes[person];
}
cout << "The person who ate the most pancake is person " << max_element(Pancakes, Pancakes+10) << endl;
cout << "The person who ate the least pancakes is person " << min_element(Pancakes, Pancakes+10) << endl;
return 0;
}
Also, I'd like to know what does the +10 in the function do? As in why would there be a +10 to my Pancakes.
Sorry for bothering you, and thanks!
*Edit: Oops I realise that the function for max_element and min_element actually returns to me an array instead of a value. Thus had to dereference it in order to find the actual person who ate the most pancakes.
Please tell me if my understanding of the max_element and min_element function is correct. Thanks!
Also these will only tell you the min/max pancakes ate not who ate them. If you want to see who ate the minimum/max you will have to use the iterator returned ( that is why you have to dereference ) then check how far the iterator is from the beginning. std::distance( Pancakes , std::min_element( Pancakes , Pancakes + 10 ) );
The reason for the + 10 is because "arrays" are just pointers so there for they are basically iterators so the first iterator would be the array ( beginning) and the last iterator would be the last element and there are 10 elements so that would be array + 10. If it was a vector they are not made up of pointers so you would have to use the vector built in iterator functions such as vector.begin() , vector.end().
If you don't want to go through the work of having to do
1 2
std::cout << "Person who ate least " << std::distance( Pancakes , std::min_element( Pancakes , Pancakes + 10 ) ) + 1 << std::endl; //+1 because arrays start at index 0 and you want the first person to be person 1 not person 0.
std::cout << "Person who ate most " << std::distance( Pancakes , std::max_element( Pancakes , Pancakes + 10 ) ) + 1 << std::endl;
Then you can just use the simplified code I suggested earlier.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
int main()
{
int pancakes[ 10 ]{ 10 , 2, 8, 4, 6 , 5, 7, 3 , 9 , 1 } , //c++11 way to initailize arrays you may need to fix if you do not have c++11 enabled.
min = pancakes[ 0 ] , max = pancakes[ 0 ];
for( int i = 0; i < 10; ++i ) //This is the standard way to write for loops...
{
if( min > pancakes [ i ] ) min = pancakes[ i ]; min = i;
if( max < pancakes[ i ] ) max = pancakes[ i ]; max = i;
}
++min;
++max;
//Once again arrays start at index 0 and we want first person to be person 1 not person 0
std::cout << min << " " << max << std::endl;
}
Then modify it slightly. Instead of keeping track of the min and max elements for say we can just keep track of the person.'