Im having a problem with this program that uses a multidimensional array. Everything works as expected except the minimum amount of food always spits out a massive negative number regardless of what the greatest input was.
heres a description of the problem:
A local zoo wants to keep track of how many pounds of its three monkeys eats each day during a typical week. Write a program that stores this information in a two dimensional 3 x 7 array, where each row represents a different monkey and each column represents a different day of the week. The program should first have the user input the data for each monkey. Then it should create a report that includes the following information:
- Average amount of food eaten per day by the whole family of monkeys.
- The least amount of food eaten during the week by any one monkey.
- The greatest amount of food eaten during the week by any one monkey.
#include<iostream>
usingnamespace std;
int main()
{
constint MONKEYS = 3;
constint DAYS = 7;
int food[MONKEYS][DAYS];
int maximum = food[0][0];
int minimum = food[0][0];
float total = 0.0f;
float average = 0.0f;
cout << "Enter the amount of food consumed for each monkey on 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 += food[monk][day];
}
cout << endl;
}
//calculate average
average = total / (MONKEYS * DAYS);
//get most eaten
for (int monk = 0; monk < MONKEYS; monk++)
{
for (int day = 0; day < DAYS; day++)
{
if (food[monk][day] > maximum)
maximum = food[monk][day];
}
}
//get least eaten
for (int monk = 0; monk < MONKEYS; monk++)
{
for (int day = 0; day < DAYS; day++)
{
if (food[monk][day] < minimum)
minimum = food[monk][day];
}
}
cout << " Report" << endl;
cout << "-----------------------------" << endl;
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;
system("PAUSE");
return 0;
}
int food[MONKEYS][DAYS];
int maximum = food[0][0];
int minimum = food[0][0];
1 2
if (food[monk][day] > maximum)
maximum = food[monk][day];
1 2
if (food[monk][day] < minimum)
minimum = food[monk][day];
You set your max and min values from uninitialized array value therefore they are undefined themselves. Then you use them in an expression with their still undefined values therefore the expression is undefined.
EDIT : The only reason maximum worked is because the undefined value happened to meet the requirements of the if statement, else it would have failed also.
//get most eaten
for (int monk = 0; monk < MONKEYS; monk++)
{
for (int day = 0; day < DAYS; day++)
{
if (food[monk][day] > maximum)
maximum = food[monk][day];
}
}
//get least eaten
minimum = maximum; // <- This line is important!
for (int monk = 0; monk < MONKEYS; monk++)
{
for (int day = 0; day < DAYS; day++)
{
if (food[monk][day] < minimum)
minimum = food[monk][day];
}
}
Before you calculate the minimum amount you have to add this line of code:
minimum = maximum;
Because when you initialized minimum you set it equal to food[0][0] which wasn't initialized in itself, hence the large negative number.
I'd recommend initializing both maximum and minimum with 0 and then later on assigning minimum to maximum.
still bad they are both undefined there is no guarantee that maximum will get set properly either so setting minimum = to maximum is not helping. But setting them both to zero will fix this. Assuming food eaten is more than zero which it should be or this would be an animal welfare violation
still bad they are both undefined there is no guarantee that maximum will get set properly...
I'd recommend initializing both maximum and minimum with 0 and then later on assigning minimum to maximum.
I don't see a flaw in setting minimum to maximum in any case, because minimum is always going to be less or equal to maximum. Provided (of course), that maximum was initialized to 0.
Of course, if you wanted to get really picky, you could #include <limits> and init minimum and maximum like so:
1 2
int minimum = std::numeric_limits<int>::max();
int maximum = std::numeric_limits<int>::min();