A Matrix Multiplication program is provided below.
Only when the number of columns in the first matrix equals the number of rows in the second matrix can two matrices with the same order be multiplied.
#include<stdio.h>
int main()
{
int n, m, c, d, p, q, k, first[10][10], second[10][10], pro[10][10],sum = 0;
printf("\nEnter the number of rows and columns of the first matrix: \n\n");
scanf("%d%d", &m, &n);
printf("\nEnter the %d elements of the first matrix: \n\n", m*n);
for(c = 0; c < m; c++) // to iterate the rows
for(d = 0; d < n; d++) // to iterate the columns
scanf("%d", &first[c][d]);
printf("\nEnter the number of rows and columns of the first matrix: \n\n");
scanf("%d%d", &p, &q);
if(n != p)
printf("Matrices with the given order cannot be multiplied with each other.\n\n");
else
{
printf("\nEnter the %d elements of the second matrix: \n\n",m*n);
for(c = 0; c < p; c++) // to iterate the rows
for(d = 0; d < q; d++) // to iterate the columns
scanf("%d", &second[c][d]);
// printing the first matrix
printf("\n\nThe first matrix is: \n\n");
for(c = 0; c < m; c++) // to iterate the rows
{
for(d = 0; d < n; d++) // to iterate the columns
{
printf("%d\t", first[c][d]);
}
printf("\n");
}
// printing the second matrix
printf("\n\nThe second matrix is: \n\n");
for(c = 0; c < p; c++) // to iterate the rows
{
for(d = 0; d < q; d++) // to iterate the columns
{
printf("%d\t", second[c][d]);
}
printf("\n");
}
for(c = 0; c < m; c++) // to iterate the rows
{
for(d = 0; d < q; d++) // to iterate the columns
{
for(k = 0; k < p; k++)
{
sum = sum + first[c][k]*second[k][d];
}
pro[c][d] = sum; // resultant element of pro after multiplication
sum = 0; // to find the next element from scratch
}
}
// printing the elements of the product matrix
printf("\n\nThe multiplication of the two entered matrices is: \n\n");
for(c = 0; c < m; c++) // to iterate the rows
{
for(d = 0; d < q; d++) // to iterate the columns
{
printf("%d\t", pro[c][d]);
}
printf("\n"); // to take the control to the next row
}
}
return 0;
}
Output:
Enter the number of rows and columns of the first matrix:
2 2
Enter the 4 elements of the first matrix:
1 2 3 4
Enter the number of rows and columns of the first matrix:
2 2
Enter the 4 elements of the second matrix:
9 8 7 6
The first matrix is:
1 2
3 4
The second matrix is:
9 8
7 6
The multiplication of the two entered matrices is:
23 20
55 48
Note: also read about the Miscellaneous Program(Remove Duplicate Elements)
If you like my post please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…