Thursday, 13 May 2021

Operations on two dimensional arrays in C

In C a two dimensional array is an array of one dimensional array. many applications requires that data to be stored in more than one dimension. One Common example is a matrix which requires an array that consists of rows and columns. The following are different operations that can perform on two dimensional arrays.


 

i) Transpose: Transpose of a matrix of order m x n can be obtained as n x m. The following C program shows how to transpose a 2D matrix.

#include<stdio.h>
void main()
{
int a[2][2], t[2][2],I, j;
printf(“ enter values into matrix”);
for(i=0;i<2;i++)
{
   for(j=0;j<2;j++)
   {
   scanf(“%d”,&a[i][j]);
   }
}
for(i=0;i<2;i++)
  {
     for(j=0;j<2;j++)
    {
      t[i][j]=a[j][i];
    }
}
printf(“ transpose of matrix is”);
for(i=0;i<2;i++)
  {
    for(j=0;j<2;j++)
    {
      printf(“%d”,a[i][j]);
    }
}
getch();
}

ii) Addition: Two matrices which are compatible with each other can be added together there by storing results on the third matrices.  Two matrices are said to be compatible when they have same number of rows and columns.  The following C program shows how two 2D matrices are added using arrays.

#include<stdio.h>
void main()
{
int a[2][2], a[2][2],c[2][2],I, j;
printf(“ enter values into matrix A”);
for(i=0;i<2;i++)
{
   for(j=0;j<2;j++)
   {
   scanf(“%d”,&a[i][j]);
   }
}
printf(“ enter values into matrix B”);
for(i=0;i<2;i++)
{
   for(j=0;j<2;j++)
   {
   scanf(“%d”,&b[i][j]);
   }
}

for(i=0;i<2;i++)
  {
     for(j=0;j<2;j++)
    {
      c[i][j]=a[i][j]-b[i][j];
    }
}
printf(“ subtraction of two matrix is”);
for(i=0;i<2;i++)
  {
    for(j=0;j<2;j++)
    {
      printf(“%d”,c[i][j]);
    }
}
getch();
}

iii) Subtraction:  Two matrices are compatible with each other can be subtracted there by storing results on the third matrices. Two matrices are said to be compatible when they have same number of rows and columns. The following C program shows how two 2D matrices are subtracted using arrays.

#include<stdio.h>
void main()
{
int a[2][2], a[2][2],c[2][2],I, j;
printf(“ enter values into matrix A”);
for(i=0;i<2;i++)
{
   for(j=0;j<2;j++)
   {
   scanf(“%d”,&a[i][j]);
   }
}
printf(“ enter values into matrix B”);
for(i=0;i<2;i++)
{
   for(j=0;j<2;j++)
   {
   scanf(“%d”,&b[i][j]);
   }
}

for(i=0;i<2;i++)
  {
     for(j=0;j<2;j++)
    {
      c[i][j]=a[i][j]-b[i][j];
    }
}
printf(“ subtraction of two matrix is”);
for(i=0;i<2;i++)
  {
    for(j=0;j<2;j++)
    {
      printf(“%d”,c[i][j]);
    }
}
getch();
}

iv) Multiplication: Two matrices can be multiplied with each other if number of columns in the first matrix is equal to number of rows in the second matrix. Suppose the order of the matrix 'A' is m x n and order of matrix 'B' is p x q then if p = =q then only multiplication of A and B are possible. 

#include<stdio.h>
void main()
{
int a[2][2], a[2][2],c[2][2],I, j,k;
printf(“ enter values into matrix A”);
for(i=0;i<2;i++)
{
   for(j=0;j<2;j++)
   {
   scanf(“%d”,&a[i][j]);
   }
}
printf(“ enter values into matrix B”);
for(i=0;i<2;i++)
{
   for(j=0;j<2;j++)
   {
   scanf(“%d”,&b[i][j]);
   }
}

for(i=0;i<2;i++)
  {
     for(j=0;j<2;j++)
    {
       for(k=0;k<2;k++)
       {
      c[i][j]=a[i][k]-b[k][j];
    }
}
}


printf(“ multiplication of two matrix is”);
for(i=0;i<2;i++)
  {
    for(j=0;j<2;j++)
    {
      printf(“%d”,c[i][j]);
    }
}
getch();
}

0 comments :

Post a Comment

Note: only a member of this blog may post a comment.

Machine Learning

More

Advertisement

Java Tutorial

More

UGC NET CS TUTORIAL

MFCS
COA
PL-CG
DBMS
OPERATING SYSTEM
SOFTWARE ENG
DSA
TOC-CD
ARTIFICIAL INT

C Programming

More

Python Tutorial

More

Data Structures

More

computer Organization

More
Top