The summer vacation has just started after I finished my first year for my bachelor's degree in computer engineering.
I was thinking that I could probably warm up with doing C++ before we start doing it next year. Therefore I started doing a couple of Beginner Exercises, more specifically these questions:
http://www.cplusplus.com/forum/articles/12974/
I managed to get to this question before I got stuck..
Requires:
variables, data types, and numerical operators
basic input/output
logic (if statements, switch statements)
loops (for, while, do-while)
arrays
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 pancakes |
So far I've done the first two, without much problems, however, I'm struggling on the last one, with 4 stars, where you're suppose to sort them after highest to lowest (which I've kind of done, just the wrong way around).
I just don't know how to fix the Persons to the amount of pancakes using an array.
Also, if you've got the chance, I'd really appreciate if you were able to look through the code to see if I do anything really wrong or if it's something I could do to make it more readable.
I've been trying to keep things outside of "int main" for practice purposes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
#include <iostream>
using namespace std;
enum persons;
void input();
void minMax();
void sortArray();
void print();
int main()
{
enum persons;
input();
sortArray();
minMax();
print();
return 0;
}
enum persons
{
PERSON1,
PERSON2,
PERSON3,
PERSON4,
PERSON5,
PERSON6,
PERSON7 ,
PERSON8,
PERSON9,
PERSON10,
MAX_PERSONS
};
int anPancake[MAX_PERSONS];
void input()
{
int nX = 0;
int nPerson = 1;
int nPancakes = 0;
while (nX < 10)
{
cout << "Type how many pancakes Person " << nPerson <<
" was eating: " << endl;
cin >> nPancakes;
anPancake[nX] = nPancakes;
nX++;
nPerson++;
}
}
void minMax()
{
int nMaxPancakes = anPancake[0];
int nMinPancakes = anPancake[0];
for(int k = 0; k < MAX_PERSONS; k++)
{
if(nMaxPancakes < anPancake[k])
nMaxPancakes = anPancake[k];
else if(nMinPancakes > anPancake[k])
nMinPancakes = anPancake[k];
}
cout << "The maximum number of pancakes eaten was: " << nMaxPancakes << endl;
cout << "The minumum number of pancakes eaten was: " << nMinPancakes << endl;
}
void sortArray()
{
for(int x = 0; x < MAX_PERSONS; x++)
{
for(int y = 0; y < MAX_PERSONS - 1; y++)
{
if(anPancake[y] > anPancake[y + 1])
{
int temp = anPancake[y + 1];
anPancake[y + 1] = anPancake[y];
anPancake[y] = temp;
}
}
}
}
void print()
{
for(int i = 0; i < MAX_PERSONS; i++)
cout << "Person " << i << " ate: " << anPancake[i] << endl;
}
|
If there's something I haven't explained, let me know, as English is not my first language.
Thanks a lot.