0
868views
Write a program to compute matrix multiplication and transpose of a matrix.

Mumbai University> FE > Sem 2> STRUCTURED PROGRAMMING APPROACH

Marks: 10 M

Year: Dec 2016

1 Answer
0
1views
#include<stdio.h>
#include<math.h>
#include<conio.h>

main()
{
int a[10][10],b[10][10],c[10][10],t[10][10],m,n,p;
int i,j,k,sum=0;
printf("Enter number of rows of matrix 1: ");
scanf("%d",&m);
printf("Enter number of columns of matrix 1: ");
scanf("%d",&n);
printf("Enter number of columns of matrix 2: ");
scanf("%d",&p);
printf("Enter the elements of First matrix:\n");
//Read First Matrix
for(i=0;i<m;i++)
{
    for(j=0;j<n;j++)
    {
        printf("Enter Element: ");
        scanf("%d",&a[i][j]);
    }
}
//Read Second Matrix
printf("Enter the elements of Second matrix:\n");
for(i=0;i<n;i++)
{
    for(j=0;j<p;j++)
    {
        printf("Enter Element: ");
        scanf("%d",&b[i][j]);
    }
}
//Matrix Multiplication
for(i=0;i<m;i++)
{
    for(j=0;j<n;j++)
    {
        for(k=0;k<p;k++)
        {
            sum=sum+((a[i][k])*(b[k][j]));
            c[i][j]=sum;
        }
        sum=0;
    }
}

//Display Matrix after multiplication
printf("Multiplication of matrix is:\n");
for(i=0;i<m;i++)
{
    for(j=0;j<p;j++)
    {
        printf("%d\t",c[i][j]);
    }
printf("\n");
}

//transpose of matrix c:
for(i=0;i<m;i++)
{
    for(j=0;j<p;j++)
    {
        t[j][i]=c[i][j];
    }
}

//Display Matrix C after transpose
printf("Transpose of matrix is:\n");
for(i=0;i<p;i++)
{

    for(j=0;j<m;j++)
    {
        printf("%d\t",t[i][j]);
    }
printf("\n");
}
getch();
}

Output:
Enter number of rows of matrix 1: 2
Enter number of columns of matrix 1: 2
Enter number of columns of matrix 1: 2
Enter element of First matrix:
Enter Element: 1
Enter Element: 2
Enter Element: 3
Enter Element: 2
Enter element of Second matrix:
Enter Element: 2
Enter Element: 3
Enter Element: 4
Enter Element: 1
Multiplication of matrix is:
10  5
14  11
Transpose of matrix is:
10  14
5   11
Please log in to add an answer.

Continue reading...

The best way to discover useful content is by searching it.

Search