Understanding Multiple Dimension Arrays in Programming
Multiple dimension arrays are an advanced data structure in programming that handles two or more dimensions. The simplest type, two-dimensional arrays, can represent complex data like matrices and game boards, such as tic-tac-toe. This article explores how to declare, initialize, and use these arrays in functions. It discusses the importance of specifying dimensions when passing arrays as function parameters. Furthermore, it highlights how to initialize them in a structured way and the differences when handling fixed vs. dynamic sizes, making it essential reading for programming learners.
Understanding Multiple Dimension Arrays in Programming
E N D
Presentation Transcript
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. 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 1 2 0 [0][0] [0][1] [0][2] 1 [1][0] [1][1] [1][2] 2 [2][0] [2][1] [2][2] 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]; 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]; CISC105 – Topic 9C
Multiple Dimension Array Declarations • We can also declare arrays of more than two dimensions. A six dimensional array of doubles 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]; 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]); CISC105 – Topic 9C
Multiple Dimension Arraysas 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. CISC105 – Topic 9C
Multiple Dimension Arraysas 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 CISC105 – Topic 9C
Multiple Dimension Arraysas 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. 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]); 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]); 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. 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… CISC105 – Topic 9C
Initialization ofMultiple 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’}; 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. 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] ); 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]); CISC105 – Topic 9C