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
Current version I have written: http://codepad.org/QHnt11CT
#include <iostream>
#include <string>
void bubbleSort(int arr[], int n) {
bool swapped = true;
int j = 0;
int tmp;
while (swapped) {
swapped = false;
j++;
for (int i = 0; i < n - j; i++) {
if (arr[i] > arr[i + 1]) {
tmp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = tmp;
swapped = true;
}
}
}
}
int main()
{
int pancakeAmount[10];
std::string consumers[10];
for (int i = 0, j = 1; i < 10; i++, j++) {
std::cout << "Please enter an amount of pancakes eaten by consumer"\
" number " << j << "." << std::endl;
std::cin >> pancakeAmount[i];
std::cout << "Please enter the name of the person who ate that amount"\
" of pancakes." << std::endl;
getline(std::cin, consumers[i]);
}
std::cout << "The results from least amount eaten to the greatest amount"\
" eaten are as follows:" << std::endl;
bubbleSort(pancakeAmount, 10);
for (int k = 0; k < 10; k++) {
std::cout << pancakeAmount[k] << std::endl;
}
return 0;
}
That is the problem I am currently working on. As of right now, the first two objectives of this problem have been solved without any notable issues.
The third objective, however, is proving to be a bit more difficult. I am having a hard time designing/implementing a sorted list with the appropriate labels. In line 47 I am trying to obtain a name or label to give to the corresponding amount.
I am running into an issue where the console will accept the amount I want to assign, but will completely ignore the call to the getline() function and loop back into asking for another amount. When the getline() function is called before the "std::cin >> pancakeAmount[i]" is called, I can give input on the first loop, but successive loops produce the error I was encountering when the getline() function was in it's original position in the code.
Am I trying to utilize an array of strings in an improper fashion, or is the getline() function not being used properly?
So today is your lucky day, because usually I don't do homework.
Your problem is that you need to ignore the return of your cin.
So this should solve your problem but it is not tested:
struct PanCakeEater
{
int pancakeEaten;
std::string name;
};
bool PanCakeSort(PanCakeEater const& eater1, PanCakeEater const& eater2)
{
return eater1.pancakeEaten < eater2.pancakeEaten;
}
int main()
{
std::vector<PanCakeEater> eaters;
const size_t numberOfEaters = 3;
for(size_t i = 0; i < numberOfEaters; ++i)
{
PanCakeEater p;
std::cout << "Please enter an amount of pancakes eaten by consumer number " << i << "." << std::endl;
std::cin >> p.pancakeEaten;
std::cin.ignore(); // ignore the \n
std::cout << "Please enter the name of the person who ate that amount of pancakes." << std::endl;
getline(std::cin, p.name);
eaters.push_back(p);
}
std::sort(eaters.begin(), eaters.end(), PanCakeSort);
for(auto iter = eaters.begin(); iter != eaters.end(); ++iter)
{
std::cout << iter->name << ": " << iter->pancakeEaten << std::endl;
}
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int ate[10],eater[10],hold,temp;
cout<<"\t\tEnter How Many Pancakes Did Each Person Eat?\n\n";
for(int h=0;h<10;h++)
{
eater[h]=h;
}
for(int q=0;q<10;q++)
{
cout<<"Person "<<eater[q]+1<<" Ate: ";
cin>>ate[q];
}
cout<<"____________________________________________\n\n"<<"\t\tOrdered List Of Above.\n\n";
for(int a=0;a<10;a++)
{
for(int b=0;b<10;b++)
{
if(ate[b]<ate[b+1])
{
hold=ate[b];
ate[b]=ate[b+1];
ate[b+1]=hold;
temp=eater[b];
eater[b]=eater[b+1];
eater[b+1]=temp;
}
}
}
for(int w=0;w<10;w++)
{
cout<<"Person "<<eater[w]+1<<" Ate "<<ate[w]<<" Pancakes\n";
}
return 0;
}
Here is a simple version of it. Must be easier for some beginners to understand.
#include <iostream>
#include <string>
using namespace std;
int main() {
int y;
cout << "How many people do you want to enter= \n";
cin >> y;
string names[10];
int pancakes[10];
for (int i = 0; i <= y-1 ; i++) {
cout << "enter name= ";
cin >> names[i];
cout << "num of pancakes= ";
cin >> pancakes[i];
}
int maxEaten = pancakes[0];
int minEaten = pancakes[0];
string maxPer = names[0];
string minPer = names[0];
for (int k = 1; k < y; k++) {
if (pancakes[0] < pancakes[k]) {
maxEaten = pancakes[k];
maxPer = names[k];
}
if (pancakes[0] > pancakes[k]) {
minEaten = pancakes[k];
minPer = names[k];
}
}
cout << maxPer << " ate " << maxEaten << " which is the maximum." << endl;
cout << minPer << " ate " << minEaten << " which is the minimum." << endl;
system("pause");
}
Related
For this program a user must enter 10 contestants and the amount of second it took for them to complete a swimming race. My problem is that I must output the 1st, 2nd and 3rd placers, so I need to get the three smallest arrays (as they would be the quickest times) but I'm unsure on how to do it. Here is my code so far.
string names[10] = {};
int times[10] = { 0 };
int num[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int min1 = 0, min2 = 0, min3 = 0;
cout << "\n\n\tCrawl";
for (int i = 0; i < 10; i++)
{
cout << "\n\n\tPlease enter the name of contestant number " << num[i] << ": ";
cin >> names[i];
cout << "\n\tPlease enter the time it took for them to complete the Crawl style: ";
cin >> times[i];
while (!cin)
{
cout << "\n\tError! Please enter a valid time: ";
cin.clear();
cin.ignore();
cin >> times[i];
}
if (times[i] < times[min1])
min1 = i;
cout << "\n\n\t----------------------------------------------------------------------";
}
system("cls");
cout << "\n\n\tThe top three winners of the Crawl style race are as follows";
cout << "\n\n\t1st Place - " << names[min1];
cout << "\n\n\t2nd Place - " << names[min2];
cout << "\n\n\t3rd Place - " << names[min3];
}
_getch();
return 0;
}
As you can see, it is incomplete. I know how to get the smallest number, but its the second and third smallest that is giving me trouble.
your code is full of errors:
what do you do with min2 and min3 as long as you don't assign them?? they are always 0
try checking: cout << min2 << " " << min3;
also you don't initialize an array of strings like that.
why you use an array of integers for just printing number of input:
num? instead you can use i inside loop adding to it 1 each time
to solve your problem use a good way so consider using structs/clusses:
struct Athlete
{
std::string name;
int time;
};
int main()
{
Athlete theAthletes[10];
for(int i(0); i < 10; i++)
{
std::cout << "name: ";
std::getline(std::cin, theAthletes[i].name);
std::cin.sync(); // flushing the input buffer
std::cout << "time: ";
std::cin >> theAthletes[i].time;
std::cin.sync(); // flushing the input buffer
}
// sorting athletes by smaller time
for(i = 0; i < 10; i++)
for(int j(i + 1); j < 10; j++)
if(theAthletes[i].time > theAthletes[j].time)
{
Athlete tmp = theAthletes[i];
theAthletes[i] = theAthletes[j];
theAthletes[j] = tmp;
}
// printing the first three athletes
std::cout << "the first three athelets:\n\n";
std::cout << theAthletes[0].name << " : " << theAthletes[0].time << std::endl;
std::cout << theAthletes[1].name << " : " << theAthletes[1].time << std::endl;
std::cout << theAthletes[2].name << " : " << theAthletes[2].time << std::endl;
return 0;
}
I hope this will give u the expected output. But i suggest u to use some sorting alogirthms like bubble sort,quick sort etc.
#include <iostream>
#include<string>
using namespace std;
int main() {
int times[10] = { 0 };
int num[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int min1 = 0, min2 = 0, min3 = 0,m;
string names[10] ;
cout << "\n\n\tCrawl";
for (int i = 0; i < 10; i++)
{
cout << "\n\n\tPlease enter the name of contestant number " << num[i] << ": ";
cin >> names[i];
cout << names[i];
cout << "\n\tPlease enter the time it took for them to complete the Crawl style: ";
cin >> times[i];
cout<<times[i];
while (!cin)
{
cout << "\n\tError! Please enter a valid time: ";
cin.clear();
cin.ignore();
cin >> times[i];
}
if(times[i]==times[min1]){
if(times[min1]==times[min2]){
min3=i;
}else{min2 =i;}
}else if(times[i]==times[min2]){
min3=i;
}
if (times[i] < times[min1]){
min1 = i;
cout <<i;
}
int j=0;
while(j<i){
if((times[j]>times[min1])&&(times[j]<times[min2])){
min2 =j;
j++;
}
j++;
}
m=0;
while(m<i){
if((times[m]>times[min2])&&(times[m]<times[min3])){
min3 =m;
m++;
}
m++;
}
cout << "\n\n\t----------------------------------------------------------------------";
}
cout << "\n\n\tThe top three winners of the Crawl style race are as follows";
cout << "\n\n\t1st Place - " << names[min1];
cout << "\n\n\t2nd Place - " << names[min2];
cout << "\n\n\t3rd Place - " << names[min3];
return 0;
}
There is actually an algorithm in the standard library that does exactly what you need: std::partial_sort. Like others have pointed out before, to use it you need to put all the participant data into a single struct, though.
So start by defining a struct that contains all relevant data. Since it seems to me that you only use the number of the contestants in order to be able to later find the name to the swimmer with the fastest time, I'd get rid of it. Of course you could also add it back in if you like.
struct Swimmer {
int time;
std::string name;
};
Since you know that there always will be exactly 10 participants in a race, you can also go ahead and replace the C-style array by a std::array.
The code to read in the users then could look like this:
std::array<Swimmer, 10> participants;
for (auto& participant : participants) {
std::cout << "\n\n\tPlease enter the name of the next contestant: ";
std::cin >> participant.name;
std::cout << "\n\tPlease enter the time it took for them to complete the Crawl style: ";
while(true) {
if (std::cin >> participant.time) {
break;
}
std::cout << "\n\tError! Please enter a valid time: ";
std::cin.clear();
std::cin.ignore();
}
std::cout << "\n\n\t----------------------------------------------------------------------";
}
Partial sorting is now essentially a one-liner:
std::partial_sort(std::begin(participants),
std::begin(participants) + 3,
std::end(participants),
[] (auto const& p1, auto const& p2) { return p1.time < p2.time; });
Finally you can simply output the names of the first three participants in the array:
std::cout << "\n\n\tThe top three winners of the Crawl style race are as follows";
std::cout << "\n\n\t1st Place - " << participants[0].name;
std::cout << "\n\n\t2nd Place - " << participants[1].name;
std::cout << "\n\n\t3rd Place - " << participants[2].name << std::endl;
The full working code can be found on coliru.
This is not a full solution to your problem, but just meant to point you into the right direction...
#include <iostream>
#include <limits>
#include <algorithm>
using namespace std;
template <int N>
struct RememberNsmallest {
int a[N];
RememberNsmallest() { std::fill_n(a,N,std::numeric_limits<int>::max()); }
void operator()(int x){
int smallerThan = -1;
for (int i=0;i<N;i++){
if (x < a[i]) { smallerThan = i; break;}
}
if (smallerThan == -1) return;
for (int i=N-1;i>smallerThan;i--){ a[i] = a[i-1]; }
a[smallerThan] = x;
}
};
int main() {
int a[] = { 3, 5, 123, 0 ,-123, 1000};
RememberNsmallest<3> rns;
rns = std::for_each(a,a+6,rns);
std::cout << rns.a[0] << " " << rns.a[1] << " " << rns.a[2] << std::endl;
// your code goes here
return 0;
}
This will print
-123 0 3
As you need to know also the names for the best times, you should use a
struct TimeAndName {
int time;
std::string name;
}
And change the above functor to take a TimeAndName instead of the int and make it also remember the names... or come up with a different solution ;), but in any case you should use a struct similar to TimeAndName.
As your array is rather small, you could even consider to use a std::vector<TimeAndName> and sort it via std::sort by using your custom TimeAndName::operator<.
So I'm trying to create an array that contains some user inputted names, and then associate those names with letter grades from tests (ex: A, B, C, D, F). My question is, how would I use an array to accept the user inputted names?
EDIT:
Sorry this is a bit long, I don't know what part to put that would help out. Totally new to C++ and I can't seem to find anything online regarding the matter, lol.
Here is some code. This program currently asks the user for test scores, then displays and drops the lowest test score, and finally, calculates the average of the scores without the lowest one. The end goal is to ask the user for 5 students names, and 4 scores for each student, then dropping the lowest score for each student and calculating the averages of ALL scores inputted regardless of student.
#include <iostream>
#include <string>
using namespace std;
void getScore(int &);
int findLowest(int [], int);
void calcAverage(int [], int);
int main () {
const int NUM_SCORES = 5;
int scores[NUM_SCORES];
cout << "Welcome to test averages." << endl;
cout << "Please enter scores for " << NUM_SCORES << " students." << endl;
cout << endl;
for (int i = 0; i < NUM_SCORES; i++) {
getScore(scores[i]);
}
for (int i = 0; i < NUM_SCORES; i++) {
cout << "Score " << (i + 1) << ": " << scores[i] << endl;
}
cout << endl;
cout << "The lowest of these scores is " << findLowest(scores, NUM_SCORES) << endl;
calcAverage(scores, NUM_SCORES);
return 0;
}
void getScore(int & s) {
s = -1;
cout << "Please enter a test score: ";
cin >> s;
while (s < 0 || s > 100) {
cout << "Score range must be from 0-100" << endl;
cout << "Please re-enter a score: ";
cin >> s;
}
}
int findLowest(int theArray [], int theArraySize) {
int lowest = theArray[0];
for (int i = 1; i < theArraySize; i++) {
if (theArray[i] < lowest) {
lowest = theArray[i];
}
}
return lowest;
}
void calcAverage(int theArray [], int theArraySize) {
int sum = 0;
for (int i = 0; i < theArraySize; i++) {
sum += theArray[i];
}
double average = (sum - findLowest(theArray, theArraySize)) / (theArraySize - 1.0);
cout << "The average is " << average << endl;
}
Try getline from #include <string>
std::string names[5];
for (int i = 0; i < 5; ++i){
getline(std::cin, names[i]);
}
I was comparing the array values in format arr[i] > arr[max]
It outputs all the values which is greater to arr[0].
Can you guys give me tips on how to ouput JUST the greatest value?
Code:
for(int i=0;i<4;i++){
cout<<"Enter how many pancakes eaten by the Person #"<<i<<" : ";
cin>>pancake;
person[i] = pancake;
}
for(int i=0;i<4;i++){
if(person[i]>person[i+1]){
pancakeCount = pancake[i];
cout<<"The number of most eaten pancakes is "<<pancakeCount<<" by Person#"<<i<<endl;
}
}
The problem is the output:
Output:
//list of persons and the number of pancakes they ate.
The number of most eaten pancakes is 10 by Person#7
The number of most eaten pancakes is 10 by Person#9
each time I put the greatest value before the last iteration, it also outputs the last iteration containing the greatest value which is Person#7
I only want the highest value to be the output itself.
int pancakeCount(0), max_person(0);
for(int i=0;i<4;i++){
cout<<"Enter how many pancakes eaten by the Person #"<<i<<" : ";
cin>>pancake;
person[i] = pancake;
}
for(int i=0;i<4;i++){
if(person[i]>pancakeCount){
pancakeCount = pancake[i];
max_person = i;
}
}
cout<<"The number of most eaten pancakes is "<<pancakeCount<<" by Person#"<<i<<endl;
Hi your code is wrong algorithim try mine
for(int i=0;i<4;i++)
{
std::cout<<"Enter how many pancakes eaten by the Person #"<<i<<" : ";
std::cin>>pancake;
person[i] = pancake;
}
for(int i=0;i<4;i++)
{
if(person[i]>pancakeCount)
{
pancakeCount = person[i];
personWhoEatMost = i;
}
}
std::cout<<"The number of most eaten pancakes is "<<pancakeCount
<<" by Person#"<<personWhoEatMost<<std::endl;
split the loops for finding the max and printing the max.
Code:
#include <iostream>
using namespace std;
int main(){
int person[4];
int pancake, pancakeMax, pancakeMin;
for (int i = 0; i<4; i++)
{
cout << "Enter how many pancakes eaten by the Person #" << i << " : ";
cin >> pancake;
person[i] = pancake;
}
//equate to one of the elements to make sure negative numbers are accomodated
pancakeMax = pancakeMin = pancake;
// most eaten
for (int i = 0; i<4; i++)
{
if (person[i]>pancakeMax)
{
pancakeMax = person[i];
}
}
for (int i = 0; i<4; i++)
{
if (person[i] == pancakeMax)
{
cout << "The number of most eaten pancakes is " << pancakeMax << " by Person#" << i << endl;
}
}
//least eaten
for (int i = 0; i<4; i++)
{
if (person[i]<pancakeMin)
{
pancakeMin = person[i];
}
}
for (int i = 0; i<4; i++)
{
if (person[i] == pancakeMin)
{
cout << "The number of least eaten pancakes is " << pancakeMin << " by Person#" << i << endl;
}
}
return 0;
}
You can add a break statement in the second loop if you think your logic needs just one value. Otherwise, right now, it give all occurances of maximum pancakes eaten.
If you are targetting efficiency you can merge the loops like so:
#include <iostream>
using namespace std;
int main(){
int person[4];
int pancake, pancakeMax, pancakeMin;
int iMax, iMin;
for (int i = 0; i<4; i++){
cout << "Enter how many pancakes eaten by the Person #" << i << " : ";
cin >> pancake;
person[i] = pancake;
}
//equate to one of the elements to make sure negative numbers are accomodated
pancakeMax = pancakeMin = pancake;
for (int i = 0; i<4; i++){
pancake = person[i];
if (pancake>pancakeMax){// most eaten
pancakeMax = pancake;
iMax = i;
}
if (pancake < pancakeMin){//least eaten
pancakeMin = pancake;
iMin = i;
}
}
cout << "The number of most eaten pancakes is " << pancakeMax << " by Person#" << iMax << endl;
cout << "The number of least eaten pancakes is " << pancakeMin << " by Person#" << iMin << endl;
return 0;
}
So Im writing a program that asks the user to input the number of pancakes a person(1-10) had for breakfast. The program must analyze the input and determine which person ate the most pancakes. Also the program must output a list in order of number of pancakes eaten of all 10 people. So far I have written the code to get the user input and the code to display the array, but not in order. Im completely lost when it comes to comparing the elements in the array:
int getPancakes();
void displayArray(int theArray[],int sizeOfArray);
void compareArray(int sizeOfArray);
int pancakes[10];
int z = 0;
int main()
{
}
int getPancakes(){
int y;
int x = 0;
for(int y = 0; y < 10; y++){
++x;
cout << "How many pancakes did person " << x << " eat?" << endl;
cin >> pancakes[y];
}
}
void displayArray(int theArray[],int sizeOfArray){
for(int x = 0 ;x < sizeOfArray ; x++){
++z;
cout << "Person " << z << " ate " << pancakes[x] << " pancakes" << endl;
}
}
So how can I instruct my program to compare the elements inside the array? Also how can I instruct my program to print the list of number of pancakes eaten by each person in order?
In order to find who ate the most pancakes, you basically need to find the position of the maximum value in the array.
int findMaxPosition(int array[], int arraySize){
int maxPosition = 0; //assume the first element is maximum
for(int i = 1; i < arraySize; i++)
if(array[i] > array[maxPosition]) //compare the current element with the known max
maxPosition = i; //update maxPosition
return maxPosition;
}
Note that this gives you the first occurence of the maximum value. If the elements are unique, that's enugh. Otherwise, you should find the maximum value, array[maxPosition], and iterate through the array and display every position on which it occurs.
Sorting is a little bit complicated. The sorting algorithms aren't so straightforward, and I'm afraid that if I write you an implementation, I wouldn't help you.
One of the simplest sorting algorithms is bubble sort. Wikipedia (http://en.wikipedia.org/wiki/Bubble_sort) has a detailed page about it, and you should be able to implement it using the pseudocode given there.
If all numbers are unique
int max = 0;
for(int x = 0 ;x < 10 ; x++)
{
if(pancakes[x] > max)
max = pancakes[x];
}
for(int x = 0 ;x < 10 ; x++)
{
if(pancakes[x] == max)
cout << "Person " << x << " ate " << pancakes[x] << " pancakes - biggest number" << endl;
}
To be blunt, there are two methods for comparing an element of an array to another value, direct and through a copy.
// Make a copy:
int count = pancakes[x];
if (count == limit)
{
//...
}
// Direct access
if (pancakes[x] == limit)
{
//...
}
There is no need to run through the array looking for max then sorting to provide output. Instead, you can just keep track of the max value you have found during the input phase:
int getPancakes(){
int max = 0;
for(int y = 0; y < 10; y++){
cout << "How many pancakes did person " << y << " eat?" << endl;
cin >> pancakes[y];
if (pancakes[y]>pancakes[max]){
max = y;
}
}
return max;
}
Note that I removed the redundant declaration of y (you are declaring it in the for loop) and x (was always going to be equal to y).
I also added a return to the function (index of the person who has eaten the most) as you had no return value. (Either return something or make the function return void (no return)).
If you only care about the max number eaten, then you don't even need to keep track of max. Instead, just read the largest value from the array after the sorting step.
Now all you need to do is implement void sortArray() and call it before calling the display function:
int main()
{
int max = getPancakes();
sortArray(); //left to you to implement
displayArray(pancakes,10);
}
You may want to consider making pancakes local to main and passing it into your functions in the same way that you are doing displayArray.
I just finished coming up with a solution to the same exercise, regarding the "who ate the most" part, even if there is more than one person who ate the most pancakes. It involves arrays and vectors. Tested and working:
#include <iostream>
#include <array>
#include <vector>
using namespace std;
int main()
{
array <int, 10> PancakeEater;
vector<int> Winners;
int max;
cout << "Type number of pancakes eaten, separated by spaces, for each of the ten contestants:" << endl;
cout << "#1 #2 #3 #4 #5 #6 #7 #8 #9 #10" << endl;
for (int i = 0; i < PancakeEater.size(); i++)
{
cin >> PancakeEater[i];
}
max = PancakeEater[0];
Winners.push_back(1);
for (int i = 0; i < PancakeEater.size()-1; i++)
{
if (max == PancakeEater[i + 1])
{
Winners.push_back(i + 2);
}
if (max < PancakeEater[i + 1])
{
max = PancakeEater[i + 1];
Winners.clear();
Winners.shrink_to_fit();
Winners.push_back(i + 2);
}
}
if (Winners.size() > 1)
{
cout << endl << "The contestants that ate the most pancakes, on a tie for first place, are contestant numbers " << endl;
for (auto item : Winners)
{
cout << item << " ";
}
}
else
{
cout << endl << "The contestant that ate the most pancakes is contestant number " << endl;
for (auto item : Winners)
{
cout << item << " ";
}
}
}
So Im doing exercise where I have to ask the user to input the number of pancakes eaten by 10 people, print out who ate the most pancakes and then organize the list from greatest to smallest, basically I have to print out:
Person 3: Ate 10 pancakes , Person 5: Ate 9 pancakes ,Person 8: Ate 8 pancakes
However after using bubble sort I cant match the person with the right value, because bubble sort swaps them! Does anyone know of any way to fix this? For example is there another way to organize the values in an array without using bubble sort?
void getPancakes()
{
int x = 0;
int temp;
for(int y = 0; y < 10; y++)
{
++x;
cout << "How many pancakes did person " << x << " eat?" << endl;
cin >> pancakes[y];
}
}
void displayArray(int theArray[],int sizeOfArray)
{
int temp;
int i,j;
int q = 10;
for(i = 0; i <= sizeOfArray - 1 ; i++)
{
for(j = i+1 ; j < sizeOfArray ; j++)
{
if(theArray[i] < theArray[j])
{
temp = theArray[i];
theArray[i] = theArray[j];
theArray[j] = temp;
}
}
}
cout << endl;
for(i = 0; i < 10; i++)
cout << "Person " << i+1 << " ate " << theArray[i] << " pancakes" << endl;
}
Like Cody said, you'll have to store both values. As #HgMs indicated you could you a class, or a struct, which is a class with only public data members. Here's an example:
struct person{
int id;
int pancakes;
};
int main(){
const int totalPeople = 10;
person personArray[totalPeople];
for (int i = 0; i < totalPeople; ++i){
cout << "How many pancakes did person " << i << " eat? ";
personArray[i].id = i;
cin >> personArray[i].pancakes;
}
for (int i = 0; i < totalPeople; ++i){
cout << "id: " << personArray[i].id << "\t" << "Pancakes eaten: " << personArray[i].pancakes << endl;
}
return 0;
}
You can then iterate over the array and look at the number of pancakes each person has eaten using the '.' to access the property.
EDIT: A bubble sort would work fine.