I'm practicing arrrays and in the following program I'm passing a multidimensional array to a function to fill it. This worked with a single dimensional array just fine.

(1) Is this bad practice?
(2) Do the multi-D arrays work the same way as the regular ones. In the program below I'm sending a 3X7 array to a function to get data. I expected it to be passed by reference and fill the array, for some reason that's not happening - could someon please explain?

#include <iostream>
#include <iomanip>

using namespace std;

// ------ PROTOTYPES ------
void getData(double [][7], int, int);
double averageMonkeyFeed(double [][7], int, int);
void highestLowestMonkeyFeed(double [][7], int, int, double &, double &);
void displayResults(double, double, double);

// ****** MAIN ********************************************************************
int main()
{
    int monkeyIndex = 3;
    int dayIndex = 7;
    double monkeyFeed[3][7];

    double highest = 0;
    double lowest = 0;
    double average = 0;

    getData(monkeyFeed, monkeyIndex, dayIndex);

// Check to see if actually getting any data
     for(int x = 0; x < monkeyIndex; x++)
     {
        for(int y = 0; y < dayIndex; y++)
        {
            cout << endl << "[" << x << "]" << "[" << y << "] ";
            cout << "*** monkeyFeed " << monkeyFeed[x][y] << "***\n";
        }
     }


  highestLowestMonkeyFeed(monkeyFeed, monkeyIndex, dayIndex, highest, lowest);
  average = averageMonkeyFeed(monkeyFeed, monkeyIndex, dayIndex);
  displayResults(average, highest, lowest);
    return 0;
}

// ----- FUNCTION Display Reults --------------------------------------------------
// takes all data from main then displays results in a report
// --------------------------------------------------------------------------------
void displayResults(double average, double highest, double lowest)
{
    cout << "\n------ RESULTS -------\n";
    cout << "\nHighest: \n" << highest;
    cout << "\nLowest: \n" << lowest;
    cout << "\nAverage: \n" << average;

}

// ----- FUNCTION Highest Lowest --------------------------------------------------
// Takes 3 X 7 array and uses a nested for loop to to find the higfhest and lowest
// feed amounts - then uses the reference property of the array to update
// --------------------------------------------------------------------------------
void highestLowestMonkeyFeed(double monkeyFeed[][7], int monkeyIndex, int dayIndex, double &highest, double &lowest)
{

    highest = monkeyFeed[0][0];
    lowest  = monkeyFeed[0][0];

    for(int x = 0; x < monkeyIndex; x++)
    {
        for(int y = 0; y < dayIndex; y++)
        {
            if(monkeyFeed[x][y] > highest)
            {
                highest = monkeyFeed[x][y];
            }

            if(monkeyFeed[x][y] < lowest)
            {
                lowest = monkeyFeed[x][y];
            }

        }
    }

    return;
}

// ----- FUNCTION Average ---------------------------------------------------------
// Takes 3 X 7 array and uses a nested for loop to find the average of the feed
// amounts. Then returns average value.
// --------------------------------------------------------------------------------
double averageMonkeyFeed(double monkeyFeed[][7], int monkeyIndex, int dayIndex)
{

    double average = 0;

     for(int x = 0; x < monkeyIndex; x++)
    {
        for(int y = 0; y < dayIndex; y++)
        {
            average += monkeyFeed[x][y];
        }
    }

    average = average / dayIndex;
    return average;
}

// ----- FUNCTION Get Data --------------------------------------------------------
// Takes 3 X 7 array and uses a nested for loop to fill each week monkey by monkey
// --------------------------------------------------------------------------------
void getData(double monkeyFeed[][7], int monkeyIndex, int dayIndex)
{
    double sum = 0;
    for(int x = 0; x < monkeyIndex; x++)
    {
        cout << "\n------ MONKEY " << x + 1 << " ----------------------\n";
        for(int y = 0; y < dayIndex; y++)
        {
            cout << "\nHow much did monkey " << x + 1 << " eat on day " << y + 1 << "? ";
            cin >> monkeyFeed[monkeyIndex][dayIndex];
            sum += monkeyFeed[monkeyIndex][dayIndex];
            while(monkeyFeed[monkeyIndex][dayIndex] < 0)
            {
                cout << "Number must be 0 or larger";
                cout << "\nHow much did monkey " << x + 1 << " eat on day " << y + 1 << "? ";
                cin >> monkeyFeed[monkeyIndex][dayIndex];
            }

        }
    }
    return;
}

Sorry - just found the stupid bug - I used the indexes instead of the loop counters in assigning the 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.