4. Multidimensional Arrays
• Arrays in C can have virtually as many dimensions as
you want.
• Definition is accomplished by adding additional
subscripts when it is defined.
• For example:
– int a [4] [3] ; // 4 Rows, 3 Columns
– defines a two dimensional array
a[0][0] a[0][1] a[0][2]
a[1][0] a[1][1] a[1][2]
a[2][0] a[2][1] a[2][2]
a[3][0] a[3][1] a[3][2]
Dr. Yousaf, PIEAS
5. 2-D Arrays
Dr. Yousaf, PIEAS
How to store these values?
1 2 3
4 5 6
7 8 9
10 11 12
How to print these values?
6. #include<stdio.h>
int main()
{
int a[4] [3] = { {1, 2, 3} , { 4, 5, 6} , {7, 8, 9} , {10, 11, 12} };
int row, col;
for (row = 0; row <=3; row++)
{ for (col = 0; col <=2; col++)
{
printf(“%d", a[row][col]);
}
}
getchar(); return 0; }
Dr. Yousaf, PIEAS
How to Print 2-D Arrays?
8. for (row = 0; row <=3; row++)
{ for (col = 0; col <=2; col++)
{
printf(“%dt",a[row][col]);
}
}
Output:
1 2 3 4 5 6 7 8 9
10 11 12
Dr. Yousaf, PIEAS
Output with Tabs
9. for (row = 0; row <=3; row++)
{ for (col = 0; col <=2; col++)
{
printf(“%dt", a[row][col]);
}
printf(“n”);
}
Dr. Yousaf, PIEAS
Output in Matrix Form
10. for (row = 0; row <=3; row++)
{ for (col = 0; col <=2; col++)
{
printf(“%dt", a[row][col]);
}
printf(“n”);
}
Dr. Yousaf, PIEAS
Output in Matrix Form
Output:
1 2 3
4 5 6
7 8 9
10 11 12
11. #include<stdio.h>
int main()
{
int a[4] [3];
int row, col;
for (row = 0; row <=3; row++)
{
printf("Enter 3 elements of row %dn", row + 1);
for (col = 0; col <=2; col++)
{
scanf("%d",&a[row][col]);
}
}
//Rest of the code goes here
Dr. Yousaf, PIEAS
How to scan 2-D Arrays?
12. Initializing Multidimensional Arrays
• The following initializes a[4][3]:
int a[4] [3] = { {1, 2, 3} , { 4, 5, 6} , {7, 8, 9} , {10, 11, 12} };
• Also can be done by:
int a[4] [3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
– is equivalent to
a[0][0] = 1;
a[0][1] = 2;
a[0][2] = 3;
a[1][0] = 4;
...
a[3][2] = 12;
Dr. Yousaf, PIEAS
14. Multiple-Subscripted Arrays
• Initialization
– int b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } };
– Initializers grouped by row in braces
– If not enough, unspecified elements set to zero
int b[ 2 ][ 2 ] = { { 1 }, { 3, 4 } };
• Referencing elements
– Specify row, then column
printf( "%d", b[ 0 ][ 1 ] );
1 2
3 4
1 0
3 4
Dr. Yousaf, PIEAS
15. Multidimensional Arrays
• Array declarations read right-to-left
• int a[10][3][2];
• “a is array of ten arrays of three arrays of two (type
ints)”. In memory
2 2 2
3
2 2 2
3
2 2 2
3
...
10
Dr. Yousaf, PIEAS
17. Addition of Two Matrices
#include <stdio.h>
int main()
{
int X[2][2] = { {1,2},{3,4} }, Y[2][2] =
{ {5,6},{7,8} };
int add[2][2];
int i, j;
printf("ntAddition of two matrices
is");
for (i = 0; i<2; i++)
{
for (j = 0; j<2; j++)
{
add[i][j] = X[i][j] + Y[i][j];
printf("%dt", add[i][j]);
}
printf("n");
}
getchar();
return 0;
}
Dr. Yousaf, PIEAS
18. Multiplication of Two Matrices
#include <stdio.h>
int main()
{
int X[2][2] = { {1,2},{3,4} },
Y[2][2] = { {5,6},{7,8} };
int add[2][2], mul[2][2];
int i, j, k, sum = 0;
printf("nntMultiplications
of two matrices is");
for (i = 0; i<2; i++)
{
for (j = 0; j<2; j++)
{
for (k=0; k<2; k++)
{
sum = sum + (X[i][k]*Y[k][j]);
}
mul[i][j] = sum;
printf("%dt", mul[i][j]);
sum = 0;
}
printf("n");
} getchar(); return 0; }
Dr. Yousaf, PIEAS