Ordering numbers inside of arrays

Hi guys, I was wondering lets say I have an array---actually let me show you
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
	const int numberOfPeople = 10;
	int pancakesEaten[numberOfPeople];
	for(int i = 0; i<numberOfPeople;i++){
		
		std::cout << "Please enter the amount of pancakes eaten for person " << i+1 << std:: endl;
		std::cin >> pancakesEaten[i];

	}
	for (int i = 0;i<numberOfPeople;i++){
		std::cout << "Person " << i+1 << " ate: " << pancakesEaten[i] << std::endl;
	}

	Sleep(5000);

	
	return 0;


OK so this code uses an array to store 10 values given by the user through std::cin. Now if I would want to print out the list in order how would go about doing that exactly? When I say order I mean print out a list of all the data stored in the array in order from largest number to smallest number. I was able to make a for loop to print out everything in the array but I don't know what I could add in there to order it up. I read about some smaller and bigger functions somewhere but I am not to sure where or how to use them. Any help would be appreciated. :D
Sort the array, and then print it out.

Try not to combine steps in programming :D
If I have correctly understood you you are going to change this loop

1
2
3
4
for (int i = 0;i<numberOfPeople;i++)
{
	std::cout << "Person " << i+1 << " ate: " << pancakesEaten[i] << std::endl;
}


to the loop

1
2
3
4
for (int i = numberOfPeople ; i != 0; )
{
	std::cout << "Person " << i << " ate: " << pancakesEaten[--i] << std::endl;
}


Also you can use standard algorithms as for example copy or reverse_copy. You could also use the based on range for if it would proviide reverse order.
Last edited on
Sort it? How would I do that...actually I will google that.. Thanks for the help...
http://lmgtfy.com/?q=Sorting+Arrays+In+C%2B%2B
----------------------------
And I see what you are saying, so I could possibly have sorting as a function somewhere, and then I can call it and use that array for its parameters, and then just print it out like the data is left after the function is run.


Thanks :D

Also isn't there a function that allows you to see which number is bigger than another? Not the operators theres like an actual function I think I have seen somewhere. I am not sure :3 Well not see which one is bigger but compare multiple numbers in it's parameters and decide which is bigger.
Last edited on
As I have said there are standard algorithms as std::sort, std::reverse, std::max_element, std::min_element, std::max, std::min and so on in C++.
Whats wrong with using

if(arr[i] > arr[i+1]) ?

I can't imagine a function being an easier to use than that
@vlad sorry I was writing that before you posted it was directed at Biscuit, anywho thank you both alot... You have helped me a bunch :D
Also
@vlad
numberOfPeople is a constant Variable, so the loop would run forever because 10 will never 0, and it will just print out the same person, also what exactly does -- do infront of the i? does it subract 1 from it before the end of the operation?
@curscascis
numberOfPeople is a constant Variable, so the loop would run forever because 10 will never 0, and it will just print out the same person, also what exactly does -- do infront of the i? does it subract 1 from it before the end of the operation?


You are wrong. Look the loop one more

1
2
3
4
for (int i = numberOfPeople ; i != 0; )
{
	std::cout << "Person " << i << " ate: " << pancakesEaten[--i] << std::endl;
}


Here i is changed from numberOfPeople to 0. So all numberOfPeople elements will be printed.
Last edited on
Topic archived. No new replies allowed.