Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Multi-Dimensional Arrays Arrays that have more than one index: Example of differences between basic data types and arrays using integers: Basic integer: int a; -- Only has one value associated with it at one time -- Unless specified otherwise, a basic variable is passed to a function by value 1D array: int a[SIZE] -- Has a series of values associated with it: a[0], a[1], a[2], ….a[SIZE-1] 2D array: int a[ROWSIZE][COLSIZE] -- Has a series of values associated with it: a[0][0], a[0][1], a[0][2], …a[0][COLSIZE-1], a[1][0],… Multi-D array: int a[SIZE][SIZE][SIZE]… Arrays are almost always passed by reference Referring to Two-Dimensional Array Elements Format arrayName[ rowIndex ][ colIndex ] 2 -3 23 25 5 9 10 0 17 -13 9 8 The accessed element of the array is on Row rowIndex and Column colIndex Both row and column indices start from 0, and must be an integer or an integer expression. The names of the elements in the i-th row all have a first suscript/index of i. The names of the elements in the j-th column all have a second suscript/index of j. Passing Two-Dimensional Arrays to Functions Defining the function: The function’s parameter list must specify that a two-dimensional array will be received. #define ROWSIZE 10 #define COLSIZE 10 Specifies the number of columns of the array parameter, which is required. void oneFunc( int ary[ ] [ COLSIZE ] , int rowSize, int colSize ) The number of rows of the array parameter is NOT required. Normally, also pass the numbers of rows and columns of the array argument. The header of the function Calling the function To pass an array argument to a function, specify the name of the array without any brackets int anArray[ ROWSIZE ][ COLSIZE ] = {{0}}; oneFunc( anArray, ROWSIZE, COLSIZE ); Array size usually passed to function Two-Dimensional Arrays - An Example Compute a multiplication table * 1 2 3 4 5 6 7 8 9 1 1 2 3 4 5 6 7 8 9 2 2 4 6 8 10 12 14 16 18 3 3 6 9 12 15 18 21 24 27 4 4 8 12 16 20 24 28 32 36 5 5 10 15 20 25 30 35 40 45 6 6 12 18 24 30 36 42 48 54 7 7 14 21 28 35 42 49 56 63 8 8 16 24 32 40 48 56 64 72 9 9 18 27 36 45 54 63 72 81 Here is the code for the multiplication table #include <stdio.h> Standard input/output – printf/scanf int main() { int nrows = 10, ncols = 10; int mult_table[10][10]; int row, col; Start main } Declare the number of rows/columns Declare a 10x10 array Declare row and column counter printf("* 0 1 2 3 4 5 6 7 8 9\n"); Print out first line for (row = 0; row < nrows; row++) { printf("%d ", row); for (col = 0; col < ncols; col++) { mult_table[row][col] = row*col; printf("%2d ", mult_table[row][col]); } printf("\n"); } Start loop over the rows Print out row number Loop over the columns Calculate multidimensional array Print multidimensional array by filling the columns fist Start new line Let’s go through this code segment: printf("* 0 1 2 3 4 5 6 7 8 9\n"); for (row = 0; row < nrows; row++) { printf("%d ", row); for (col = 0; col < ncols; col++) { mult_table[row][col] = row*col; printf("%2d ", mult_table[row][col]); } printf("\n"); } } The print out to the screen: This is the very 1st line * 0 1 2 3 4 5 6 7 8 9 0 0 0 0 0 0 0 0 0 0 0 1 0 …… This column (except for the ‘*’) comes from printing out “row” The use of 2-D arrays in imaging Suppose you wanted to find Waldo in the image below. Each pixel has a “color” (which is represented by a number) associated with it that can be stored as a 2-D array Can search this 2D array in order to find the correct match with Waldo!