DIY C Program on Matrix Multiplication (http://www.bloggerspot.tk)



Matrix Multiplication

DIY C Program on Matrix Multiplication (http://www.bloggerspot.tk)



Write a C program to perform matrix multiplication. Assume only square matrices of the same dimension.

Input Format:
The input consists of (2*(m*m)+1) integers. The first integer corresponds to m, the number of rows /
columns in the matrix . The next m*m integers correspond to the elements in the first matrix. The last m*m
elements correspond to the elements in the second matrix. The elements are read in rowwise order, first
row first, then second row and so on. Assume that the maximum value of m is 10.

Output Format:
Refer sample output for details.

Sample Input 1:
2
4 5
6 9
1 2
3 4

Sample Output 1:
19 28
33 48

Solution:

#include<stdio.h>
int main()
{
int m, a[10][10], b[10][10], mul[10][10], i, j, k, sum = 0;
scanf("%d", &m);
for(i = 0; i < m; i++)
{
for(j = 0; j < m; j++)
{
scanf("%d", &a[i][j]);
}
}
for(i = 0; i < m; i++)
{
for(j = 0; j < m; j++)
{
scanf("%d", &b[i][j]);
}
}
for(i = 0; i < m; i++)
{
for(j = 0; j < m; j++)
{
for(k = 0; k < m; k++)
{
sum = sum + a[i][k] * b[k][j];
}
mul[i][j] = sum;
sum = 0;
}
}
for(i = 0; i < m; i++)
{
for(j = 0; j < m; j++)
{
printf("%d ", mul[i][j]);
}
printf("\n");
}
return 0;
}

No comments:

Post a Comment

Your Comments here.

© Copyright 2015. Website by Way2themes - Published By Gooyaabi Templates