Lecture 12
Multidimensional Arrays
Arrays in C can
have multiple subscripts. A common use of multiple subscripted arrays is to
represent tables of values consisting of information arranged in rows and
columns. To identify a particular table element, we must specify two
subscripts: the first identifies the row and the second identifies the column.
Arrays that require two subscripts to identify a particular element are called
double subscripted arrays. Note that, multidimensional arrays can have more
than two subscripts. ANSI standard supports at least 12 subscripts.
When we say a
3X4 array, we declare it as follows-
int a [3][4];
When we declare
in such a way, the actual thing that takes place in memory is illustrated
below-
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]
|
So, the first
subscript denotes the row and the second subscript denotes the column.
A multiple subscripted
array can be initialized in its declaration much like a subscripted array. b [2] [2] can be declared and initialized
like
int b [2] [2] =
{{1,2}, {3,4}};
in that case, 1
and 2 initialize b[0][0] and b[0][1] and 3 and 4 initialize b[1][0] and b[1][1].
Let’s
demonstrate the stuff with an example. We will declare three 2X3 arrays-
array1, array2 and array3. Thereafter, we will put values into those arrays in
several different ways.
main(){
int array1 [2][3]={{1,2,3},{4,5,6}};
int array2 [2][3]={1,2,3,4,5};
int array3 [2][3]={{1,2},{4}};
printf(“Values in array1 are: \n”);
for(int i=0;i<=1;i++){ // taking track of rows
for (int j=0;j<=2;j++) //taking track of columns
printf (“%d ”, array1[i][j]);
printf(“\n”);
}
printf(“Values in array2 are: \n”);
for(int i=0;i<=1;i++){ // taking track of rows
for (int j=0;j<=2;j++) //taking track of columns
printf (“%d ”, array2[i][j]);
printf(“\n”);
}
printf(“Values in array3 are: \n”);
for(int i=0;i<=1;i++){ // taking track of rows
for (int j=0;j<=2;j++) //taking track of columns
printf (“%d ”, array3[i][j]);
printf(“\n”);
}
}
The output of
the program is-
Values
in array1 are:
1
2 3
4
5 6
Values
in array2 are:
1
2 3
4
5 0
Values
in array3 are:
1
2 0
4
0 0
To understand
more deeply how the values are stored in a two dimensional array, let us
consider another example-
main(){
int a [4][5];
int i, j;
for(i=0;i<=4;i++)
for (j=0;j<=5;j++)
a[i][j]=i*j;
for(i=0;i<=4;i++){
for (j=0;j<=5;j++)
printf(“%d ”, a[i][j]);
printf(“\n”);
}
}
The output of
the program is-
0
0 0 0 0
0
1 2 3 4
0
2 4 6 8
0
3 6 9 12
It is important
to remember that while initializing a 2-D array it is necessary to mention the
second (column) dimension, whereas the first dimension (row) is optional.
Thus the
declarations,
are perfectly
valid.
But declarations
like
are not valid.
Three Dimensional Array
We aren’t going
to show a programming example that uses a three-dimensional array. This is
because, in practice, one rarely uses this array. However, an example of
initializing a three-dimensional array will consolidate your understanding of
subscripts:
This three
dimensional array can be thought of array or array of array. In other word, 3
pieces of 4X2 arrays.
Passing Arrays to Functions
Array elements
can be passed to a function by calling the function by value, or by reference.
In the call by value we pass values of array elements to the function, whereas
in the call by reference we pass addresses of array elements to the function. As
we did not see call by reference yet, we will see that later.
and the output
is
Here, we are
passing an individual array element at a time to the function display( ) and
getting it printed in the function display( ). Note that since at a time
only one element is being passed, this element is collected in an ordinary
integer variable m, in the function display( ).
Now we will take
a loot at an example that passes a two dimensional array to a function-
The function
prototype in that case will be
void
print (int [ ][ ], int , int );
The output will
be-
1 comments:
awesome! chemical overhaul aircon price
Post a Comment