Program:
#include<stdio.h>
//#include<conio.h>
int a[10][10],b[10][10],c[10][10],i,j,k;
void read(int r1,int c1,int a[10][10])
{
for(i=0;i<r1;++i)
for(j=0;j<c1;++j)
scanf("%d",&a[i][j]);
}
void mul(int r1,int c2,int c1)
{
for(i=0;i<r1;++i)
for(j=0;j<c2;++j)
{
c[i][j]=0;
for(k=0;k<c1;++k)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
void printmat(int r1,int c2)
{
for(i=0;i<r1;++i)
{
for(j=0;j<c2;++j)
printf("%d\t",c[i][j]);
printf("\n");
}
}
void main()
{
int r1,c1,r2,c2;//clrscr();
printf("Enter order of matrix 1:");
scanf("%d %d",&r1,&c1);
printf("Enter order of matrix 2:");
scanf("%d %d",&r2,&c2);
if(c1!=r2)
printf("Matrices cannot be multiplied");
else
{
printf("Enter Elements for matrix 1:\n");
read(r1,c1,a);
printf("Enter Elements for matrix 2:\n");
read(r2,c2,b);
mul(r1,c2,c1);
printf("Resultant matrix :\n");
printmat(r1,c2);
}
// getch();
}
Output:
nn@linuxmint ~ $ gcc c9.c
nn@linuxmint ~ $ ./a.out
Enter order of matrix 1:2
2
Enter order of matrix 2:2
2
Enter Elements for matrix 1:
1
1
1
1
Enter Elements for matrix 2:
2
2
2
2
Resultant matrix :
4 4
4 4
nn@linuxmint ~ $
#include<stdio.h>
//#include<conio.h>
int a[10][10],b[10][10],c[10][10],i,j,k;
void read(int r1,int c1,int a[10][10])
{
for(i=0;i<r1;++i)
for(j=0;j<c1;++j)
scanf("%d",&a[i][j]);
}
void mul(int r1,int c2,int c1)
{
for(i=0;i<r1;++i)
for(j=0;j<c2;++j)
{
c[i][j]=0;
for(k=0;k<c1;++k)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
void printmat(int r1,int c2)
{
for(i=0;i<r1;++i)
{
for(j=0;j<c2;++j)
printf("%d\t",c[i][j]);
printf("\n");
}
}
void main()
{
int r1,c1,r2,c2;//clrscr();
printf("Enter order of matrix 1:");
scanf("%d %d",&r1,&c1);
printf("Enter order of matrix 2:");
scanf("%d %d",&r2,&c2);
if(c1!=r2)
printf("Matrices cannot be multiplied");
else
{
printf("Enter Elements for matrix 1:\n");
read(r1,c1,a);
printf("Enter Elements for matrix 2:\n");
read(r2,c2,b);
mul(r1,c2,c1);
printf("Resultant matrix :\n");
printmat(r1,c2);
}
// getch();
}
Output:
nn@linuxmint ~ $ gcc c9.c
nn@linuxmint ~ $ ./a.out
Enter order of matrix 1:2
2
Enter order of matrix 2:2
2
Enter Elements for matrix 1:
1
1
1
1
Enter Elements for matrix 2:
2
2
2
2
Resultant matrix :
4 4
4 4
nn@linuxmint ~ $
0 comments:
Post a Comment