// // arrayexample.cpp // // program to demonstrate passing multidimensional arrays // into and out of functions, and working with them using // nested for loops // #include #include // Maximum array size const int NUMROWS = 10; const int NUMCOLS = 20; // Function prototypes void GetRowsAndCols(int& rows, int& cols); void ReadArray(int rows, int cols, int A[][NUMCOLS]); void WriteArrayRows(int rows, int cols, const int A[][NUMCOLS]); void WriteArrayCols(int rows, int cols, const int A[][NUMCOLS]); void main() { int rows, cols; int A[NUMROWS][NUMCOLS]; GetRowsAndCols(rows, cols); cout << endl; ReadArray(rows, cols, A); cout << endl; cout << "Row by row output: " << endl; WriteArrayRows(rows, cols, A); cout << endl << endl; cout << "Column by column output: " << endl; WriteArrayCols(rows, cols, A); cout << endl; } // // void GetRowsAndCols(int& rows, int& cols) // // Purpose: get the number of rows and columns to use from the array // requires a valid value for rows and for columns // // Parameters: // rows and cols: output the number of rows and columns input by user // void GetRowsAndCols(int& rows, int& cols) { // get a valid value for rows do { cout << "How many rows do you want to fill in? " << "(0 < rows <= " << NUMROWS << ") "; cin >> rows; } while (rows < 1 || rows > NUMROWS); // get a valid value for columns do { cout << "How many columns do you want to fill in? " << "(0 < rows <= " << NUMCOLS << ") "; cin >> cols; } while (cols < 1 || cols > NUMCOLS); } // // void ReadArray(int rows, int cols, int A[][NUMCOLS]) // // Purpose: read in values from a user to fill in // a rows X cols section of an array // // Parameter: // rows, cols: the number of rows and columns of the array // to fill in // A: array to contain (and return) the values read in // void ReadArray(int rows, int cols, int A[][NUMCOLS]) { for (int i = 0; i < rows; i++) { cout << "Enter " << cols << " numbers for row number " << i << " "; for (int j = 0; j < cols; j++) cin >> A[i][j]; } } // // void WriteArrayRows(int rows, int cols, const int A[][NUMCOLS]) // // Purpose: print out a rows X cols section of an array // row by row // // Parameter: // rows, cols: the number of rows and columns of the array // to output // A: array to output // void WriteArrayRows(int rows, int cols, const int A[][NUMCOLS]) { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) cout << A[i][j] << " "; // Did you remember the space? cout << endl; // Did you remember the newline? } } // // void WriteArrayCols(int rows, int cols, const int A[][NUMCOLS]) // // Purpose: print out a rows X cols section of an array // column by column // // Parameter: // rows, cols: the number of rows and columns of the array // to output // A: array to output // void WriteArrayCols(int rows, int cols, const int A[][NUMCOLS]) { for (int i = 0; i < cols; i++) { // For each column.... for (int j = 0; j < rows; j++) // For each row of the column... cout << A[j][i] << " "; // Did you remember the space? cout << endl; // Did you remember the newline? } }