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;
}