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 92 93
|
#include <iostream>
using namespace std;
/*
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
*/
int main ()
{
int const length = 10;
int numbPerson = length;
int person[length]= {1,2,3,4,5,6,7,8,9,10};
int numberOfPancakes[length];
int personHolder = -1;
int pancakeHolder = -1;
//get number of pancakes eaten by 10 different people.
for(int i=0;i<numbPerson;i++)
{
cout << "Enter the amount of pancakes person "<<person[i]<<" ate."<<endl;
cin >> numberOfPancakes[i];
}
//find min and max
int min = numberOfPancakes[0];
int max = numberOfPancakes[0];
for (int i=1; i<length; i++)
{
if (numberOfPancakes[i] < min)
min = numberOfPancakes[i];
if (numberOfPancakes[i] > max)
max = numberOfPancakes[i];
}
cout << "Min: " << min << endl;
cout << "Max: " << max << endl;
//Sort using Bubble sort.
for (int counter = length-1 ; counter > 0 ; counter--)
{
for (int i = 0 ; i < numbPerson ; i++)
{
if (numberOfPancakes[i] > numberOfPancakes[i+1])
{
pancakeHolder = numberOfPancakes[i];
numberOfPancakes[i] = numberOfPancakes[i+1];
numberOfPancakes[i+1] = pancakeHolder;
personHolder = person[i];
person[i] = person[i+1];
person [i+1] = personHolder;
}
}
numbPerson--;
}
for (int i=0;i<length; i++)
{
cout<< person[i] << ") " << numberOfPancakes[i]<< endl;
}
return 0;
}
|