This is to calculate how much 3 monkeys eat in a week.
it generates the total amount eaten by all the monkeys, the least amount eaten by a monkey and the most amount eaten by a monkey.
The issue is the least (min) and most (max) eaten seem to be not correct.
I think it may be how I am initiating the variables but I am not sure.
#include<iostream>
#include<string>
using namespace std;
#include<iostream>
using namespace std;
int main()
{
const int MONKEYS = 3;
const int DAYS = 7;
int food[MONKEYS][DAYS];
int maximum = 0;
int minimum = 0;
float total = 0.0f;
float average = 0.0f;
cout << "please enter the amount each monkey ate each day " << endl;
//get data from user and sum it
for (int monk = 0; monk < MONKEYS; monk++)
{
for (int day = 0; day < DAYS; day++)
{
cout << "Monkey " << (monk + 1) << ", day " << (day + 1) << ": ";
cin >> food[monk][day];
total =total + food[monk][day];
maximum = food[0][0];
minimum = food[0][0];
if (food[monk][day] > maximum)
{
maximum = food[monk][day];
}
if (food[monk][day] < minimum)
{
minimum = food[monk][day];
}
}
cout << endl;
}
average = total / (MONKEYS * DAYS);
cout << endl;
cout << "The total amount of food consumed is " << total << endl;
cout << "The average amount of food consumed by the monkeys each day is " << average << endl;
cout << "The most eaten by a monkey is " << maximum << endl;
cout << "The least eaten by a monkey is " << minimum << endl;
okay
thank you
I can set the min to some insane number and the max to 0 before the loop but I was thinking there may be a way to set it as the first input to start then change it.
So I tried that but then they get set to some weird negative number where the array is stored which causes my minimum to be that number throughout the program. The max will still work though but not the min.
Ah, I missed that you read the values from the user in the same loop. Well, you could either split it up into two loops. One that reads input and one that looks for the max and min. Another alternative is to initialize maximum to the lowest possible value (std::numeric_limits<int>::lowest()) and minimum to the highest possible value (std::numeric_limits<int>::max()) before the loop.
Yeah, I didn't know there was a way to set the minimum to the highest possible value without knowing what the highest possible value is.
I did think about doing them in different loops and possibly making them different functions but it makes more sense to me if I do it all in one loop. I might do different functions next time it seems to be easier to read that way.