#include <iostream>
usingnamespace std;
int eaten [10], i; // declaring variables
int main ()
{
cout << "How many pancakes did the following persons eat?" << endl << endl; // getting the user to input the data
for (i=0; i<=9; i++)
{
cout << "Person " << i << ": ";
cin >> eaten[i];
}
int max = eaten[0];
for (i = 1; i<=9; i++) // calculating maximum
{
if (max < eaten[i])
max = i;
}
int min = eaten[0];
for (i = 0; i<=9; i++) // calculating minimum
{
if (min > eaten[i])
min = i;
}
cout << endl << "The person who ate the most pancakes was person: " << max << endl << endl; // printing final results
cout << endl << "The person who ate the least pancakes was person: " << min << endl << endl;
system("PAUSE");
return 0;
}
My problem is that when I run the code, the minimum never says person 0 has the least and the same for the maximum. It says the amount typed in instead of the person's number. I know this is because I declare each one as "eaten[0]" which enables the rest of the "for" function to work, but I don't know any other way around it.
Here lies the problem. Originally, 'max' was declared to hold the number of pancakes that person 0 ate and here you are changing it to represent the index of the person. You need to make another variable that represents the index of the most pancakes and least pancakes.
1 2 3 4 5 6
if (max < eaten[i]) {
max = eaten[i];
most_index = i;
}
cout << "The person who at the most was Person #" << most_index << endl;
#include <iostream.h>
int eaten [10], i; // declaring variables
int main ()
{
cout << "How many pancakes did the following persons eat?" << endl << endl; // getting the user to input the data
for (i=0; i<=9; i++)
{
cout << "Person " << i+1 << ": ";
cin >> eaten[i];
}
int pos_max=1,pos_min=1;
int max = eaten[0];
for (i = 1; i<=9; i++) // calculating maximum
{
if (max < eaten[i])
{
max = eaten[i];
pos_max=i+1;
}
}
int min = eaten[0];
for (i = 0; i<=9; i++) // calculating minimum
{
if (min > eaten[i])
{
min = eaten[i];
pos_min=i+1;
}
}
cout << endl << "The person who ate the most pancakes was person: " << pos_max << " with "<<max<<" pancakes"<< endl << endl; // printing final results
cout << endl << "The person who ate the least pancakes was person: " << pos_min << " with "<<min<<" pancakes"<< endl << endl;
cin.get();
return 0;
}