Member Avatar for Nick6425fl

I'll post the question below but I can't get the smallest and largest to print. I'm not even sure I have the math correct to calculate them because I can't get results to print.

A local zoo wants to keep track of how many pounds of food each of its three monkeys
eats each day during a typical week. Write a program that stores this information
in a two-dimensional 3 × 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.
Input Validation: Do not accept negative numbers for pounds of food eaten.

Any help would be great!

#include<iostream>
#include <iomanip>
using namespace std;

// Constants
const int MONKEYS = 3;
const int DAYS = 7;

//function prototypes
double familyAverage(const double mf[MONKEYS][DAYS]);
double littleMeal(const double mf[MONKEYS][DAYS]);
double bigMeal(const double mf[MONKEYS][DAYS]);


int main()
{ 
   
    double monkey_table [MONKEYS][DAYS]={0};
    int x, y;                     
   
   cout << "Three monkeys are in a zoo and this program will tell us ";
   cout << "how much food they eat on average. We will also ";
   cout << "tell you the largest and smallest meal they consumed.\n\n";

   for (x = 0; x < MONKEYS; x++)
   {
      for (y = 0; y < DAYS; y++)
      {
        do
        {        
            cout << "Monkey " << x+1<<" ";
            cout << "Day " << y+1 <<" ";
            cin >> monkey_table [x][y];

         if(monkey_table [x][y] < 0)
            cout<<" Please enter a positive number for pounds of food."<<endl; 

         }
         while(monkey_table [x][y] < 0);
      
      }
      

   }
    cout << showpoint << fixed << setprecision(2) << endl;
    cout << "Average amount of food eaten per day by the whole family of monkeys is "<<familyAverage(monkey_table)<<endl;
    
    system ("pause"); 
    
   return 0;
}


double familyAverage(const double mf[MONKEYS][DAYS])
{
    double total;

    for (int x = 0; x < MONKEYS; x++)
            for (int y = 0; y < DAYS; y++)
                total += mf[x][y];
            total= (total / DAYS)/3;
            
    return total;
}


double littleMeal(const double mf[MONKEYS][DAYS])
{
    double small= INT_MAX;

    for (int x = 0; x < MONKEYS; x++)
            for (int y = 0; y < DAYS; y++)
                if (mf[x][y] > small)
                    small= mf[x][y];
                    
        cout << "The smallest meal is " << small << endl;    
    return small;
}


double bigMeal(const double mf[MONKEYS][DAYS])
{
    double big= INT_MAX;

    for (int x = 0; x < MONKEYS; x++)
            for (int y = 0; y < DAYS; y++)
                if (mf[x][y] < big)
                    big= mf[x][y];
                    
            cout << "The biggest meal is " << big << endl;
            
           
    return big;
}

Three things.
In the average function, you must initialize total to be 0, otherwise you are adding the pounds consumed to some random value.

In your min and max functions, set the min or max variable to the first value found in the data. Using an arbitrary initial value (like INT_MAX) may not always be correct for the given data set. In the case of the max function, you should have used, at the minimum, a negative number, since only positive values should be in the data set.

Your comparison directions are backwards in both the min and max functions.

And, as a general note, your min/max functions return the found value. That should be displayed in main, like you do the average. Don't do display in calculating functions, except for debugging statements that you will later remove.

commented: Wonderful Help!! +1

Before you start calculation for average, max and min, you should always set the value to something. For example, you declared double total in your familyAverage. Now this variable called double points to a block of memory. This block of memory may contain some data in it that is irrelevant to what you are coding here. It's called garbage value. So when you initialize these variables but don't set them to a value, it's some random number. And as you try to calculate, you are adding mf[x][y] to this garbage value which is not what you want.

double familyAverage(const double mf[MONKEYS][DAYS])
{
    double total;

    for (int x = 0; x < MONKEYS; x++)
            for (int y = 0; y < DAYS; y++)
                total += mf[x][y];
            total= (total / DAYS)/3;
            
    return total;
}

Your calculation here is wrong. what you are doing here is you get total value and then you divide by Days and 3 for MONKEYS amount of time. When you take average, you divide by Days and 3 only one time.

double littleMeal(const double mf[MONKEYS][DAYS])
{
    double small= INT_MAX;

    for (int x = 0; x < MONKEYS; x++)
            for (int y = 0; y < DAYS; y++)
                if (mf[x][y] > small) // this statement says if mf[x][y] is bigger than small, then small is mf[x][y]. Is that what you really want? You want mf[x][y] to be smaller.
                    small= mf[x][y];
                    
        cout << "The smallest meal is " << small << endl;    
    return small;
}
 
