Access over 20 million homework & study documents

search

matrix multiplicarion C program

Subject

Programming

Type

Lab

Rating

Showing Page:
1/2
PROGRAM:
#include<stdio.h>
int main()
{
int i,j,k,m,n,p,q;
printf("\nEnter the number of rows of first matrix:\n");
scanf("%d",&m);
printf("Enter the number of columns of the first matrix:\n");
scanf("%d",&n);
printf("\nEnter the number of rows of the second matrix:\n");
scanf("%d",&p);
printf("\nEnter the number of columns of the second matrix:\n");
scanf("%d",&q);
if(n!=p)
{
printf("\nINVALID:number of columns of 1st matrix must be equal
to number of rows of 2nd matrix to be multiplied.\n");
exit(0);
}
int a[m][n],b[p][q],c[m][q];
printf("Enter %d elements of the first matrix:\n",(m*n));
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("The first matrix is:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("Enter %d elements of the second matrix:\n",(p*q));
for(i=0;i<p;i++)
{

Sign up to view the full document!

lock_open Sign Up
for(j=0;j<q;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("The second matrix is:\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("%d ",b[i][j]);
}
printf("\n");
}
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
}
}
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
for(k=0;k<n;k++)
{
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
}
}
printf("The multiplied matrix:\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d ",c[i][j]);
}
printf("\n");
}
return 0;
}

Sign up to view the full document!

lock_open Sign Up

Unformatted Attachment Preview

PROGRAM:#includeint main() { int i,j,k,m,n,p,q; printf("\nEnter the number of rows of first matrix:\n"); scanf("%d",&m); printf("Enter the number of columns of the first matrix:\n"); scanf("%d",&n); printf("\nEnter the number of rows of the second matrix:\n"); scanf("%d",&p); printf("\nEnter the number of columns of the second matrix:\n"); scanf("%d",&q); if(n!=p) { printf("\nINVALID:number of columns of 1st matrix must be equal to number of rows ...
Purchase document to see full attachment

Anonymous
Really helpful material, saved me a great deal of time.

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4