Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topic 9C – Multiple Dimension Arrays. CISC105 – Topic 9C Multiple Dimension Arrays A multiple dimension array is an array that has two or more dimensions.

Similar presentations


Presentation on theme: "Topic 9C – Multiple Dimension Arrays. CISC105 – Topic 9C Multiple Dimension Arrays A multiple dimension array is an array that has two or more dimensions."— Presentation transcript:

1 Topic 9C – Multiple Dimension Arrays

2 CISC105 – Topic 9C Multiple Dimension Arrays A multiple dimension array is an array that has two or more dimensions. Two dimensional arrays are the simplest type of multiple dimension arrays. They can be used to represent tables of data, matrices, and other two dimensional objects. One of the most obvious example of a two dimensional array is the tic-tac-toe board.

3 CISC105 – Topic 9C Multiple Dimension Arrays Example: Tic-Tac-Toe A tic-tac-toe board has two dimensions, the row and the column. Each dimension is composed of 3 possible indices (there are 3 rows and 3 columns) [0][0][0][1][0][2] [1][0] [2][0] [1][1][1][2] [2][1][2][2]

4 CISC105 – Topic 9C Two Dimensional Arrays We can see how a two dimensional array looks like a table or matrix. Thus, we can represent a two dimensional array as rows and columns. To declare a two-dimensional array: (data type) array_name[# of rows][# of columns];

5 CISC105 – Topic 9C Two Dimensional Arrays Declarations Therefore, a two dimensional array of integers, named x, with 29 rows and 33 columns would be declared as: int x[29][33]; A two dimensional array of characters, named c, with 119 rows and 2 columns would be declared as: char c[119][2];

6 CISC105 – Topic 9C Multiple Dimension Array Declarations We can also declare arrays of more than two dimensions. A six dimensional array of double s could be declared as: double a[2][3][9][4][5][3]; A three dimensional array of integers could be declared as: int x[4][5][4];

7 CISC105 – Topic 9C Referencing Multiple Dimension Arrays To access an element of a multiple dimension array, we use the same form as for one dimensional arrays, with the extra dimension(s) also present, such as: x[2][7] = x[1][7] + 27; y[9][2][0][4] = 9; printf(“%d”,p[3][4][5][9][0]);

8 CISC105 – Topic 9C Multiple Dimension Arrays as Function Arguments We have seen that when one dimensional arrays are passed into a function, the size of the array is not passed in. This allows us to pass arrays of any size into the function. When passing a multiple dimension array as a function parameter, the size MUST BE specified in the function prototype. Thus, we can only pass in fixed-size multiple dimensional arrays into functions.

9 CISC105 – Topic 9C Multiple Dimension Arrays as Function Arguments However, there is one exception to this rule. The FIRST size (in a two dimensional array, the number of rows) MAY be omitted in a function prototype. Therefore, if we wanted to create a function that takes one parameter, a tic-tac-toe board, we could write the prototype as either: void function_x(char board[][3]); void function_x(char board[3][3]); OR

10 CISC105 – Topic 9C Multiple Dimension Arrays as Function Arguments If the size of a multiple dimension array is known and fixed, such as the size of a tic-tac-toe board, the size of all dimensions should be specified. If this is not fixed, or known, the first dimension can, and should, be omitted.

11 CISC105 – Topic 9C Multiple Dimension Arrays as Function Arguments Thus, the following function prototypes are valid: void function_a(char[][4]); int function_b(int[4][9]); int function_c(char p[3][9][2]); int function_d(int[][9][2][4]); int function_e(int[4][9][2][4]); int function_f(char[][3][9]);

12 CISC105 – Topic 9C Multiple Dimension Arrays as Function Arguments In contrast, the following function prototypes are NOT valid: void function_a(char[][]); int function_b(int[4][]); int function_c(char p[][][2]); int function_d(int[][9][][]); int function_e(int[4][][4][]); int function_f(char[9][][9]);

13 CISC105 – Topic 9C Initialization of Multiple Dimension Arrays Multiple dimension arrays can be initialized in much the same way as one dimensional arrays. char board[3][3] = { {‘X’, ‘X’, ‘O’}, {‘O’, ‘X’, ‘O’}, {‘X’, ‘O’, ‘X’} }; Notice how the initialization list is grouped into rows.

14 CISC105 – Topic 9C Initialization of Multiple Dimension Arrays In addition to grouping the initialization list into rows, the initialization list can simply be provided as a straight list: char board[3][3] = {‘X’,‘X’,‘O’,‘O’,‘X’, ‘O’,‘X’,‘O’,‘X’}; This type of initialization list fills in the first row, left-to-right, then the second row, left-to-right, then the third row, etc…

15 CISC105 – Topic 9C Initialization of Multiple Dimension Arrays Thus, this declaration will set board[0][0] to ‘X’, board[0][1] to ‘X’, board[0][2] to ‘O’, board[1][0] to ‘O’, board[1][1] to ‘X’, board[1][2] to ‘O’, board[2][0] to ‘X’, board[2][1] to ‘O’, board[2][2] to ‘X’. char board[3][3] = {‘X’,‘X’,‘O’,‘O’,‘X’, ‘O’,‘X’,‘O’,‘X’};

16 CISC105 – Topic 9C Summary Multiple dimension arrays are very similar to one dimensional arrays, in declarations, initialization, and referencing. When passing a multiple dimension array into a function, the size of the dimensions MUST be specified (in contrast to one dimensional arrays), with the exception of the size of the first array dimension.

17 CISC105 – Topic 9C An Example Problem #1 Write a prototype for a function named function1 that takes two parameters, a three dimensional integer array with sizes 4, 9, & 10, and a four dimensional integer array with sizes 9, 4, 2, & 2. The first array is for input and the function should not be able to modify it. The second array is for input and output; the function can modify it. The function returns a float data type. float function1( const int array1[4][9][10], int array2[9][4][2][2] );

18 CISC105 – Topic 9C An Example Problem #2 Write a function that takes one parameter, a tic-tac-toe board. It should then ask the user which box they want to put a ‘X’ in, by specifying the row and column. The function should then set that specified box equal to ‘X’. The prototype for this function is given: void set_an_X(char board[3][3]);


Download ppt "Topic 9C – Multiple Dimension Arrays. CISC105 – Topic 9C Multiple Dimension Arrays A multiple dimension array is an array that has two or more dimensions."

Similar presentations


Ads by Google