Presentation is loading. Please wait.

Presentation is loading. Please wait.

Multi-Dimensional Arrays Arrays that have more than one index: Example of differences between basic data types and arrays using integers: Basic integer:

Similar presentations


Presentation on theme: "Multi-Dimensional Arrays Arrays that have more than one index: Example of differences between basic data types and arrays using integers: Basic integer:"— Presentation transcript:

1 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

2 Referring to Two-Dimensional Array Elements Format arrayName[ rowIndex ][ colIndex ] Both row and column indices start from 0, and must be an integer or an integer expression. The accessed element of the array is on Row rowIndex and Column colIndex 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.

3 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 void oneFunc( int ary[ ] [ COLSIZE ], int rowSize, int colSize ) 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 The header of the function Normally, also pass the numbers of rows and columns of the array argument. Specifies the number of columns of the array parameter, which is required. The number of rows of the array parameter is NOT required.

4 Two-Dimensional Arrays - An Example Compute a multiplication table

5 Here is the code for the multiplication table #include int main() { int nrows = 10, ncols = 10; int mult_table[10][10]; int row, col; 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"); } Standard input/output – printf/scanf Start main Declare the number of rows/columns Declare a 10x10 array Declare row and column counter Print out first line 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

6 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"); } Let’s go through this code segment: The print out to the screen: * 0 1 2 3 4 5 6 7 8 9 0 00 0 0 00 0 00 0 1 0…… This is the very 1 st line This column (except for the ‘*’) comes from printing out “row”

7 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!


Download ppt "Multi-Dimensional Arrays Arrays that have more than one index: Example of differences between basic data types and arrays using integers: Basic integer:"

Similar presentations


Ads by Google