Hi everyone, I have been teaching myself c++ for about two weeks now, I love learning new stuff about it and I am finding the best way to make concepts stick for me, is to write example programs. I found a thread on here about beginner challenges, but I am stuck on one part of the challenge.
I have to basically, use an array to store how many pancakes each of 10 people ate. After that I need it to read the results, then I need it to tell me which person ate the most pancakes.
So far I have the program able to store the 10 peoples pancakes, read the results, and tell me the greatest amount of pancakes eaten. But I cannot figure out how to have to the program tell me which person ate the most pancakes.
My code is below (please correct any bad habits or anything as well, as I want to nip any of those in the bud, before I start learning more advanced topics. Thanks!
Hi Cody thanks for the answer! I tried your solution, unfortunately since the loop runs ten times, person always ends up being equal to ten. But I think you may have pushed me in the right direction, by using another variable to track the people. Before I was thinking along the lines of, find the greatest number and record what Array element that number was in, but I am not sure if thats possible? Atleast without a more comprehensive understanding of c++ (which I don't have yet).
Hey guys, I figured it out, it was basically what Cody said, I guess my code earlier was kind of messy and it didn't work out as expected, but I cleaned everything up and finally got it working. Check it out below, if you guys spot any room for improvement please let me know, I want to avoid bad habits before getting to the more advanced stuff.
#include <iostream>
usingnamespace std;
int main()
{
int array[10];
for (int i = 0; i < 10; i++)
{
cout << "Enter how many pancakes person " << i + 1 << " has eaten ";
cin >> array[i];
cin.ignore();
}
cout << "\n";
for (int i = 0; i < 10; i++)
{
cout << "Person " << i + 1 << " ate " << array[i] << " pancakes";
cout << "\n";
}
int most = array[0];
int least = array[0];
int personm;
int personl;
for (int i = 0; i < 10; i++)
{
if (array[i] > most)
{
most = array[i];
personm = i + 1;
}
elseif (array[i] < least)
{
least = array[i];
personl = i + 1;
}
}
cout << "\n";
cout << "The most amount of pancakes eaten was " << most << " by person " << personm << endl;
cout << "The least amount of pancakes eaten was " << least << " by person " << personl << endl;
cin.get();
return 0;
}