Hello everybody,

I am having trouble with the following. In my code, I don't get the right results for my getAverage/ getLeastAmount/ getGreatestAmount function. I don't know what I am doing wrong. Could anybody give me some hints, please?

#include <iostream>
#include <iomanip>
#include <cassert>
#include <fstream>

using namespace std;

const int monkey = 3;
const int days = 7;
int row=0;
int col=0;

void getInput(int FoodForMonkey[][days]);
void getTotal(int FoodForMonkey[][days]);
void getAverage (int FoodForMonkey[][days]);
void getLeastAmount (int FoodForMonkey[][days]);
void getGreatestAmount (int FoodForMonkey[][days]);
void InitializeArray (int FoodForMonkey[][days]);


int main()
{
    int FoodForMonkey[monkey][days];
    
    getInput (FoodForMonkey);
    InitializeArray(FoodForMonkey);
    getAverage (FoodForMonkey);
    getLeastAmount (FoodForMonkey);
    getGreatestAmount (FoodForMonkey);   
    
    system ("pause");
    return 0;
}

//**************************************
void getInput(int FoodForMonkey[][days])
{
     int input;
     
     for (int row=0; row < monkey; row++)
     {
         for (int col=0; col < days; col++)
         {
             cout << "Monkey " <<row+1 <<" for day "
             << col+1 << " = ";
             cin >> FoodForMonkey[row][col];
             
             if (FoodForMonkey[row][col]<0)
             {cout << "Amount of food cannot be negative. Try again."
             << endl;
             return;}
     }   }
}

//**************************************

void InitializeArray(int FoodForMonkey[][days])
{
    for (int row=0; row < monkey; row++)
    {
        for (int col=0; col < days; col++)
        {
         FoodForMonkey[row][col]=col+1;
        }
    }   
}

//**************************************
void getAverage (int FoodForMonkey[][days])
{
    
    for (int col=0; col < days; col++)
    {
        float numTotal=0;
        cout << "Average amount eaten on day " << col+1;
        for (int row=0; row < monkey; row++)
        {
         numTotal=numTotal + FoodForMonkey[row][col]; 
        }
        cout << " is " << float(numTotal/3) << endl;
    }                
}

//**************************************
void getLeastAmount (int FoodForMonkey[][days])
{   
    int leastAmount=0;
    
    for (int row=0; row < monkey; row++)
    {
        //int leastAmount=0;
        for (int col=0; col < days; col++)
        {
        if (FoodForMonkey[row][col] > FoodForMonkey[row][col+1])
            leastAmount=FoodForMonkey[row][col];
        }
     cout << "Least amount consumed is " << leastAmount << endl; 
     }          
}

//**************************************
void getGreatestAmount (int FoodForMonkey[][days])
{
    int FoodForMonkey2[row][col];
    FoodForMonkey2[row][col]= FoodForMonkey[monkey][days];
    int greatestAmount;
    
    for (int row=0; row < monkey; row++)
    {
        int greatestAmount=0;
        for (int col=0; col < days; col++)
        {
        if (FoodForMonkey2[row][col] < FoodForMonkey2[row][col+1])
        greatestAmount=FoodForMonkey2[row][col];
        }
     }
     cout << "Greatest amount consumed is " << greatestAmount;      
}

//**************************************

Recommended Answers

All 9 Replies

I think the problem is here:

void getAverage (int FoodForMonkey[][days])
{
    
    for (int col=0; col < days; col++)
    {
        float numTotal=0;
        cout << "Average amount eaten on day " << col+1;
        for (int row=0; row < monkey; row++)
        {
              //Since you are calculating a daily average, 
              //you should clear this variable each loop iteration 
              numTotal += FoodForMonkey[row][col]; 
              //You might also want to dedicate a variable to keep a running total of all days
              //if you want to calculate a total average
              total_ave += FoodForMonkey[row][col];
        }
        cout << " is " << float(numTotal/3) << endl;
        //Clear 'numTotal' in preperation for the next day's calculation
        numTotal = 0.00f;
    }                
}

Thank you, that makes sense (and works). :)

Now for my getLeastAmount function, I still don't get the output I want. This is what I have now:

void getLeastAmount (int FoodForMonkey[][days])
{   
    cout << "Least Amount per Monkey: " <<endl<<endl;
    
    int leastAmount=0;
    
    for (int row=0; row < monkey; row++)
    {
        leastAmount=0;
        
        for (int col=0; col < days-1; col++)
        {
            if (FoodForMonkey[row][col] > FoodForMonkey[row][col+1])
            leastAmount=FoodForMonkey[row][col+1];
        }
        cout << row+1 <<". Monkey " << " = " << leastAmount<< endl; 
     }   
        cout<<endl;     
}

Any suggestions?!?

In your algorithm, you are continuously updating 'leastamount' based on the result of 2 vars being tested. Therefore, it will never truly hold the lest amount of the array, but will only hold the result of the last test performed.

What you should do, is introduce a third variable that will be assigned a value only at such time that it qualifies as the least amount, else increment the array and proceed to the next test.

