CMPT 102 Introduction to Scientific Computer Programming Introduction

  • Slides: 14
Download presentation
CMPT 102 Introduction to Scientific Computer Programming Introduction to 2 -D Arrays 10/26/2020 ©

CMPT 102 Introduction to Scientific Computer Programming Introduction to 2 -D Arrays 10/26/2020 © Janice Regan, CMPT 102, Sept. 2006 0

Matrices ] A matrix or two-dimensional array is a set of number arranged in

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. S type array_name[num_rows][num_columns] S type array_name[length_column][length_row] S int matrix[3][4]; S double mice[7][9]; S char courselist[4][32]; © Janice Regan, CMPT 102, Sept. 2006 1

Matrices S int matrix[3][4]; row[0] row[1] row[2] in memory row 0 row 1 row

Matrices S int matrix[3][4]; row[0] row[1] row[2] in memory row 0 row 1 row 2 row 0 matrix[0][0] matrix[0][1] matrix[0][2] matrix[0][3] © Janice Regan, CMPT 102, Sept. 2006 2

Matrices S 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]

Matrices S 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[2][2] Row[2][1] Row[2][0] Row[1][3] Row[1][2] Row[1][1] Row[1][0] Row[0][3] Row[0][2] Row[0][1] Row[0][0] © Janice Regan, CMPT 102, Sept. 2006 3

Initializing 2 -D arrays ] double myarray [3][5] = { { 1. 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 © Janice Regan, CMPT 102, Sept. 2006 4

Initializing 2 -D arrays int myarray [5][4] = { { 1, 3, 5, 4

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 © Janice Regan, CMPT 102, Sept. 2006 5

Initializing 2 -D arrays int myarray [ ][4] = { { 1, 3, 5,

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 © Janice Regan, CMPT 102, Sept. 2006 6

INVALID: Initializing 2 -D arrays int myarray [ ][ ] = { { 1,

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) S Must know the length of each 1 -D array (row) to determine where the next 1 -D array (row) begins S The second pair of square brackets contains the number of elements in a row S Thus, the 2 nd pair of square brackets cannot be left empty © Janice Regan, CMPT 102, Sept. 2006 7

Initializing 2 -DArrays for(i=0; i<NROWS; i++) { for(k=0; k<NCOLS; k++) { myarrayt[ i ][

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 row 1 and so on The loop over each row is the inner loop, the loop over each column is the outer loop. © Janice Regan, CMPT 102, Sept. 2006 8

Accessing Array Elements ] int matrix[3][4]; S matrix has 12 integer elements S matrix[0][0]

Accessing Array Elements ] int matrix[3][4]; S matrix has 12 integer elements S matrix[0][0] element in first row, first column S matrix[2][3] element in last row, last column S matrix is the address of the first element S &matrix[0][0] is the address of the first element S matrix[1] is the address of the second row © Janice Regan, CMPT 102, Sept. 2006 9

Choosing array sizes: parameters ] When calling a function with a 2 -D array

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 funct 1 ( myarray[NCOLS][NROWS] ); double array. In. Main[NCOLS][NROWS]; answer = funct 1(array. In. Main); S S 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) © Janice Regan, CMPT 102, Sept. 2006 10

2 -DArrays as Function Parameters void addconst(int b[NROWS][NCOLS], int bt[NROWS][NCOLS], int used. Rows, int

2 -DArrays as Function Parameters void addconst(int b[NROWS][NCOLS], int bt[NROWS][NCOLS], int used. Rows, int used. Cols, int const) { /* Declare Variables. */ int i, k; for(i=0; i<used. Rows; i++) { for(k=0; k<used. Cols; j++) { bt[ i ][ k ] += 1; } } return; } © Janice Regan, CMPT 102, Sept. 2006 11

Choosing array sizes: parameters ] When calling a function with a 2 -D array

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 funct 1 ( myarray[ ][NCOLS] ); double array. In. Main[NROWS][NCOLS]; answer = funct 1(array. In. Main); S S 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 © Janice Regan, CMPT 102, Sept. 2006 12

2 -DArrays as Function Parameters void addconst(int b[ ][NCOLS], int bt[ ][NCOLS], int used.

2 -DArrays as Function Parameters void addconst(int b[ ][NCOLS], int bt[ ][NCOLS], int used. Rows, int const) { /* Declare Variables. */ int i, k; for(i=0; i<Used. Rows; i++) { for(k=0; k<used. Cols; j++) { bt[ i ][ k ] += 1; } } return; } © Janice Regan, CMPT 102, Sept. 2006 13