By executing following C program a user can multiply a matrix and also can get transpose of it. In this program total 13 for loops are used.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,m,n,p,q,k,sum=0; /* Declaring sum=0 since we use it as a temporary variable while multiplication */
int first[10][10],second[10][10],multiply[10][10],transpose[10][10];
/* Accepting 1st matrix */
printf("Enter the number of rows and columns of first matrix\n");
scanf("%d%d",&m,&n);
printf("Enter the elements of first matrix\n");/* Order (m,n) */
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&first[i][j]);
}
}
/* Accepting 2nd Matrix */
printf("Enter the number of rows and columns of second matrix\n");
scanf("%d%d",&p,&q);
if(n!=p) /* To multiply Matrices, Number of columns of 1st matrix should be equal to number of rows in 2nd matrix */
printf("matrix with entered order cant be multiplied with each other.\n");
else /* Accepting 2nd Matrix */
{
printf("Enter the element of second matrix\n"); /* Order (p,q) */
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",&second[i][j]);
}
}
/*matrix Multiplication*/
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
for(k=0;k<p;k++)
{
sum=sum+first[i][k]*second[k][j]; /* calculating and storing final value of element in sum */
}
multiply[i][j]=sum; /* Assigning value stored in sum to Matrix: multiply[i][j] */
sum=0; /* Again we will make sum=0 since we want to multiply next element */
}
}
/* Printing multiplied matrix */
printf("Product of entered matrices\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",multiply[i][j]);
}
printf("\n");
}
}
/* transpose of matrix */
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
transpose[i][j]=multiply[j][i];
/*for example i=1 and j=2 then in matrix, element (1,2) will be swapped with (2,1) thus we get transpose of matrix*/
}
}
/* Printing transposed matrix */
printf("Transpose of the matrix \n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d\t",transpose[i][j]);
}
printf("\n"); /* \n to bring cursor to next line and print elements in matrix format */
}
/* Matrix Multiplication and its Transpose in C by SlashMyCode.com */
getch();
}
#include<conio.h>
void main()
{
int i,j,m,n,p,q,k,sum=0; /* Declaring sum=0 since we use it as a temporary variable while multiplication */
int first[10][10],second[10][10],multiply[10][10],transpose[10][10];
/* Accepting 1st matrix */
printf("Enter the number of rows and columns of first matrix\n");
scanf("%d%d",&m,&n);
printf("Enter the elements of first matrix\n");/* Order (m,n) */
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&first[i][j]);
}
}
/* Accepting 2nd Matrix */
printf("Enter the number of rows and columns of second matrix\n");
scanf("%d%d",&p,&q);
if(n!=p) /* To multiply Matrices, Number of columns of 1st matrix should be equal to number of rows in 2nd matrix */
printf("matrix with entered order cant be multiplied with each other.\n");
else /* Accepting 2nd Matrix */
{
printf("Enter the element of second matrix\n"); /* Order (p,q) */
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",&second[i][j]);
}
}
/*matrix Multiplication*/
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
for(k=0;k<p;k++)
{
sum=sum+first[i][k]*second[k][j]; /* calculating and storing final value of element in sum */
}
multiply[i][j]=sum; /* Assigning value stored in sum to Matrix: multiply[i][j] */
sum=0; /* Again we will make sum=0 since we want to multiply next element */
}
}
/* Printing multiplied matrix */
printf("Product of entered matrices\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",multiply[i][j]);
}
printf("\n");
}
}
/* transpose of matrix */
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
transpose[i][j]=multiply[j][i];
/*for example i=1 and j=2 then in matrix, element (1,2) will be swapped with (2,1) thus we get transpose of matrix*/
}
}
/* Printing transposed matrix */
printf("Transpose of the matrix \n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d\t",transpose[i][j]);
}
printf("\n"); /* \n to bring cursor to next line and print elements in matrix format */
}
/* Matrix Multiplication and its Transpose in C by SlashMyCode.com */
getch();
}
Output:
Flowchart:
Click on image to enlarge |
If this post was helpful for you and too get more programs and daily interesting feeds stay connected with us on facebook and twitter :)
No comments:
Post a Comment
Feel free to ask us any question regarding this post