So i am back again, working with arrays this time. I can get it to run in the main but i am not sure how to make this code into a void function .

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;




int main ()
{        // Declare variables and initialize
        const int monkeytot = 3;
        const int Wdays = 7;
        int monkeys;
        int days;
        int most = 0;
        int least = 0;
        int total = 0;
        double average = 0;
        double totaleaten;
double food [monkeytot][Wdays];
// start program


// Fill array with numbers
        for (monkeys = 0; monkeys < monkeytot; monkeys++)
        {
            for (days = 0; days < Wdays; days++)
            {
                cout << " Enter pounds of food eaten by monkey "  << (monkeys + 1)<< ", Day " << (days + 1) << ": ";
                cin >> food[monkeys][days];
                cout << food[monkeys][days];
            }
            cout << endl;
        }
return 0;
}

i tried this: but when it compiles it says food not declared in scope. I have put food[monkeys][Wdays] with and without the double, but i am not sure how to call the function in int main. the book i am using doesn't really explain for 2d arrays

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

void monkeyfood ()
{ // Declare variables and initialize

        const int monkeytot = 3;
        const int Wdays = 7;
        int monkeys;
        int days;
        double food[monkeys][Wdays];
// Fill array with numbers
        for (monkeys = 0; monkeys < monkeytot; monkeys++)
        {
            for (days = 0; days < Wdays; days++)
            {
                cout << " Enter pounds of food eaten by monkey "  << (monkeys + 1)<< ", Day " << (days + 1) << ": ";
                cin >> food[monkeys][days];
                cout << food[monkeys][days];
            }
            cout << endl;
        }
};



int main ()
{        // Declare variables and initialize
        const int monkeytot = 3;
        const int Wdays = 7;
        int monkeys;
        int days;
        int most = 0;
        int least = 0;
        int total = 0;
        double average = 0;
        double totaleaten;

// start program


}

Actualy just noticed in your first bit of code you have this:

double food [monkeytot][Wdays];

But then in your second bit you have this:

double food[monkeys][Wdays];

Try changing that.

err fat fingers. thanks for pointing that out. I got the program to ask for the amount of food for each day, but i am not sure how print it out so it say
monkey sun - rest of the weekdays
number of monkeys and amount
i can only seem to figure out the code for it

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;


    const int monkeytot = 3;        //number of monkeys
    const int Wdays = 7;            // Days of the week
void foodeaten(double food[monkeytot][Wdays]);
void dispfoodeaten (double food[monkeytot][Wdays]);

int main ()
{        // Declare variables and initialize

        double food[monkeytot][Wdays];      // array to stor information    
        int monkeys;                    
        int days;
        int most = 0;
        int least = 0;
        int total = 0;
        double average = 0;

        foodeaten(food);
        dispfoodeaten(food);
// start program

        return 0;
}
void foodeaten(double food[monkeytot][Wdays])
{// Fill array with numbers
        int monkeys;                    
        int days;
        for (monkeys = 0; monkeys < monkeytot; monkeys++)
        {
            for (days = 0; days < Wdays; days++)
            {
                cout << " Enter pounds of food eaten by monkey "  << (monkeys + 1)<< ", Day " << (days + 1) << ": ";
                cin >> food[monkeys][days];
                cout << food[monkeys][days];
            }
            cout << endl;
        }
};
void dispfoodeaten(double food[monkeytot][Wdays])
{// Sum all food eaten (array elements) by all monkeys
        int total = 0;
        double average = 0;
        for (int monkeys = 0; monkeys < monkeytot; monkeys++)
        {
            for (int days = 0; days < Wdays; days++)
            total += food[monkeys][days];           
        }
 for (moneky = 0, monkey < monkeytot, monkey ++)
        {cout<< "Moneky"<<(monkey + 1);
            {
}
}
}
    // Average amount of food eaten per day by all monkeys
        average = total / 21;
        cout    << "The total amount of food eaten (in pounds) is: " << total << " pounds." << endl;    
        cout << "The average amount of food eaten (in pounds) by all monkeys is: " << setw(4) << fixed << setprecision(1) << average << " pounds." << endl;
}

You may fine this little edit/clean_up of your code ... a useful example for your future coding problems ...

Note: little typos in your code will cause your code to NOT compile.

For example:

for (moneky = 0, monkey ...
What is the typo above?

// monkey_food.cpp //


#include <iostream>
#include <iomanip>
#include <string>

using namespace std;


const int MONKEY_TOT = 3; // number of monkeys
const int NUM_DAYS = 2;   // KEEP small for testing, change later //


void takeInFoodEaten( double food[MONKEY_TOT][NUM_DAYS] );
void dispFoodEaten( double food[MONKEY_TOT][NUM_DAYS] );




int main ()
{
    double food[MONKEY_TOT][NUM_DAYS]; // array to store information

    // You are NOT using these !!! //
    /*
    int monkeys;
    int days;
    int most = 0;
    int least = 0;
    int total = 0;
    double average = 0;
    */

    takeInFoodEaten(food);

    // rest of program done here //
    dispFoodEaten(food);

    return 0;
}





void takeInFoodEaten(double food[MONKEY_TOT][NUM_DAYS])
{// Fill array with numbers
    for( int monkey = 0; monkey < MONKEY_TOT; monkey++ )
    {
        for( int day = 0; day < NUM_DAYS; day++ )
        {
            cout << "Enter pounds of food eaten by monkey "
                 << (monkey + 1) << ", day " << (day + 1) << ": ";
            cin >> food[monkey][day];
            cout << "You entered : " << food[monkey][day] << endl;
        }
        cout << endl;
    }
}


void dispFoodEaten( double food[MONKEY_TOT][NUM_DAYS] )
{// Sum all food eaten (array elements) by all monkeys

    int total = 0, monkeyDayMealCount = 0;
    double average = 0;

    for( int monkeys = 0; monkeys < MONKEY_TOT; monkeys++ )
    {
        for( int days = 0; days < NUM_DAYS; days++ )
        {
            total += food[monkeys][days];
            ++monkeyDayMealCount;
        }
    }

    cout << "The total weight of food eaten by "
         << MONKEY_TOT << " monkeys in " << monkeyDayMealCount
         << " (monkey) days was: " << total << " pounds.\n";

    if( monkeyDayMealCount != 0 ) // make sure NOT to divide by ZERO !!! //
        average = double(total) / monkeyDayMealCount;

    cout << fixed << setprecision(1);

    cout << "The average weight of food consumed/day by each monkey was: "
         << setw(4) << average << " pounds." << endl;
}

Late comment to add:

(It is too late now to edit the above code.)

// It might be better to just a double for the total ? //
// Be aware of the potential problem in the result when you divide
// an integer value by an other integer value !!! //

Late comment to add:

(It is too late now to edit the above code.)

// It might be better to just use a double for the total ? //
// Be aware of the potential problem in the result when you divide
// an integer value by an other integer value !!! //

Thank you for you help.

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.