Multidimensional Arrays as Function Arguments
-
Because of the way a multidimensional array is handled in memory,
when it is used as a parameter of a function,
the parameter type appearing in the function prototype and
in the header of the function definition
must contain a specification for the size of each dimension
except the first one.
-
The example here illustrates what we have learned
about multidimensional arrays.
//Reads quiz scores for each student into the two-dimensional array named
//grade (but the input code is not shown in this display). Computes the
//average score for each student and the average score for each quiz.
//Displays the quiz scores and the averages.
#include <iostream>
#include <iomanip>
using namespace std;
const int NUMBER_STUDENTS = 4, NUMBER_QUIZZES = 3;
void compute_st_ave(const int grade[ ][NUMBER_QUIZZES], double st_ave[ ]);
//Precondition: Global constant NUMBER_STUDENTS and NUMBER_QUIZZES are
//the dimensions of the array grade.
//Each of the indexed variables grade[st_num-1, quiz_num-1] contains the
//score for student st_num on quiz quiz_num.
//Postcondition: Each st_ave[st_num-1] contains the average for student
//number stu_num.
void compute_quiz_ave(const int grade[ ][NUMBER_QUIZZES], double quiz_ave[ ]);
//Precondition: Global constant NUMBER_STUDENTS and
//NUMBER_QUIZZES are the dimensions of the array grade. Each of
//the indexed variables grade[st_num-1, quiz_num-1] contains the
//score for student st_num on quiz quiz_num.
//Postcondition: Each quiz_ave[quiz_num-1] contains the average for
//quiz numbered quiz_num.
void display(const int grade[ ][NUMBER_QUIZZES],
const double st_ave[ ], const double quiz_ave[ ]);
//Precondition: Global constant NUMBER_STUDENTS and
//NUMBER_QUIZZES are the dimensions of the array grade. Each of the
//indexed variables grade[st_num-1, quiz_num-1] contains the score for
//student st_num on quiz quiz_num. Each st_ave[st_num-1] contains the
//average for student stu_num. Each quiz_ave[quiz_num-1] contains the
//average for quiz numbered quiz_num.
//Postcondition: All the data in grade, st_ave, and quiz_ave have been
output.
int main( )
{
int grade[NUMBER_STUDENTS][NUMBER_QUIZZES];
double st_ave[NUMBER_STUDENTS];
double quiz_ave[NUMBER_QUIZZES];
cout << "I am filling the array grade for you.\n";
grade[0][0] = 10; grade[0][1] = 10; grade[0][2] = 10;
grade[1][0] = 2; grade[1][1] = 0; grade[1][2] = 1;
grade[2][0] = 8; grade[2][1] = 6; grade[2][2] = 9;
grade[3][0] = 8; grade[3][1] = 4; grade[3][2] = 10;
compute_st_ave(grade, st_ave);
compute_quiz_ave(grade, quiz_ave);
display(grade, st_ave, quiz_ave);
return 0;
}
void compute_st_ave(const int grade[ ][NUMBER_QUIZZES], double st_ave[ ])
{
for (int st_num = 1; st_num <= NUMBER_STUDENTS; st_num++)
{ //Process one st_num:
double sum = 0;
for (int quiz_num = 1; quiz_num <= NUMBER_QUIZZES; quiz_num++)
sum = sum + grade[st_num-1][quiz_num-1];
//sum contains the sum of the quiz scores for student number st_num.
st_ave[st_num-1] = sum/NUMBER_QUIZZES;
//Average for student st_num is the value of st_ave[st_num-1]
}
}
void compute_quiz_ave(const int grade[ ][NUMBER_QUIZZES],
double quiz_ave[ ])
{
for (int quiz_num = 1; quiz_num <= NUMBER_QUIZZES; quiz_num++)
{ //Process one quiz (for all students):
double sum = 0;
for (int st_num = 1; st_num <= NUMBER_STUDENTS; st_num++)
sum = sum + grade[st_num-1][quiz_num-1];
//sum contains the sum of all student scores on quiz number quiz_num.
quiz_ave[quiz_num-1] = sum/NUMBER_STUDENTS;
//Average for quiz quiz_num is the value of quiz_ave[quiz_num-1]
}
}
//Uses iostream and iomanip:
void display(const int grade[ ][NUMBER_QUIZZES],
const double st_ave[ ], const double quiz_ave[ ])
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(1);
cout << setw(10) << "Student" << setw(5) << "Ave"
<< setw(15) << "Quizzes\n";
for (int st_num = 1; st_num <= NUMBER_STUDENTS; st_num++)
{//Display for one st_num:
cout << setw(10) << st_num << setw(5) << st_ave[st_num-1] << " ";
for (int quiz_num = 1; quiz_num <= NUMBER_QUIZZES; quiz_num++)
cout << setw(5) << grade[st_num-1][quiz_num-1];
cout << endl;
}
cout << "Quiz averages = ";
for (int quiz_num = 1; quiz_num <= NUMBER_QUIZZES; quiz_num++)
cout << setw(5) << quiz_ave[quiz_num-1];
cout << endl;
}