Multi-Dimensional Arrays
14
Introduction to Programming
Two-Dimensional Arrays In general, an array with m rows and n columns is called an m x n array (m by n array).
int array[3][4]; col 0 row 0 row 1 row 2
col 1
col 2
col 3
a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3] Introduction to Programming
Two-Dimensional Arrays • The first and second indeces signify the row and the column of the element in the array respectively. • Physically (in main memory), elements are allocated and placed consecutively (row-wise), just like one-dimensional arrays. int array[2][2]; a[0][0] a[0][1] a[1][0] a[1][1] Introduction to Programming
Declaration • Declaration – size or number of elements should be specified before compilation. – only integer literals and preprocessor constants are allowed. #define ROW 10 #define COL 5 ... int array[5][10]; double d[ROW][COL]; Introduction to Programming ...
Initialization • Initialization #define ROW 4 #define COL 3 ... int array[2][2] = { {0,1},{2,3} }; int array1[2][2] = {0,1,2,3}; double array2[ROW][COL] = {0}; float array3[2][COL] = { {2},{3,4} }; ...
Introduction to Programming
Accessing Elements • Rules on one-dimensional arrays still apply. • Indeces should be from 0 to N-1 for all N dimension sizes. int array[2][3]; array[2][3] = 3; array[1][3] = 78; array[1][2] = 99;
/* epic fail! */ /* fail! */ /* last-element win! */
Introduction to Programming
An Important Note • What you have to understand is that given an array declaration of int arr[2][3];
arr[0] is a one-dimensional array of ints and that arr[0][0] is an int.
• This concept extends to N dimensions.
Introduction to Programming
Array of Strings • Remember that a string is just an array of chars terminated by '\0'. • Hence, an array of strings is just a twodimensional array of chars.
Introduction to Programming
Example – 1/2 #include <stdio.h> #include <string.h> /* number of strings */ #define STRINGS 4 /* max number of chars per string */ #define MAX_CH 100
Introduction to Programming
Example – 2/2 int main() { char strings[STRINGS][MAX_CH] = {“Ako”, ”si”, ”Wilmarc”, ”Lopez!”}; strcmp(strings[0], strings[1]); strcat(strings[2], strings[3]); return 0; } Introduction to Programming
Multi-Dimensional Arrays • All of the discussed rules apply for Ndimensional arrays. int three_d[2][2][2] = { {{1,2},{3,4}}, {{5,6},{7,8}} }; float four_d[2][3][4][5]; int i,j,k,l; for (i=0; i<2; i++) for (j=0; j<3; j++) for (k=0; k<4; k++) for (l=0; l<5; l++) four_d[i][j][k][l] = i*j*k*l; Introduction to Programming
Arrays to Functions • One-dimensional Arrays – size is not required, hence, all array sizes will work. void funxn( int array[], int size ); void funxn2( int array[SIZE] );
• N-dimensional Arrays – only the size of the left-most dimension, wrt brackets, is optional. The rest is compulsary. void funxn_2d( int array[][COL], int row); void funxn_3d( char array2[][5][5], int n ); Introduction to Programming
Arrays to Functions • Note that by NOT specifying the size of the leftmost dimension, the compiler will let you pass arrays of any size on that dimension (i.e. the leftmost one). • If a function deals with an array of a specific size, then specifying the size is best. Why?
Introduction to Programming