I am doing the beginner exercises, and I am sorta stuck on Pancake Glutton. I have done half of part 1 and finished part 3. Right now this code is not finished because I wanted to know if there was an easier way then what I am doing at the moment. http://www.cplusplus.com/forum/articles/12974/
#include <iostream>
usingnamespace std;
int main ()
{
int x;
int PancakeEater[10];
cout << "Please Enter How Many Pancakes Each Person Ate\n";
for (int x =0; x < 4; x++)
{
cout << "Person " << x+1 << ": \n";
cin >> PancakeEater[x];
cout << endl;
}
if( PancakeEater[0] > PancakeEater[1])
{
cout << "Person 1 At The Most Pancakes " << PancakeEater[0] << endl;
}
elseif(PancakeEater[1]>PancakeEater[2])
{
cout << "Person 2 At The Most Pancakes " << PancakeEater[1] << endl;
}
elseif(PancakeEater[2] > PancakeEater[3])
{
cout << PancakeEater[2] << endl;
}
elseif (PancakeEater[3]>PancakeEater[4])
{
cout << PancakeEater [3];
}
elseif (PancakeEater[4]>PancakeEater[0])
{
cout<< PancakeEater[4];
}
for (x = 0; x < 4; x++)
{
cout << "Person " << x+1 << " ate: ";
cout << PancakeEater[x] <<endl;
}
}
I DO REALIZE that if PancakeEater[0] is greater than PancakeEater[1] but is still not the biggest number it will print and ignore everything else. I just wanted to know if I had to keep doing what I am doing but do this instead
if(PancakeEater[0] > PancakeEater[1] and PancakeEater[0] > PancakeEater[2] and etc. etc.)
There are a lot of cases to if/else through to find out who ate the most! Better here would be to just search the array for the highest number(s). Something like:
1 2 3 4 5 6 7 8 9
int most = 0, mostgluttonist = 0;
for(x = 0;x < 10;++x;)
{
if(PancakeEater[x] > most)
{
most = PancakeEater[x];
mostgluttonist = x;
}
}
If you have to do a more searches on this array, then you would be best to sort the array and then find the answer. This method is just fine for an array size 10 with 1 search to do on though. You can also modify the loop to be better, by storing anyone who ate the same as another person, if you want to have a go at doing that.
#include <iostream>
usingnamespace std;
int main ()
{
int z;
int y = 0;
int temp = 0;
int x;
int PancakeEater[10];
cout << "Please Enter How Many Pancakes Each Person Ate\n";
for (int x =0; x < 10; x++)
{
cout << "Person " << x+1 << ": \n";
cin >> PancakeEater[x];
cout << endl;
}
for (int z = 0; z < 10; z++)
{
if(PancakeEater[z] > temp)
{
temp = PancakeEater[z];
}
}
cout << "Person "<< y << " ate the most pancakes: " << temp << endl << endl;
for (x = 0; x < 10; x++)
{
cout << "Person " << x+1 << " ate: ";
cout << PancakeEater[x] <<endl;
}
}
I fixed it! but now I am stuck on how to make it show the person who ate the most correctly. I have tried setting it to PancakeEater[x] then [z] but then I realized that will just show the last value. Any suggestions?
When I do your code Superdude I always get 9 for GuyWhoAteMost, and I am assuming its because z will end up at 9 after the for loop. so GuyWhoAteMost will then be 9.
#include <iostream>
int main()
{
shortint x = 0, y = 0, z = 0, arr[10] = {5,2,7,2,9,5,5,2,11,4};
for(x = 0;x < 10;++x)
{
if(arr[x] > y)
{
y = arr[x]; //Store the value of the element in the array.
z = x; //Store the position of the element in the array.
}
}
std::cout << "Highest amount was: " << y << " which was element " << z << " in the array.\n";
return 0;
}
std::max_element returns an iterator. Study the std::distance.
However, these standard algorithms do no more than the explicit loop, which has been discussed in other comments. While learning to use the STL is a very good thing, the main purpose of homework is to learn to think logically. Therefore, explicit loop is more educative, because it helps you start to understand why the code does or doesn't work.
I doubt this is his homework since it is an article on this site. But I would also suggest to try and solve things yourself and not with stl first then if you want to compare try the stl.
You can always use a bubble sort method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
bool sorted = false;
int temp = 0;
while( !sorted )
{
sorted = true;
for( int i = 0; i < N; ++i )
{
if( PancakeEater[i] < PancakeEater[i+1] )
{
temp = PancakeEater[i];
PancakeEater[i] = PancakeEater[i+1];
PancakeEater[i+1] = temp;
sorted = false;
}
}
}
//This sorts in order form greatest -> least
Here's a version I tried to make fairly easy with a class
but you don't need a class you can just add the bubble sort to your array whihc would prob be better for you instead of using the unnecessary stuff I did.