Ok so I got directed towards some practice problems about a week ago and I finally got some time to get to em. The code is for this problem:
Pancake Glutton
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
I did some modifications. I plan on letting the users enter names for each of the 10 people, but haven't really thought about how to do that yet. I wrote my own algorithm on finding the smallest and biggest eater out of the bunch (felt pretty good about that). I kinda wanna throw these functions into header files to make it look a little nicer, but once I realized I wanted to do that I was almost done haha. I might still do it, who knows. Anywho, here it is. Lemme know what you think and how I could improve the program and my practices. Thank you in advance!
P.S. before I put this up, I noticed I need to put in a verification somewhere in there so people cant put in negative numbers or letters. Is there a way to check if an input was not an int? Also, would like to figure out a way to see ties. For example, two people tie for the biggest eater award with 23 pancakes or something. Would like to say that happened and what two people it was. I have a feeling this project is going to be kind of complex for my skill level, but hey, how else do you learn? I'll appreciate all input you guys have! Thanks again
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
|
#include <iostream>
using namespace std;
int FindAverage(int list[], int size);
int FindBiggestEater(int list[], int size);
int FindSmallestEater(int list[], int size);
int main()
{
int pancakes[10];
cout << "Please enter the number of pancakes eaten by each person: " << endl;
//get input
for(int x = 0; x < 10; x++)
{
cin >> pancakes[x];
}
cout << endl;
//display list
for (int x = 0; x < 10; x++)
{
cout << "Person " << x + 1 << " ate " << pancakes[x] << " pancakes" << endl;
}
//get average
cout << endl << "The average number of pancakes that were eaten is " << FindAverage(pancakes, 10) << endl << endl;
//get biggest
cout << "The biggest eater had " << FindBiggestEater(pancakes, 10) << " pancakes" << endl << endl;
//get smallest
cout << "The smallest eater had " << FindSmallestEater(pancakes, 10) << " pancakes" << endl << endl;
return 0;
}
int FindAverage(int list[], int sizeOfArray)
{
int accumulator = 0;
for(int count = 0; count < sizeOfArray; count++)
{
accumulator = accumulator + list[count];
}
return accumulator / 10;
}
int FindBiggestEater(int list[], int size)
{
int biggest = list[0];
int count = 0;
do
{
if (list[count] > biggest)
{
biggest = list[count];
}
else
{
count++;
}
}while(count < size);
return biggest;
}
int FindSmallestEater(int list[], int size)
{
int smallest = list[0];
int count = 0;
do
{
if (list[count] < smallest)
{
smallest = list[count];
}
else
{
count++;
}
}while(count < size);
return smallest;
}
|