Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 16: Working with Complex Data Arrays. Double-Subscripted Arrays Commonly used to represent tables of values consisting of information arranged.

Similar presentations


Presentation on theme: "Lecture 16: Working with Complex Data Arrays. Double-Subscripted Arrays Commonly used to represent tables of values consisting of information arranged."— Presentation transcript:

1 Lecture 16: Working with Complex Data Arrays

2 Double-Subscripted Arrays Commonly used to represent tables of values consisting of information arranged in rows and columns. Also called two-dimensional arrays Examples

3 Defining Two-Dimensional Arrays #define ROWSIZE 3 #define COLUMNSIZE 4 int arrayName[ ROWSIZE ][ COLUMNSIZE ]; Examples int a[ 10 ][ 25 ]; float b[ 5 ][ 15 ]; float c[ ROWSIZE ][ COLUMNSIZE ]; Specifying the type of each element Name of the array The number of the rows The number of the columns

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

5 Initializing Two-Dimensional Arrays Using nested loops #define ROWSIZE 3 #define COLSIZE 4 int a[ ROWSIZE ][ COLSIZE ]; int row, col; for (row = ; row < ; row ++) { for (col = ; col < ; col ++) { printf(“Enter the element a[%d][%d]: ”, row, col); scanf(“%d”, ); printf(“\n”); } Initializing an array in a definition with initializer lists. Similar to a single-subscripted array. Initializers grouped by row in braces int a[ 2 ] [ 3 ] = {{1, 2, 3}, {4, 5, 6}}; If not enough, unspecified elements set to zero. int a[ 2 ][ 3 ] = {{1}, {4, 5}}; int b[ 3 ][ 5 ] = {{1}, {2}}; 0101 0 COLSIZE COLSIZE - 1 ROWSIZE ROWSIZE - 1 ROWSIZE 0101 0 a[row][col] a[row, col] &a[row][col] &a[row, col] &a[row][col]

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

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

8 In-class Programming Assignment 29.09529.6730.24530.8231.39531.9732.54533.12… 51.88552.56324.88135.90233.88120.67650.0453.364… 57.84527.2756.69526.1255.54554.9724.39553.82… Left Front Right

9 Practice Question Q. Which is the right prototype of a function that takes a two-dimensional array as parameter? A. void func( int ary[] ); B. void func( int ary[][25] ); C. void func( int ary[10][] ); D. void func( int ary[][] ); Solution: B


Download ppt "Lecture 16: Working with Complex Data Arrays. Double-Subscripted Arrays Commonly used to represent tables of values consisting of information arranged."

Similar presentations


Ads by Google