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.
This is what i got so far
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
usingnamespace std;
int main()
{
int person[10]; //stores how many pancakes eaten
int numberOfPeople = 0; //used to count from person 1-10 when inputting
cout << "Enter the number of pancakes eaten by the 10 people\n" << endl;
for(int i = 0; i < 10; i++)
{
numberOfPeople++;
cout << "Person " << numberOfPeople << ": ";
cin >> person[i];
}
return 0;
}
I don't know how i find the biggest number in the array.
Would really appreciate some help.
This piece of code will help you (I'll explain it):
1 2 3 4
int Largest( 0 );
for( short i( 0 ); i < 10; i++ )
if( Person[i] > Largest )
Largest = Person[i];
Here, a for loop is created that is set to loop 10 times. With each pass of the loop, the ith element of Person is compared to the current value stored in Largest. If the value within Largest is less than the value within the ith element of Person, Largest is assigned the value of the ith element of Person. At the end of the loop, Largest will contain the largest value within the Person array.
do a for loop after that that checks person[i] against person[i+1]
so like your array is: [5, 9, 2, 3, 8, 10, 4, 1, 7, 6] {This is person 0- 9 and how many pancakes they ate... what the user would have inputted randomly or something}
you say something like int biggest;if person[0 or i] > p[i+1] then person 0 = biggest. and check it against all of them and you should have the largest number.
Oh and i highly recommend the next star as well - the one to sort them all from least to greatest or vice. It might be tough but well worth it. :)
#include <iostream>
usingnamespace std;
int main()
{
int person[10]; //stores how many pancakes eaten
int numberOfPeople = 0; //used to count from person 1-10 when inputting
cout << "Enter the number of pancakes eaten by the 10 people\n" << endl;
for(int i = 0; i < 10; i++)
{
numberOfPeople++;
cout << "Person " << numberOfPeople << ": ";
cin >> person[i];
}
int largest = 0;
for(int i = 0; i < 10; i++)
if(person[i] > largest)
largest = person[i];
cout << largest << endl;
return 0;
}
I now have the largest number. But i also have to figure which element(person) in the array that has that number.
Is there any way to do this?
I'm not quite sure what you mean. Do you mean this: Once the largest number is found, print the ith index to the screen? If so, then you may want to consider this:
1 2 3 4 5 6 7 8 9 10 11 12
int largest = 0;
int LargestIndex( 0 );
for(int i = 0; i < 10; i++)
if(person[i] > largest)
{
largest = person[i];
LargestIndex = i;
}
std::cout << "The largest value entered was " << Largest << "." << std::endl;
std::cout << "The largest number is at index: " << LargestIndex << "." << std::endl;
Here, LargestIndex store the index of the element which contains the largest value.
#include <iostream>
usingnamespace std;
int main()
{
int person[10]; //stores how many pancakes eaten
int numberOfPeople = 0; //used to count from person 1-10 when inputting
cout << "Enter the number of pancakes eaten by the 10 people\n" << endl;
for(int i = 0; i < 10; i++)
{
numberOfPeople++;
cout << "Person " << numberOfPeople << ": ";
cin >> person[i];
}
int largest = 0;
int largestPerson = 0;
for(int i = 0; i < 10; i++)
if(person[i] > largest)
{
largest = person[i];
largestPerson = i;
}
int smallest = 100000;
int smallestPerson = 0;
for(int i = 0; i < 10; i++)
if(person[i] < smallest)
{
smallest = person[i];
smallestPerson = i;
}
largestPerson++; //to get index 0 = person 1
smallestPerson++;
cout << "\nPerson " << largestPerson << " ate the most pancakes. Which was: " << largest << endl;
cout << "\nPerson " << smallestPerson << " ate the least pancakes. Which was: " << smallest << endl;
return 0;
}
Don't know if it is the best way to find the smallest number, but couldn't think of any other way.