Download presentation
Presentation is loading. Please wait.
1
© Janice Regan, CMPT 102, Sept. 2006 0 CMPT 102 Introduction to Scientific Computer Programming Introduction to 2-D Arrays
2
© Janice Regan, CMPT 102, Sept. 2006 1 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. type array_name[num_rows][num_columns] type array_name[length_column][length_row] int matrix[3][4]; double mice[7][9]; char courselist[4][32];
3
© Janice Regan, CMPT 102, Sept. 2006 2 Matrices int matrix[3][4]; row[0] row[1] row[2] in memory row0row1row2 matrix[0][0]matrix[0][1]matrix[0][2] matrix[0][3] row0
4
© Janice Regan, CMPT 102, Sept. 2006 3 Matrices int matrix[3][4]; in memory Row[0][0] Row[1][0] Row[2][0] Row[0][1] Row[1][1] Row[2][1] Row[0][2] Row[1][2] Row[2][2] Row[0][3] Row[1][3] Row[2][3] Row[1][0] Row[2][0] Row[1][1] Row[2][1] Row[0][2] Row[1][2] Row[2][2] Row[0][3]Row[1][3] Row[2][3] Row[0][1] Row[0][0]
5
© Janice Regan, CMPT 102, Sept. 2006 4 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
6
© Janice Regan, CMPT 102, Sept. 2006 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
7
© Janice Regan, CMPT 102, Sept. 2006 6 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
8
© Janice Regan, CMPT 102, Sept. 2006 7 INVALID: Initializing 2-D arrays int myarray [ ][ ] = { { 1, 3, 5, 4 }, { 7, 4, 3, 0} }; int thisarray[ 3][ ] = { {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 2 nd pair of square brackets cannot be left empty
9
© Janice Regan, CMPT 102, Sept. 2006 8 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 row is the inner loop, the loop over each column is the outer loop.
10
© Janice Regan, CMPT 102, Sept. 2006 9 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 last row, last column matrix is the address of the first element &matrix[0][0] is the address of the first element matrix[1] is the address of the second row
11
© Janice Regan, CMPT 102, Sept. 2006 10 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)
12
© Janice Regan, CMPT 102, Sept. 2006 11 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; }
13
© Janice Regan, CMPT 102, Sept. 2006 12 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
14
© Janice Regan, CMPT 102, Sept. 2006 13 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; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.