1. Write a C Program to find sum and subtraction of two matrices using functions & dynamic memory allocation.
2. Write a C program to check two matrices are identical or not using functions & dynamic memory allocation.
#include <stdio.h>
#define MAX_SIZE 3
int A[MAX_SIZE][MAX_SIZE] = {{1,2,3},{4,5,6},{7,8,9}};
int B[MAX_SIZE][MAX_SIZE] = {{1,3,11},{10,1,2},{3,1,5}};
int Y[MAX_SIZE][MAX_SIZE];
void MatrixAddition(void)
{
int c,r,i,j;
// Initializing elements of matrix mult to 0.
for(i = 0; i < MAX_SIZE; ++i) for(j = 0; j < MAX_SIZE; ++j) Y[i][j]=0;
for(i = 0; i < MAX_SIZE; ++i)
{
for(j = 0; j < MAX_SIZE; ++j)
{
Y[i][j] = A[i][j] + B[i][j];
}
}
printf("\n\nDisplaying the Adition of Matrices: A + B: \n");
for(r=0;r<MAX_SIZE;r++)
{
for(c=0;c<MAX_SIZE;c++)
{
printf("\t%3d",Y[r][c]);
// cout<<"\t"<<Y[r][c];
}
printf("\n");
}
}
void MatrixSubstraction(void)
{
int c,r,i,j;
// Initializing elements of matrix mult to 0.
for(i = 0; i < MAX_SIZE; ++i) for(j = 0; j < MAX_SIZE; ++j) Y[i][j]=0;
for(i = 0; i < MAX_SIZE; ++i)
{
for(j = 0; j < MAX_SIZE; ++j)
{
Y[i][j] = A[i][j] - B[i][j];
}
}
printf("\n\nDisplaying the Substraction of Matrices: A - B: \n");
for(r=0;r<MAX_SIZE;r++)
{
for(c=0;c<MAX_SIZE;c++)
{
printf("\t%3d",Y[r][c]);
// cout<<"\t"<<Y[r][c];
}
printf("\n");
}
}
void CheckIdentical(void)
{
int Flag=1;
int c,r,i,j;
for(i = 0; i < MAX_SIZE; ++i)
{
for(j = 0; j < MAX_SIZE; ++j)
{
if(A[i][j] != B[i][j]) Flag=0;
}
}
if(Flag==1) printf("\n\nThe two matrices A and B are Identical");
else printf("\n\nThe two matrices A and B are NOT Identical");
}
main(void)
{
int c,r,i,j;
printf("\nMatrix-A: \n");
for(r=0;r<MAX_SIZE;r++)
{
for(c=0;c<MAX_SIZE;c++)
{
printf("\t%3d",A[r][c]);
}
printf("\n");
}
printf("\n\nMatrix-B: \n");
for(r=0;r<MAX_SIZE;r++)
{
for(c=0;c<MAX_SIZE;c++)
{
printf("\t%3d",B[r][c]);
}
printf("\n");
}
MatrixAddition();
MatrixSubstraction();
CheckIdentical();
return (0);
}
Comments
Thanks a lot !!
Leave a comment