Write a program to perform matrix multiplication by passing input matrix to the function and printing resultant matrix.
Advertisement Remove all ads
Solution
Program :-
Sourse code :
//Program for matrix multiplication using functions.
#include <stdio.h>
#include <stdlib.h>
void input(int m, int n, int a[m][n])
{
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
printf("%d, %d : ", i, j);
scanf("%d", &a[i][j]);
}
}
}
void print(int m, int n, int a[m][n])
{
int i, j;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("%3d ", a[i][j]);
}
printf("\n");
}
}
void multiply(int m, int n, int p, int a[m][n], int b[n][p], int c[m][p])
{
for (int i = 0; i < m; i++) {
for (int j = 0; j < p; j++) {
c[i][j] = 0;
for (int k = 0; k < n; k++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
}
void main()
{
int r1, c1, r2, c2;
printf("Row and column for matrix #1 :\n");
scanf("%d %d", &r1, &c1);
printf("Row and column for matrix #2 :\n");
scanf("%d %d", &r2, &c2);
if (r2 != c1) {
printf("The matrices are incompatible.\n");
exit(EXIT_FAILURE);
}
int mat1[r1][c1], mat2[r2][c2], ans[r1][c2];
printf("Enter elements of the first matrix.\n");
input(r1, c1, mat1);
printf("The elements of the first matrix are :\n");
print(r1, c1, mat1);
printf("Enter elements of the second matrix.\n");
input(r2, c2, mat2);
printf("The elements of the second matrix are :\n");
print(r2, c2, mat2);
multiply(r1, r2, c2, mat1, mat2, ans);
printf("The product is :\n");
print(r1, c2, ans);
getch( );
}
Row and column for matrix #1 : 2 2 Row and column for matrix #2 : 2 2 Enter elements of the first matrix. 0, 0 : 1 0, 1 : 17 1, 0 : 25 1, 1 : 30 The elements of the first matrix are : 1 17 25 30 Enter elements of the second matrix. 0, 0 : 74 0, 1 : 89 1, 0 : 3 1, 1 : 65 The elements of the second matrix are : 74 89 3 65 The product is : 125 1194 1940 4175 |
Concept: Control Structures - Looping
Is there an error in this question or solution?
Advertisement Remove all ads
APPEARS IN
Advertisement Remove all ads
Advertisement Remove all ads