Two Dimensional Arrays: by Dr. Karuna

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 14

TWO DIMENSIONAL ARRAYS

By Dr. Karuna
TWO DIMENSIONAL ARRAYS
• Arrays with two subscripts(dimensions/indices) are
called Two dimensional arrays.
• It is widely used in matrix operations.
• Its syntax is:  
Data_Type Array_Name[Row_Size][Col_Size];
• Here we can note that Two Dimensional Arrays are "an
array of One-Dimensional arrays of elements of same
type".
• Example:
• int a[3][4];
TWO DIMENSIONAL ARRAYS
• Example:
int a[3][4];
• The above code can be pictorially represented
as shown below:
Initialization of Two Dimensional Arrays

• 1. Compile time Initialization


Example-1:
int a[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
• The above code can be pictorially represented
as shown below
1. Compile time Initialization
1. Compile time Initialization
• Example-4:
int a[ ][2] = { 11, 2, 3, 4, 5 }; /*Row size = 3 */
int a[ ][4] = { 11, 2, 3, 4, 5 }; /*Row size = 2 */
int a[ ][5] = { 11, 2, 3, 4, 5 }; /*Row size = 1 */
• In 2D’ Arrays Row size is optional but COLUMN
size Compulsory during initializing 2D’ Arrays
at the time of their declaration
2. Run time Initialization
• Example:
int matrix[2][3];
scanf("%d", &matrix[0][0]); //read data from keyboard into matrix at row=0 col=0.
scanf("%d", &matrix[0][1]); //read data from keyboard into matrix at row=0 col=1.
scanf("%d", &matrix[0][2]); //read data from keyboard into matrix at row=0 col=2.
scanf("%d", &matrix[1][0]); //read data from keyboard into matrix at row=1 col=0.
scanf("%d", &matrix[1][1]); //read data from keyboard into matrix at row=1 col=1.
scanf("%d", &matrix[1][2]); //read data from keyboard into matrix at row=1 col=2.
Accessing of Array elements
• Example: Program to illustrate reading to two-dimensional array.
#include<stdio.h>
void main()
{
int matrix[2][3], i, j;
printf("enter elements of 2*3 matrix: \n”);
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
scanf("%d", &matrix[i][j]);
}
}
}
Accessing of Array elements
• Example: Program to illustrate printing of two-dimensional array.
#include<stdio.h>
void main()
{
int matrix[2][3], i, j;
printf("elements of the matrix are: \n”);
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("%d \t", matrix[i][j]);
}
printf(“\n”);
}
}
}
Program to carry out the following expression
C=A+2*B , where A and B are MXN Matrix
#include<stdio.h>
void main ()
{
int a[10][10], b[10][10], c[10][10], i, j, m, n;
printf(“\n\tEnter the Order of Matrix : “);
scanf(“%d %d”,&m,&n); /* m is ROW & n is COLUMN */
printf(“\n\tEnter %d Elements of Matrix A : “, m * n);
for(i = 0; i < m; i++) /* specify i-th Row */
{
for(j = 0; j < n; j++) /* specify j-th Column */
{
scanf(“%d”,&a[ i ][ j ]);
}
}
Program to carry out the following expression
C=A+2*B , where A and B are MXN Matrix

printf(“\n\tEnter %d Elements of Matrix B: “, m * n);


for(i = 0; i < m; i++) /* specify i-th Row */
{
for(j = 0; j < n; j++) /* specify j-th Column */
{
scanf(“%d”,&b[ i ][ j ]);
}
}
Program to carry out the following expression
C=A+2*B, where A and B are NXN Matrix

printf(“\n\tCarry out the equation C=A+2*B \n”);


for(i = 0; i < m; i++) /* specify i-th Row */
{
for(j = 0; j < n; j++) /* specify j-th Column */
{
c[i][j]=a[ i ][ j ]+2*b[ i ][ j ];
}
}
Program to carry out the following expression
C=A+2*B , where A and B are NXN Matrix
printf(“\n\tThe resultant Matrix is: ” );
for(i = 0; i < m; i++) /* specify i-th Row */
{
for(j = 0; j < n; j++) /* specify j-th Column */
{
printf(“%d\t”,c[ i ][ j ]);
}
printf(“\n”);
}
} /* closing of main */
Thank you

You might also like