// Faan Tone Liu // Comp 1672 // 1/15/2003 // Demo program for comp 1672 // This program shows various aspects of two-dimensional arrays #include using namespace std; const int num_rows(3); // This is the number of rows in my array, and it // corresponds to the number of students const int num_cols(4); // This is the number of columns in my array, and it // corresponds to the number of exams given void print_array(int arr[][num_cols]); int main() { int i, j; // Declare and initialize array that holds the exam scores for all students int testscores[num_rows][num_cols] = { {95, 97, 93, 96}, // Scores for first student {86, 0, 82, 88}, // Scores for second student {69, 78, 85, 91} }; // Scores for third student // example of how to access array; output the entire last row // which corresponds to all the scores for one student cout << "Here are the scores for the last student: "; for (i = 0; i < num_cols; i++) cout << testscores[2][i] << " "; cout << endl; // Now let's output the entire 3rd column, which corresponds to the // scores on the third exam cout << "Here are the scores for the 3rd exam: " << endl; for (i = 0; i < num_rows; i++) cout << testscores[i][2] << endl; // Here's an example of how to pass an array to a function: cout << "Here are all of the scores:" << endl; print_array(testscores); // Here's how we would set all values of the array to 0. // This example shows how to traverse the entire array. for (i=0; i < num_rows; i++) { for (j=0; j < num_cols; j++) { testscores[i][j] = 0; } } // Here's an example of how to pass an array to a function: cout << "Here are all of the scores:" << endl; print_array(testscores); return 0; } // This function outputs the values in the array, assumes the array is // num_rows by num_cols void print_array(int arr[][num_cols]) { int i, j; for (i=0; i < num_rows; i++) { for (j=0; j < num_cols; j++) cout << arr[i][j] << " "; cout << endl; } }