Pancake Glutton

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/

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
#include <iostream>

using namespace 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;
}

else if(PancakeEater[1]>PancakeEater[2])
{
    cout << "Person 2 At The Most Pancakes " << PancakeEater[1] << endl;


}

else if(PancakeEater[2] > PancakeEater[3])
{
    cout << PancakeEater[2] << endl;
}


else if (PancakeEater[3]>PancakeEater[4])
{
    cout << PancakeEater [3];
}

else if (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.
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
#include <iostream>

using namespace 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?
Here:



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
#include <iostream>

using namespace std;

int main ()
{
int GuyWhoAteMost = 0;
int MostPancakesEaten = 0;
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];
     
   }
   GuyWhoAteMost = z;
   MostPancakesEaten = temp;

}

cout << "Person "<< GuyWhoAteMost << " ate the most pancakes: " <<  temp << MostPancakesEaten << endl;

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


    cout << "Person " << x+1 << " ate: ";
    cout << PancakeEater[x] << " pancakes.\n";


}

cout << "Press ENTER to exit...";
cin.sync();
cin.get();
return 0;
}

Hope this helps!
Last edited on
If you want to use an already made algorithm you can use the sort algorithm in the algorithm library.


You could also create an algorithm to sort an array/vector yourself fairly easy by using the operator > or < and a loop/function.
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.
Still need help. Trying to figure out how to assign the person who ate the most pancakes. !Updated 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

#include <iostream>
#include <algorithm>

using namespace std;

int main ()
{




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;


}




cout << "Person "<< x /* place holder */ << " ate the most pancakes: " << *max_element(PancakeEater, PancakeEater+10)  << endl << endl;

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


    cout << "Person " << x+1 << " ate: ";
    cout << PancakeEater[x] <<endl;


}




}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main()
{
    short int 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.


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
#include <iostream>
#include <vector>

class Person
{
	public:
		Person() = default;
		Person( int pancakes );
		friend bool operator<( const Person &rhs , const Person &lhs );
		friend std::ostream &operator<<( std::ostream &stm , const Person &per );
		static int id;
	private:
		int eater_id , pancakes_ate;
};

Person::Person( int pancakes ) : eater_id( Person::id ) , pancakes_ate( pancakes )
{
	++id;
}

bool operator<( const Person &rhs , const Person &lhs )
{
	return( rhs.pancakes_ate < lhs.pancakes_ate );
}

std::ostream &operator<<( std::ostream &stm , const Person &per )
{
	return( stm << "Person: " << per.eater_id << " | Pancakes: " << per.pancakes_ate );
}

void bubblesort( std::vector<Person> &container )
{
	bool sorted = false;
	Person temp;
	while( !sorted )
	{
		sorted = true;
		for( size_t i = 0; i < container.size(); ++i )
		{
			if( container[i] < container[i+1] )
			{
				temp = container[i];
				container[i] = container[i+1];
				container[i+1] = temp;
				sorted = false;
			}
		}
	}
}
int Person::id = 0;
int main()
{
	int pancakes = 0;
	std::vector<Person> container;
	
	for( int i = 0; i < 10; ++i )
	{
		std::cout << "Please enter the amount of pancakes person " << i + 1 << " ate: ";
		std::cin >> pancakes;
		container.push_back( Person( pancakes ) );
	}
	
	bubblesort( container );
	
	for( const auto &it : container )
		std::cout << it << std::endl;
	
	std::cout << "The person who ate the most is : " << std::endl;
	std::cout << container.front() << std::endl;
	std::cout << "The person who ate the least is :" << std::endl;
	std::cout << container.back() << std::endl;
	
	return( 0 );
}


Simple version:
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
#include <iostream>

void bubblesort( int *array , const int SIZE )
{
	bool sorted = false;
	int temp = 0;
	while( !sorted )
	{
		sorted = true;
		for( int i = 0; i < SIZE; ++i )
		{
			if( array[i] < array[i+1] )
			{
				temp = array[i];
				array[i] = array[i+1];
				array[i+1] = temp;
				sorted = false;
			}
		}
	}
}

int main()
{
	const int NUMBER_OF_PANCAKE_EATERS = 10;
	int PancakeEater[NUMBER_OF_PANCAKE_EATERS] , pancakes = 0;
	
	for( int i = 0; i < NUMBER_OF_PANCAKE_EATERS; ++i )
	{
		std::cout << "Please enter the number of pancakes person " << i + 1 << " ate: ";
		std::cin >> pancakes;
		PancakeEater[i] = pancakes;
	}
	
	bubblesort( PancakeEater , NUMBER_OF_PANCAKE_EATERS );
	
	std::cout << "MOst pancakes ate was: " << PancakeEater[0] << std::endl;
	std::cout << "Least pancakes ate was: " << PancakeEater[9] << std::endl;
}


Tyvm for the help guys it worked finally! I can move onto the next one ;D!
You should post the fixed solution in case someone else ever has the same problem.
Topic archived. No new replies allowed.