Finding the max number

I'm doing the exercises in this topic: http://www.cplusplus.com/forum/articles/12974/
The one I'm talking about is Pancake Glutton. (For now I'm doing the exercise without the * and the **** so don't give me the code for them too)
This is the code:

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

using namespace std;

int main()
{
	int person[100];
	for(int i=0;i<10;i++)
	{
		cout<<"Write how many pancakes person "<<i+1<<" ate."<<endl;
		cin>>person[i];
	}
	//comparison
	return 0;
}


I don't know what code I have to write in order to compare the pancekes eaten buy people and finding the one that ate the most pancakes.
Do I have to use a for loop?
It will look a lot like the loop you already have. Your current loop increments i so that it is an index that marches over your array and lets the user put values into it. Your new for loop will treat the indicies the same way, but the body of the loop will do something else. Instead of saying "put a value at position i" it will have to resolve "is the value at position i the biggest one I've seen so far?" You should use a new variable to keep track of the biggest item in the array you've seen as your loop marches over it.

EDIT:
Is it possible for a person to eat a negative number of pancakes? Consider using an unsigned type:
unsigned int person[100];
Last edited on
The code is like this, right?

1
2
3
4
5
6
7
for(int j=0;j<10;j++)
	{
		if(max<person[j])
		{
			max=person[j];
		}
	}


How can I know who is the person? After this for I have to give an output that say something like: "The person who ate the most pancakes is person 4."
After I do that for loop it doesn't give me the person who ate the most pancakes.

EDIT: Nevermind! I found the 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
#include <iostream>

using namespace std;

int main()
{
	unsigned int person[100];
	int max=0, number=0;
	for(int i=0;i<10;i++)
	{
		cout<<"Write how many pancakes person "<<i+1<<" ate."<<endl;
		cin>>person[i];
	}
	for(int j=0;j<10;j++)
	{
		if(max<person[j])
		{
			max=person[j];
			number=j;
		}
	}
	cout<<"The person who ate the most pancakes is person "<<number+1<<endl;
	cout<<"Person "<<number<<" ate "<<max<<" pancakes."<<endl;
	
	return 0;
}
Last edited on
You need to store the index j as well (in order to get the right person).
max is only usefull inside the loop.
Okay, I did the second point of the exercise(It was similar to the previous for loop).
The third point... well, I have no clue for that one. Now I understand why there are ****. I'm sure I have to use the for loop but multiples time, right? But I don't know what to write in it. Do I have to re-order them? I'm literally stuck here.
For the last part, you need to sort your array of people to arrange the number of pancakes from largest to smallest. Remember to keep track of who ate what as you sort.

You can try to implement bubble sort, insertion sort or selection sort to do this.
So, while I'm sorting, I have to put people and how many pancakes they ate into a new array, right?

Where do I find those sorts? (Bubble sort, insertion sort and selection sort)
Last edited on
Kernul wrote:
Where do I find those sorts? (Bubble sort, insertion sort and selection sort)

You have to write the code yourself if you want to use any of those sorting methods I mentioned:
http://en.wikipedia.org/wiki/Bubble_sort
http://en.wikipedia.org/wiki/Insertion_sort
http://en.wikipedia.org/wiki/Selection_sort

There is also a sorting method in the algorithms library
http://www.cplusplus.com/reference/algorithm/sort/

This sort is already written for you, but you should practice writing the other sorts I mentioned as this is a good way to boost your learning.

To use the sort algorithm (implemented as introsort http://en.wikipedia.org/wiki/Introsort), here is one of the many possible ways of doing it (This can also be applied to any sorting algorithm):
- First build a list numbers numbered 0-9 (indices)
- Then sort this list of numbers by using value contained in your pancake array as the condition
- So something like this:
1
2
3
4
5
...
std::array<int> nums = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
std::sort(nums.begin(), nums.end(), [&](int a, int b) -> bool {
return persons[a] > persons[b];
});


Then to print the values
1
2
for (int v : nums)
    std::cout << "Person " << v << " ate " << persons[v] << " pancakes\n";


And there you have it. All the features used are part of c++11 standard
std::array is found in the <array> library
[&](int , int ) -> bool is a lambda function that takes 2 integers as parameter and returns bool
for (int v : nums) This is a range based for-loop

Like I said, it is one way of doing it, and there are other ways
Thank you! I'll exercise on those sorting methods. C:
Topic archived. No new replies allowed.