Write a C program to implement 2D Matrix Multiplication with output:
Here a 3 x 3 matrix multiplication is given you can change the value of m and n for any m x n matrix . Given program can multiply any number of 2-D matrix.
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,j,k,t,n=3,m=3,f,a1=3,b1=3,flag=1;
int a[3][3], b[3][50], c[3][50];
clrscr();
printf("enter A column wise:");
for(j=0;j<m;j++)
{
for(i=0;i<n;i++)
{
scanf("%d",&a[i][j]);
}
}
do
{
printf("enter B column wise:");
for(j=0;j<b1;j++)
{
for(i=0;i<a1;i++)
{
scanf("%d",&b[i][j]);
}
}
if(a1==m)
{
for(i=0;i<n;i++)
{
for(j=0;j<b1;j++)
{
c[i][j]=0;
for(k=0;k<a1;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
}
}
flag=0;
}
else
{
printf("dimension mismatch::::::::");
}
if(t==0)
{
printf("The transformed matrix is : \n");
}
for(i=0;i<n;i++)
{
for(j=0;j<b1;j++)
{
printf("%d\t",c[i][j]);
if(j==(b1-1))
printf("\n");
}
}
if(t!=0)
{
printf("do you want to multiply further?:\nenter\n 1 for yes\n0 for no ::: ");
scanf("%d",&f);
if(f==0)
{
flag=0;
}
else
{
flag=1;
for(i=0;i<a1;i++)
{
for(j=0;j<b1;j++)
{
a[i][j]=c[i][j];
}
}
printf("want to carry out other transformation?\nenter\n 1 for transformation\n0 for point ::: ");
scanf("%d",&f);
if(f==1)
b1=3;
else
{
printf("The transformation matrix is :\n");
for(i=0;i<n;i++)
{
for(j=0;j<b1;j++)
{
printf("%d\t",c[i][j]);
if(j==(b1-1))
printf("\n");
}
}
printf("enter no of points to be transformed:");
scanf("%d",&b1);
t=0;
}
}
}
else
flag=0;
}while(flag==1);
getch();
}
OUTPUT:
No comments:
Post a Comment