2 Dimensional Array {With NEB Questions}

Two Dimensional Array
The Two dimensional array can be defined as an array of arrays that is capable of storing data in rows and column. Each row in the array is associated with however many columns you have defined for the array.
The only drawback is the fact that the entire array must contain elements of the same type, which is defined when you declare the array. Because of the capability of storing data in rows and columns, it is very powerful programming compared to one dimensional array.
20 arrays are created to implement a relational database like data structure. In 2D array an element can be accessed simply by array name followed by index numbers enclosed in brackets.
Syntax to declare two-dimensional Array in C:
1. data_type arr_name[numRows][numCols];
Eg: int arr[2][2];

2. data_type arr_name[numRows][numCols]={{first_row},{second_row}……, {last_row}};
eg int a[2][2]={{10,12},{23,9}};
It can be alswo written as
int a{10,12,23,9};

Initialization of 2D array in C:
In single dimensional array, we don’t need to specify the size of Arry if declaration and initialization are being done simultaneously. However, this will not work with 2d array we have to define at least second dimension of array. The 2D array can be declared and defined as:
int arr[5][3];
If you wish to initilize the value in the array during compile time, you could use :
int matrix[][3]={{0,1,2,3},{3,4,5}, {6,7,8},{9,10,12},{12,13,14}};

 

The numCols must be declared if we assign the values in the two dimensional array. This code initilizes matrix[0][0]=0, matrix[0][1]=1 and so on.

Displaying/Retriving Data
Because two dimensional data must be filled by row and column, processing a two dimensional arry involves using nested for loops. For instance in order to display an array declared above matrix[5][3] with user input, we could use the following code:
for(rows=0;row<5;row++)
{
for(col=0;col<3;rol++)
{
printf(“%d”,matrix[row][col]);
}
}

Q. Write a C program to read a matrix , store it in array and display it.

#include <conio.h>
#include <stdio.h>
void main()
{

int i,j,matrix[3][3];


//storing data
printf("Enter Numbers for 3x3 matrix");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&matrix[i][j]);
}
}

//storing data
printf("\n The matrix is\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",matrix[i][j]);
}
printf("\n");
}

getch();
}

 

Output:

 

Q.No. 2) Write A program to enter two 3×3 matrix and add the matrices.

#include <conio.h>
#include <stdio.h>
void main()
{
int i,j,a[3][3],b[3][3],c[3][3];

//Enter first 3x3 matrix
printf("Enter First 3x3 matrix");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
  scanf("%d",&a[i][j]);
}
}

//Enter Second 3x3 matrix
printf("Enter Second 3x3 matrix");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
  scanf("%d",&b[i][j]);
}
}

//Displaying the sum of matfices matrices
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
  c[i][j]= a[i][j] + b[i][j];
}
}

//Enter first 3x3 matrix
printf("Sum of the matrices \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
  printf("%d ",c[i][j]);
}
printf("\n");
}

getch();
}

Q No. 3) Write a program to enter two 3×3 matrices and multiply them.

 Output:

Question No. 4) Write a program to enter 2×3 matrices and find its transpose.

#include <conio.h>
#include <stdio.h>
void main()
{
int a[2][3],b[3][2],i,j;

//Enter 2x3 matrix
printf("Enter a 2x3 matrix");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
  scanf("%d",&a[i][j]);
}
}

//transposing a matrix
for(i=0;i<3;i++)
{
  for(j=0;j<2;j++)
  {
    b[i][j]=a[j][i];
  }
}


//Displaying the transpose matrix b
printf("The Transposed Matrix is as follows\n");
for(i=0;i<3;i++)
{
  for(j=0;j<2;j++)
  {
    printf("%d \t",b[i][j]);
  }
  printf("\n");
}

getch();
}

Output

Other Questions

  1. Write a program to enter 2×3 matrix and find the sum.
  2. Write a program to enter two matrices of 2×2 and find the differences.
  3. Write a program to enter a 3×3 matrix and transpose the matrix.