# need help with array in descending order

Page 1 of 1

## 3 Replies - 2901 Views - Last Post: 01 March 2011 - 03:59 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=219677&amp;s=1f3c2ab7469b9220ab430f3dbc611a2a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 miatech

Reputation: 0
• Posts: 19
• Joined: 18-February 11

# need help with array in descending order

Posted 01 March 2011 - 03:19 PM

Write a program that asks the user to enter the number of pancakes eaten for breakfast by 10 different people (Person 1, Person 2, ..., Person 10)
Once the data has been entered the program must analyze the data and output which person ate the most pancakes for breakfast.

Modify the program so that it also outputs which person ate the least number of pancakes for breakfast.

Modify the program so that it outputs a list in order of number of pancakes eaten of all 10 people.
i.e.
Person 4: ate 10 pancakes
Person 3: ate 7 pancakes
Person 8: ate 4 pancakes
...
Person 5: ate 0 pancak
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
The first portion of this exercise was pretty easy, but is the second part that I need help with... this is not sorting, but printing the array in descending order, but keeping the index and value intact. Here's what I got so far

```#include <iostream>
#include <string>
using namespace std;
int main()
{
int persons[10] = { 4, 5, 6, 2, 1, 4, 8, 9, 10, 20 };
for(int i = 0; i < 10; i++)
{
cout << "Enter how many pancakes person " << i << " ate: ";
cin >> persons[i];
}
//finding biggest and smallest
int small = persons[0], big = persons[0], idSmall, idBig;
for(int j = 0; j < 10; j++)
{
if(small > persons[j]) { small = persons[j]; idSmall = j; }
if(big < persons[j]) { big = persons[j]; idBig = j; }
}

cout << "The person who ate the most pancakes > " << idBig << endl;
cout << "The person who ate the least pancakes > " << idSmall << endl;

system("pause");
return 0;
}
```

Is This A Good Question/Topic? 0

## Replies To: need help with array in descending order

### #2 Aphex19

• Born again Pastafarian.

Reputation: 619
• Posts: 1,873
• Joined: 02-August 09

## Re: need help with array in descending order

Posted 01 March 2011 - 03:40 PM

You need to use a sorting algorithm. I might be flamed for advising this, but bubble sort might be a good one to go with at first, it's relatively simple.

### #3 #define

• Duke of Err

Reputation: 1860
• Posts: 6,698
• Joined: 19-February 09

## Re: need help with array in descending order

Posted 01 March 2011 - 03:49 PM

You don't need to sort, although it could be done that way with parallel arrays.

You know the largest number of pancakes. Iterate through the array and print the people who have that number. Decrement the number by one and iterate through the array again, and print the people, if any, who have that number. Repeat until finished.

### #4 miatech

Reputation: 0
• Posts: 19
• Joined: 18-February 11

## Re: need help with array in descending order

Posted 01 March 2011 - 03:59 PM

#define, on 01 March 2011 - 03:49 PM, said:

You don't need to sort, although it could be done that way with parallel arrays.

You know the largest number of pancakes. Iterate through the array and print the people who have that number. Decrement the number by one and iterate through the array again, and print the people, if any, who have that number. Repeat until finished.

I get what you're saying.... I think is the easiest and simplest way.

Thanks