c program to implement matrix subtraction

Description


This program demonstrates matrix subtraction.  Three 2D arrays are declared a[][], b[][] and s[][]. The row and column value of the matrix is read into the variables r and c. The program uses for loop to read the value into the two matrices a[][] and b[][]. The statement s[i][j]=a[i][j]-b[i][j]; inside the for loop performs matrix subtraction and the statement printf("%d ",s[i][j]); prints the new matrix to the output screen.


Program 


// matrix subtraction c program

#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

int r,c,a[50][50],b[50][50],s[50][50],i,j;

printf("Enter the row value and cloum value\n");

scanf("%d%d",&r,&c);

printf("Enter the elements of first matrix\n");

for(i=0;i<r;i++)

{

for(j=0;j<c;j++)

{

scanf("%d",&a[i][j]);

}}

printf("Enter the elements of second matrix\n");

for(i=0;i<r;i++)

{

for(j=0;j<c;j++)

{

scanf("%d",&b[i][j]);

}}

for(i=0;i<r;i++)

{

for(j=0;j<c;j++)

{

s[i][j]=a[i][j]-b[i][j];

}}

printf("Sum of matrix\n");

for(i=0;i<r;i++)

{

for(j=0;j<c;j++)

{

printf("%d ",s[i][j]);

}

printf("\n");

}

getch();

}



No comments: