Unformatted text preview:

Matrices and 2-D ArraysExample#include <iostream>#include <iomanip>using namespace std;int main(){const int M = 5;const int N = 7;int A[M][N];int B[M][N] = { { 11, 12, 13, 14 },{ 21, 22, 23, 24 },{ 31, 32, 33, 34 } };int m = 3, n = 4;for (int i = 0; i < m; ++i){for (int j = 0; j < n; ++j)cout << setw(4) << B[i][j] << ’ ’; //See ALT.cout << endl;}return 0;}/* Output:11 12 13 1421 22 23 2431 32 33 34*/// ALT: cout << setw(4) << *(*(B+i)+j) << ’ ’;1Matrices and 2-D Arrays— Just like a 1-D array, except that you need twoindices to specify an element: A[i][j] is the variablein “row i” and “column j.”— Filled from the “upper left” corner toward the ”lowerright” corner. E.g., B[][] above is visualized as fol-lows.11 12 13 14 ?? ?? ??21 22 23 24 ?? ?? ??31 32 33 34 ?? ?? ???? ?? ?? ?? ?? ?? ???? ?? ?? ?? ?? ?? ??— An “array of arrays,” stored contiguously by rowin C++. E.g., B[][7] above is stored as follows.11 12 13 14 ?? ?? ?? 21 22 23 24 ?? ?? ?? 31 32 ...— Pointers to pointers. Type int [ ][N] is the sameas int (*)[N] and is more specific than int**. Givenan array A defined as int A[M][N] ...;,A[i][j] means *(A[i] + j) or *(*(A+i)+j)or A[0][i*N + j] ,where N is the number of columns of storage in A.A[i][j] is the ijth element.A[i] is the address of A[i][0].A is the address of A[0].— When used as a function parameter, a 2-D array’scolumn dimension must be specified explicitly.int [][5] is not the same type as int [][7].2Example: Generate an N × N multiplication table andstore it in a 2-D array.#include <iostream>#include <iomanip>#include <cassert>using namespace std;const int N_ROWS_A = 20; // max number of rows of dataconst int N_COLS_A = 20; // max number of columns of datavoid makeMultTable( int A[][ N_COLS_A ], int N ){if ( N <= N_COLS_A && N <= N_ROWS_A)for (int i=0; i<N; ++i)for (int j=0; j<N; ++j)A[i][j] = (i+1)*(j+1);elsecerr << "Error in makeMultTable(): table size "<< "exceeds array dimensions.\n\n";}3void printTable( int A[][ N_COLS_A ], int N,ostream& os=cout, int fieldWidth=5 ){if ( N <= N_COLS_A && N <= N_ROWS_A)for (int i=0; i<N; ++i){for (int j=0; j<N; ++j)os << setw(fieldWidth) << A[i][j] << ’ ’;os << endl;}elsecerr << "Error in printTable(): table size "<< "exceeds array dimensions.\n\n";}int main(){static int A[ N_ROWS_A ][ N_COLS_A ];cout << "Enter table size N (N <= "<< N_COLS_A << "): ";int N; cin >> N;assert (N > 0 && N <= 20);makeMultTable( A, N );printTable( A, N );return 0;}/* Sample I/O:Enter table size N (N <= 20): 41 2 3 42 4 6 83 6 9 124 8 12


View Full Document

UCLA COMSCI 31 - Matrices and 2-D Arrays Example

Download Matrices and 2-D Arrays Example
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Matrices and 2-D Arrays Example and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Matrices and 2-D Arrays Example 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?