Reputation: 55
I'm following a set of beginner assignments I found on a forum for c++, but I'm totally stuck at a task now. The task is as follows:
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.
Now I've sorted out the original bit and the one star bit, however I chose to make it a bit more difficult for myself and not just go with "Person 1, 2, 3, 4 and so on, instead I've assigned names to the characters, and I print it out with a switch
.
Here's my current code, I'd love some suggestions on how I could sort the array without messing with the order of the numbers in the array, as that would mess up my naming code I guess.
Here's my code, I know it's not the prettiest code out there but it's functional for now.
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int nMostPancakesName;
int nLeastPancakesName;
enum BreakfastNames
{
NED, // 0
ARYA, // 1
JON, // 2
ROBB, // 3
SANSA, // 4
CATELYN, // 5
BRAN, // 6
THEON, // 7
HODOR, // 8
GHOST // 9
};
int anArray[10];
cout << "Enter the number of pancakes Ned ate for breakfast: " << endl;
cin >> anArray[NED];
cout << "How many did Arya eat?" << endl;
cin >> anArray[ARYA];
cout << "And Jon?" << endl;
cin >> anArray[JON];
cout << "What about Robb?" << endl;
cin >> anArray[ROBB];
cout << "Did Sansa have any?" << endl;
cin >> anArray[SANSA];
cout << "Catelyn?" << endl;
cin >> anArray[CATELYN];
cout << "Crippleboy aka Bran?" << endl;
cin >> anArray[BRAN];
cout << "The traitor didn't get any, did he?" << endl;
cin >> anArray[THEON];
cout << "Hodor?" << endl;
cin >> anArray[HODOR];
cout << "No pets at the dining table, Ghost." << endl;
cin >> anArray[GHOST];
int nMaxPancakes = 0;
for (int nPancakes = 0; nPancakes < 10; nPancakes++)
if (anArray[nPancakes] > nMaxPancakes)
{
nMostPancakesName = nPancakes;
nMaxPancakes = anArray[nPancakes];
}
int nLeastPancakes = 100;
for (int nPancakes2 = 0; nPancakes2 < 10; nPancakes2++)
if (anArray[nPancakes2] < nLeastPancakes)
{
nLeastPancakesName = nPancakes2;
nLeastPancakes = anArray[nPancakes2];
}
for (int nStartIndex = 0; nStartIndex < 10; nStartIndex++)
{
int nSmallestIndex = nStartIndex;
for (int nCurrentIndex = nStartIndex + 1; nCurrentIndex < 10; nCurrentIndex++)
{
if (anArray[nCurrentIndex] < anArray[nSmallestIndex])
nSmallestIndex = nCurrentIndex;
}
}
switch(nMostPancakesName)
{
case 0:
cout << "Ned had " << nMaxPancakes << endl;
break;
case 1:
cout << "Arya had " << nMaxPancakes << endl;
break;
case 2:
cout << "Jon had " << nMaxPancakes << endl;
break;
case 3:
cout << "Robb had " << nMaxPancakes << endl;
break;
case 4:
cout << "Sansa had " << nMaxPancakes << endl;
break;
case 5:
cout << "Catelyn had " << nMaxPancakes << endl;
break;
case 6:
cout << "Bran had " << nMaxPancakes << endl;
break;
case 7:
cout << "Theon had " << nMaxPancakes << endl;
break;
case 8:
cout << "Hodor had " << nMaxPancakes << endl;
break;
case 9:
cout << "Ghost had " << nMaxPancakes << endl;
break;
}
switch(nLeastPancakesName)
{
case 0:
cout << "Ned had " << nLeastPancakes << endl;
break;
case 1:
cout << "Arya had " << nLeastPancakes << endl;
break;
case 2:
cout << "Jon had " << nLeastPancakes << endl;
break;
case 3:
cout << "Robb had " << nLeastPancakes << endl;
break;
case 4:
cout << "Sansa had " << nLeastPancakes << endl;
break;
case 5:
cout << "Catelyn had " << nLeastPancakes << endl;
break;
case 6:
cout << "Bran had " << nLeastPancakes << endl;
break;
case 7:
cout << "Theon had " << nLeastPancakes << endl;
break;
case 8:
cout << "Hodor had " << nLeastPancakes << endl;
break;
case 9:
cout << "Ghost had " << nLeastPancakes << endl;
break;
}
return 0;
}
Upvotes: 0
Views: 1552
Reputation: 28241
The answer by Wolfgang Skyler describes a sorting algorithm. You need a minor addition to the algorithm: something that remembers how the elements were swapped. One easy-to-understand solution is an additional array that starts sorted, and gets permuted in exactly the same way as the score
array:
BreakfastNames names[] = {
NED, // 0
ARYA, // 1
JON, // 2
ROBB, // 3
SANSA, // 4
CATELYN, // 5
BRAN, // 6
THEON, // 7
HODOR, // 8
GHOST // 9
};
// Code by Wolfgang Skyler goes here
...
// swap person 1 and 2, since 2 ate more that 1
int tmp = score[ScoreCheck];
score[ScoreCheck] = score[ScoreCheck+1];
score[ScoreCheck+1] = tmp;
BreakfastNames tmp1 = names[ScoreCheck];
names[ScoreCheck] = names[ScoreCheck+1];
names[ScoreCheck+1] = tmp1;
...
It is also possible to adapt your solution even though you don't implement the sorting algorithm. To do it, use the third parameter of std::sort
so it sorts the array names
but compares the data in scores
:
struct MyComparison
{
...
bool operator()(BreakfastNames name1, BreakfastNames name2)
{
...
return whatever1 < whatever2;
}
};
sort(names, names + 10, MyComparison(scores));
Upvotes: 1
Reputation: 1368
Here's a quick example. There are better way's to do this, but this should give you a basic idea of one of the way's to do this.
int score[10];
int ScoreCheck = 0;
// initalize the array "score"
for (ScoreCheck = 0; ScoreCheck < 10; ScoreCheck++)
{
score[ScoreCheck] = ScoreCheck; // just put everone somewhere
}
ScoreCheck = 0;
while ( ScoreCheck < 9 ) // check to see if we've reached the last person
{
// check to see if the person lower on the chart ate more
if ( anArray[score[ScoreCheck]] < anArray[score[ScoreCheck+1]] )
{
// swap person 1 and 2, since 2 ate more that 1
int tmp = score[ScoreCheck];
score[ScoreCheck] = score[ScoreCheck+1];
score[ScoreCheck+1] = tmp;
// now go back to the beggining to make
// sure they are in order from begining to end
ScoreCheck = 0;
continue;
}
// nope, it's in order so far
// increment to the next person on the chart
ScoreCheck++
}
Upvotes: 1
Reputation: 55
So I've sorted most of it with the following code:
sort(anArray, anArray + 10);
for(int ooo=0; ooo < 10; ooo++)
{
cout << anArray[ooo] << endl;
}
Now the question is how I would solve it to get the names next to the results. I'm feeling like I should go back and make the array multi dimensional, so I'd get a set value for the name as well as the number of pancakes eaten, would this be a good way to go about it or would you recommend something else?
Upvotes: 0