MultiDimensional Arrays that have more than one index

  • Slides: 7
Download presentation
Multi-Dimensional Arrays that have more than one index: Example of differences between basic data

Multi-Dimensional 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 1 D array: int a[SIZE] -- Has a series of values associated with it: a[0], a[1], a[2], …. a[SIZE-1] 2 D 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]… Arrays are almost always passed by reference

Referring to Two-Dimensional Array Elements Format array. Name[ row. Index ][ col. Index ]

Referring to Two-Dimensional Array Elements Format array. Name[ row. Index ][ col. Index ] The accessed element of the array is on Row row. Index and Column col. Index 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

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 one. Func( int ary[ ] [ COLSIZE ] , int row. Size, int col. Size ) 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 an. Array[ ROWSIZE ][ COLSIZE ] = {{0}}; one. Func( an. Array, ROWSIZE, COLSIZE ); Array size usually passed to function

Two-Dimensional Arrays - An Example Compute a multiplication table

Two-Dimensional Arrays - An Example Compute a multiplication table

Here is the code for the multiplication table #include <stdio. h> Standard input/output –

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]; int row, col; Start main } Declare the number of rows/columns Declare a 10 x 10 array Declare row and column counter printf("* 0 1 2 3 4 5 6 7 8 9n"); 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("%2 d ", 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

Let’s go through this code segment: printf("* 0 1 2 3 4 5 6 7 8 9n"); for (row = 0; row < nrows; row++) { printf("%d ", row); for (col = 0; col < ncols; col++) { mult_table[row][col] = row*col; printf("%2 d ", mult_table[row][col]); } printf("n"); } } The print out to the screen: This is the very 1 st line * 0 1 2 3 4 5 6 7 8 9 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

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 2 D array in order to find the correct match with Waldo!