Sorry to interject as you are focused on the other functions, but how come you prompt the user for input and then overwrite the values in InitializeArray? (I'm probably missing something obvious...)

Well, I thought you always have to initialize anything first, so you don't get any "junk" values. I also thought since I am initializing first in the main function, and then call the getInput function, that I am not overwriting the values.
I could be completely wrong, though. I am new at this (obviously) and taking the class online. This is just my logic.

Regarding what Clinton Portis was saying:

I am not sure how to do that. I tried to put an if-statement after the second for loop, but that doesn't do anything. I am not even sure if my loops are correct at all. The longer I look at it, the more confused I get...

It is a good habit to initialize
But you are taking input first and initializing second

int main()
{
    int FoodForMonkey[monkey][days];
    
    getInput (FoodForMonkey);
    InitializeArray(FoodForMonkey);

Oh sorry, my bad. I fixed that in the meantime. Here is an updated code

#include <iostream>
#include <iomanip>
#include <cassert>
#include <fstream>

using namespace std;

const int monkey = 3;
const int days = 7;
int row=0;
int col=0;

void getInput(int FoodForMonkey[][days]);
void getTotal(int FoodForMonkey[][days]);
void getAverage (int FoodForMonkey[][days]);
void getLeastAmount (int FoodForMonkey[][days]);
void getGreatestAmount (int FoodForMonkey[][days]);
void InitializeArray (int FoodForMonkey[][days]);


int main()
{
    int FoodForMonkey[monkey][days];
    
    InitializeArray(FoodForMonkey);
    getInput (FoodForMonkey);
    getAverage (FoodForMonkey);
    getLeastAmount (FoodForMonkey);
    getGreatestAmount (FoodForMonkey);   
    
    system ("pause");
    return 0;
}

//**************************************
void getInput(int FoodForMonkey[][days])
{
     int input;
     
     for (int row=0; row < monkey; row++)
     {
         for (int col=0; col < days; col++)
         {
             cout << "Monkey " <<row+1 <<" for day "
             << col+1 << " = ";
             cin >> FoodForMonkey[row][col];
             
             if (FoodForMonkey[row][col]<0)
             {cout << "Amount of food cannot be negative. Try again."
             << endl;
             return;}
         }  
         cout << endl; 
     }
}

//**************************************

void InitializeArray(int FoodForMonkey[][days])
{
    for (int row=0; row < monkey; row++)
    {
        for (int col=0; col < days; col++)
        {
         FoodForMonkey[row][col];
        }
    }   
}

//**************************************
void getAverage (int FoodForMonkey[][days])
{
    float totalAvg=0;
    cout << "Average amount: " << endl << endl;
    for (int col=0; col < days; col++)
    {
        float numTotal=0;
        cout << "Day " << col+1;
        for (int row=0; row < monkey; row++)
        {
         numTotal=numTotal + FoodForMonkey[row][col]; 
         totalAvg = totalAvg + FoodForMonkey[row][col];
        }
        cout << " = " << float(numTotal/3)<<endl;
        numTotal=0;
    }           
    cout << endl;     
}

//**************************************
void getLeastAmount (int FoodForMonkey[][days])
{   
    cout << "Least Amount per Monkey: " <<endl<<endl;
    
    int leastAmount;
    int leastAmountTemp=0;
    
    for (int row=0; row < monkey; row++)
    {
        leastAmount=0;
        //leastAmountTemp=0;
        for (int col=0; col < days-1; col++)
        {
            if (FoodForMonkey[row][col] > FoodForMonkey[row][col+1])
            leastAmount=FoodForMonkey[row][col+1];   
        }
        //if (leastAmount)
        //leastAmountTemp; 
        cout << row+1 <<". Monkey " << " = " << leastAmountTemp << endl;
     }
      
        cout<<endl;     
}

//**************************************
void getGreatestAmount (int FoodForMonkey[][days])
{
    cout << "Greatest Amount per Monkey: " <<endl<<endl;
    
    int greatestAmount;
    
    for (int row=0; row < monkey; row++)
    {
        greatestAmount=0;
        
        for (int col=0; col < days+1; col++)
        {
            if (FoodForMonkey[row][col] < FoodForMonkey[row][col-1])
            greatestAmount=FoodForMonkey[row][col-1];
        }
        cout << row+1 <<". Monkey " << " = " << greatestAmount<< endl; 
     }   
        cout<<endl;   
}

//**************************************

Ok ok.. I'll do it for you. No thinking, effort, learning or attempt at trying required on your part:

for (int row=0; row < monkey; row++)
{
        leastAmount=0;
        
        for (int col=0; col < days-1; col++)
        {
            if(FoodForMonkey[row][col] > FoodForMonkey[row][col+1])
                 if(leastAmount > FoodForMonkey[row][col+1])
                 {
                       leastAmount = FoodForMonkey[row][col+1];                    
                 }
            cout << row+1 <<". Monkey " << " = " << leastAmount<< endl; 
        }
void getLeastAmount (int FoodForMonkey[][days])
{   
    cout << "Least Amount per Monkey: " <<endl<<endl;
    
    int leastAmount;
    int leastAmountTemp=0;
    
    for (int row=0; row < monkey; row++)
    {
        leastAmount=FoodForMonkey[row][0];
        //leastAmountTemp=0;
        for (int col=1; col < days; col++)
        {
            if (FoodForMonkey[row][col] < leastAmount)
                  leastAmount=FoodForMonkey[row][col];   
        }
        //if (leastAmount)
        //leastAmountTemp; 
        cout << row+1 <<". Monkey " << " = " << leastAmountTemp << endl;
     }
      
        cout<<endl;     
}

EDIT: CP beat me to it.

I just took the lowest to be the first value in the row and compared it from there. I think that's what you were originally trying to do. Now your results should be comparable to what you were looking for (or at least not all your init values)

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.