1 / 7

Multi -Dimensional Arrays

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

remy
Download Presentation

Multi -Dimensional Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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

  3. Specifies the number of columns of the array parameter, which is required. The header of the function Normally, also pass the numbers of rows and columns of the array argument. The number of rows of the array parameter is NOT required. 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

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

  5. Here is the code for the multiplication table #include <stdio.h> int main() { intnrows = 10, ncols = 10; intmult_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. 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 0 …… 1 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!

More Related