How to pass a 2D array to another function in C++?

Here the code
void results()
{
double storeScores;
for (int ii = 0; ii < NUM_ROWS; ii++)
{
cout << "Please enter 9 Numbers that you want to input for group" << ii + 1 << endl;

for (int jj = 0; jj < NUM_COLS; jj++)
{
cout << jj + 1 << " | ";
cin >> storeScores;
scores[ii][jj] = checkInput(storeScores);
}
}
}

double checkInput(double aScores){
if(aScores > 20 || aScores < 0){
cout << "Invalid value! Enter score again:" << endl;
cin >> aScores;
}
return aScores;
}

void printscores()
{
cout << " " <<endl;
cout << " " <<endl;
cout << "Sample Output:" << endl;
cout << "-----------------------------------" << endl;
cout << "pair before tr. after tr." << endl;
cout << "-----------------------------------" << endl;
}


Can ya help me to pass the array from results to printscores. i've been trying to figure this out all day

?2009-10-04T06:32:56Z

Favorite Answer

Here is how you can create and initialize your double ** on the heap. Use i.e. double **scores = GetDouble(NUM_ROWS, NUM_COLS) to create your array. The following prototype declares a parameter of your array: double fct(double **d). I hope that helps.

double **GetDouble(int iRows, int iCols)
{
double **dbl = new double*[iRows];
for (int i = 0; i < iRows; i++){
dbl = new double[iCols];
for (int j = 0; j < iCols; j++){
dbl[i][j]=0.0f;
}
}
return dbl;
}

Paul2009-10-04T13:38:23Z

Change:

void printscores()

to:

void printscores(double scores[NUW_ROWS][NUM_COLS])

mpchatty2009-10-04T02:27:56Z

Pass a pointer to the 2nd method. :)