3 Replies - 2901 Views - Last Post: 01 March 2011 - 03:59 PM Rate Topic: -----

#1 miatech   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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   User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 619
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1860
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#4 miatech   User is offline

  • New D.I.C Head

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

Re: need help with array in descending order

Posted 01 March 2011 - 03:59 PM

View Post#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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1