Multi-Dimensional ArraysArrays 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 value1D 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 ]
Both row and column indices start from 0, and must be an integer or an integer expression.
2 -3 23 25
5 9 17 -13
10 0 9 8
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.
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 functionTo 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.
Two-Dimensional Arrays - An ExampleCompute 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>
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/columnsDeclare a 10x10 arrayDeclare row and column counter
Print out first line
Start loop over the rows
Print out row numberLoop over the columns
Calculate multidimensional arrayPrint multidimensional array by fillingthe columns fistStart new line
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 0 0 0 0 0 0 0 0 0 0
1 0 ……
This is the very 1st line
This column (except for the ‘*’) comes from printing out “row”
The use of 2-D arrays in imagingSuppose 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 arrayCan search this 2D array in order to find the correct match with Waldo!