// Example 2-2 - 2D Arrays and Functions #include #include using namespace std; const int Cols = 4; void print2dArray(int [][Cols], int rows); void print1ElementOf2dArray(int element); void print1RowOf2dArray(int row[]); void print1ColumnOf2dArray(int [][Cols], int rows, int col); int main() { const int Rows = 3; int a[Rows][Cols] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}}; // print the array print2dArray(a,Rows); // Print the second row cout << "The second row" << endl; print1RowOf2dArray(a[1]); // Print the second element in the third row cout << "The second element in the third row" << endl; print1ElementOf2dArray(a[2][1]); // Print the third column cout << "The third column" << endl; print1ColumnOf2dArray(a, Rows, 2); return 0; } void print2dArray(int A[][Cols], int rows) { for (int i = 0; i < rows; i++) { for (int j = 0; j < Cols; j++) cout << setw(3) << A[i][j]; cout << endl; } cout << endl; } void print1ElementOf2dArray(int element) { cout << setw(3) << element << endl << endl; } void print1RowOf2dArray(int A[]) { for (int i = 0; i < Cols; i++) cout << setw(3) << A[i]; cout << endl; } void print1ColumnOf2dArray(int A[][Cols], int rows, int col) { for (int i = 0; i < rows; i++) { cout << setw(3) << A[i][col] << endl; } cout << endl; }