Matrix Addition & Subtraction
In this example, we will understand to perform Matrix Addition, Subtraction using Array and know how to write, compile and debug it in C language and also learn how to implement it in c.
In order to do this you will need
- Basic knowledge of c programming language.
- As well as know how to operate codeblocks.
An array is a fixed-size sequential collection of elements of same data types that share a common name.It is simply a group of data types. An array is a derived data type.An array is used to represent a list of numbers or a list of names.
So let's begin with our program
1.Open a codeblocks software.
2.Open new empty file.
3.Copy and paste the code from below.
Input code-
//PROGRAM FOR ADDITION AND SUBTRACTION OF MATTRIX BY ARRAY.
#include<stdio.h>
int main()
{
int a[2][2],b[2][2],c[2][2],d[2][2],i,j,k,m[2][2]={0};
printf("Enter first 2*2 matrix elements");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Given 2*2 matrix elements of first: \n ");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("\t %d",a[i][j]);
}
printf("\n");
}
printf("Enter second 2*2 matrix elements");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("Given 2*2 matrix elements of second: \n ");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("\t %d",b[i][j]);
}
printf("\n");
}
//// logic for addition
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("Addition result: \n ");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("\t %d",c[i][j]);
}
printf("\n");
}
//// logic for Sub
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
d[i][j]=a[i][j]-b[i][j];
}
}
printf("Sub result: \n ");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("\t %d",d[i][j]);
}
printf("\n");
}
return 0;
}
Now simply build the code. Look for any error. Now run the code.
For output enter any four number for first 2X2 matrix then press enter key & again enter any four number for second 2X2 matrix and now press enter key .
Click the following button to download a program for Matrix Addition, Subtraction using Array
Thats it.Thank you for scrolling.
Tags:
Array