//and the same calculation error for finding max

double bigMeal(const double mf[MONKEYS][DAYS])
{
    double big= INT_MAX;

    for (int x = 0; x < MONKEYS; x++)
            for (int y = 0; y < DAYS; y++)
                if (mf[x][y] < big)
                    big= mf[x][y];
                    
            cout << "The biggest meal is " << big << endl;
            
           
    return big;
}

i hope that helps.

Your calculation here is wrong. what you are doing here is you get total value and then you divide by Days and 3 for MONKEYS amount of time. When you take average, you divide by Days and 3 only one time.

Look at his code again. The division is not actually in the loops, although the indentation makes it seem that way at a quick glance.

Member Avatar for Nick6425fl

In the average function, you must initialize total to be 0, otherwise you are adding the pounds consumed to some random value.

double total = 0 ;

I think this is right.

Now for the other directions its hard for me because this is my first c++ class and I get so confused when it comes to functions and passing the values.

I'm thanking all in advance that are willing to take the time to try and help me out. I'll make another attempt at your corrections and try to repost the code.

Yep, that's right. One down, just a couple more small changes to make.

Member Avatar for Nick6425fl

Vmanes I can't do it. I've been trying for 2 hours and nothing. Every time I try to move it to the main it gives me 15 errors. and the comparisons for the min and max i'll look at once I get the code in line. Any additional help would be great. I'm sure I'll look back at this post months down the road and laugh.

What are those errors that you are getting? And what is it that you are trying to move to main, cout?

Member Avatar for Nick6425fl

just trying to display min and max.

are you trying to do something like:

cout << "minimum amount of food eaten is " << littleMeal( monkey_table ) << endl;

You had the right idea for displaying the average - build upon what you've seen works.

your first problem is an assertion when you run the code, your debugger should tell you exactly where the problem is (its in familyAverage func). Second problem is a logic problem with bigMeal(). Take a look at those 2 functions and try to track down the problem

Member Avatar for Nick6425fl

are you trying to do something like:

cout << "minimum amount of food eaten is " << littleMeal( monkey_table ) << endl;

You had the right idea for displaying the average - build upon what you've seen works.

That displays the min and max!!!! Hours trying to figure out 2 lines. I see what you mean with the comparisons being backwards...working on those now.

Member Avatar for Nick6425fl

I fixed the calculations and it works great. Thank you to everybody who helped, but vamnes thank you especially... you saved the day.

Using this post and my book I managed to get this far but I can't get the code to calculate the lowest value. Do you see my mistake?

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    double averageWhole = 0;
    int monk, day;
    
    const int NUM_DAYS = 7;
    const int NUM_MONKS = 3;
    double average[NUM_MONKS][NUM_DAYS],
        lowest,
        highest;    
            
    cout << "This program will calculate the average weekly consumption , the highest";
    cout << "amount eaten, and the lowest amount eaten of the three monkeys at the zoo.\n\n";
    cout << "Enter the amount of food eaten for the monkey and day specified:\n\n";            
            

for (monk = 0; monk < NUM_MONKS; monk++)
    {
        for (day = 0; day < NUM_DAYS; day++)
        {
          cout << "Monkey " << (monk + 1);
          cout << ", Day " << (day + 1) << ": ";
          cin >> average[monk][day];  
          
          if (average[monk][day] < 0)
          {
          cout << " \nNegative pound values are not possible.\n";
          cout << "Please run the program again and enter a positive integer.\n";
          while (average[monk][day] < 0);
          }
           
                  
        }
     cout << endl;
     }
          
for (monk = 0; monk < NUM_MONKS; monk++)
    {
        for (day = 0; day < NUM_DAYS; day++)
           averageWhole += average[monk][day] / NUM_DAYS;
    }                                            

for (monk = 0; monk < NUM_MONKS; monk++)
    {
        for (day = 0; day < NUM_DAYS; day++)    
            {
            if ((average[monk][day]) > highest)
            highest = average[monk][day];
            }
    }    

for (monk = 0; monk < NUM_MONKS; monk++)
    {
        for (day = 0; day < NUM_DAYS; day++)
            {
            if ((average[monk][day]) < lowest)
            lowest = average[monk][day];
            }                 
    }                               

cout << fixed << showpoint << setprecision(2);
cout << " Average amount: " << averageWhole << endl;
cout << " Lowest amount: " << lowest << endl; 
cout << " Highest amount: " << highest << endl;

cout << endl;
system("PAUSE");
return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.