140 likes | 278 Views
CMPT 128: Introduction to Computing Science for Engineering Students. Introduction to 2-D Arrays. Matrices. A matrix or two-dimensional array is a set of number arranged in a grid with rows and columns. A matrix is defined using a type declaration statement.
E N D
CMPT 128: Introduction to Computing Science for Engineering Students Introduction to 2-D Arrays
Matrices • A matrix or two-dimensional array is a set of number arranged in a grid with rows and columns. A matrix is defined using a type declaration statement. • typearray_name[num_rows][num_columns] • type array_name[length_column][length_row] • int matrix[3][4]; • double mice[7][9]; • char courselist[4][32];
Matrices • int matrix[3][4]; row[0] row[1] row[2] in memory row0 row1 row2 row0 matrix[0][0] matrix[0][1] matrix[0][2] matrix[0][3]
Matrices • int matrix[3][4]; Row[0][0] Row[0][1] Row[0][2] Row[0][3] Row[1][0] Row[1][1] Row[1][2] Row[1][3] Row[2][0] Row[2][1] Row[2][2] Row[2][3] in memory Row[2][3] Row[0][3] Row[1][0] Row[1][1] Row[1][2] Row[1][3] Row[2][0] Row[2][1] Row[2][2] Row[0][0] Row[0][1] Row[0][2]
Initializing 2-D arrays • double myarray [3][5] = { { 1.0, 2.3, 3.5, 4.2 ,5.1}, { 0.1, 1.2, 2.3, 3.4, 4.2}, { 9.9, 8.8, 7.7, 6.6, 5.5}}; int yourarray[2][3] = { 1,2,3,4,5,6}; • Allocates enough space for a 2-D array myarray with 3 rows and 5 columns. • Allocate enough space for a 2-D array yourarray with 2 rows and 3 columns • Think of myarray an array of 3 arrays of length 5
Initializing 2-D arrays int myarray [5][4] = { { 1, 3, 5, 4 }, { 7, 4, 3, 0} }; int thisarray[6] [2] = { 1,4, 9,7, 4,1, 0,0 }; • Allocates enough space for myarray a 2-D array with 5 rows and 4 columns, and for thisarray a 2-D array with 6 rows and 2 columns • The initial values given for myarray will fill the first two rows of myarray, the remainder of the array will be filled with zeros • The intial values given for thisarray will fill the first four rows of thisarray, the remainder of the array will be filled with zeros
Initializing 2-D arrays int myarray [ ][4] = { { 1, 3, 5, 4 }, { 7, 4, 3, 0} }; int thisarray[ ] [2] = { 1,4, 9,7, 4,1, 0,0 }; • Allocates enough space for myarray a 2-D array with 2 rows and 4 columns, and for thisarray a 2-D array with 4 rows and 2 columns • Think of myarray as an array of 2 arrays of length 4, and thisarray as an array of 4 arrays of length 2 • When the number of rows is not given in the declaration, the number of rows is determined by the number of elements initialized in the declaration
INVALID: Initializing 2-D arrays int myarray [ ][ ] = { { 1, 3, 5, 4 }, { 7, 4, 3, 0} }; int thisarray[ 4][ ] = { {1,4}, {9,7}, {4,1}, {0,0} }; • Think of the 2-D array as an array of 1-D arrays (rows) • Must know the length of each 1-D array (row) to determine where the next 1-D array (row) begins • The second pair of square brackets contains the number of elements in a row • Thus, the 2nd pair of square brackets cannot be left empty
Initializing 2-DArrays for(i=0; i<NROWS; i++) { for(k=0; k<NCOLS; k++) { myarrayt[ i ][ k ] = 10; } } • For efficient initialization or evaluation be sure that elements are initialized or evaluated in the order they occur in memory. Along row 0 then along row1 and so on • The loop over each column is the inner loop, the loop over each row is the outer loop.
Accessing Array Elements • int matrix[3][4]; • matrix has 12 integer elements • matrix[0][0] element in first row, first column • Matrix[2][3] element in third row, fourth column • matrix[2][3] element in last row, last column
Choosing array sizes: parameters • When calling a function with a 2-D array as an argument the choice of a variable should be compatible with the argument • If the call is of the form double funct1 ( myarray[NCOLS][NROWS] ); double arrayInMain[NCOLS][NROWS]; answer = funct1(arrayInMain); • pass in an array as an argument that has the same maximum row length (NCOLS) as the parameter of the function • Pass in an array as an argument that has the same maximum column length (NROWS)
2-DArrays as Function Parameters void addconst(int b[NROWS][NCOLS], int bt[NROWS][NCOLS], int usedRows, int usedCols, int const) { /* Declare Variables. */ int i, k; for(i=0; i<usedRows; i++) { for(k=0; k<usedCols; j++) { bt[ i ][ k ] += 1; } } return; }
Choosing array sizes: parameters • When calling a function with a 2-D array as an argument the choice of a variable should be compatible with the argument • If the call is of the form double funct1 ( myarray[ ][NCOLS] ); double arrayInMain[NROWS][NCOLS]; answer = funct1(arrayInMain); • pass in an array as an argument that has the same maximum row length (NCOLS) as the parameter of the function • The argument can have any maximum column length (NROWS) and the function will still work properly
2-DArrays as Function Parameters void addconst(int b[ ][NCOLS], int bt[ ][NCOLS], int usedRows, int const) { /* Declare Variables. */ int i, k; for(i=0; i<UsedRows; i++) { for(k=0; k<usedCols; j++) { bt[ i ][ k ] += 1; } } return; }