Access Millions of academic & study documents

5 Write a C program to read matrix A and matrix B from the screen t

Content type
User Generated
Showing Page:
1/4
5. Write a C program to read matrix A and matrix B from
the screen then calculate their product based on matrix
multiplication rules.
Solution
Program:
#include <stdio.h>
#include <math.h>
int main()
{
int m, n, p, q, c, d, k, sum = 0;
int A[10][10], B[10][10], product[10][10];
printf(\"Enter the number of rows and columns of matrix
A:\ \");
scanf(\"%d%d\", &m, &n);
printf(\"Enter the elements of matrix A\ \");
for (c = 0; c < m; c++) //row of matrix A
for (d = 0; d < n; d++)//column of matrix A
scanf(\"%d\", &A[c][d]);//scanning elements of matrix A

Sign up to view the full document!

lock_open Sign Up
Showing Page:
2/4
printf(\"Enter the number of rows and columns of matrix
B:\ \");
scanf(\"%d%d\", &p, &q);
if (n != p)// checking the column of matrix A is same as
row of matrix B
{
printf(\"Matrix mutiplication is not possible\");
printf(\"\ Column of matrix A must be same as row of
matrix B\");
}
else
{
printf(\"Enter the elements of matrix B\ \");
for (c = 0; c < p; c++)//row of matrix B
for (d = 0; d < q; d++)//column of matrix B
scanf(\"%d\", &B[c][d]);//scanning elements of matrix B
for (c = 0; c < m; c++)
{
for (d = 0; d < q; d++)
{
for (k = 0; k < p; k++)

Sign up to view the full document!

lock_open Sign Up
Showing Page:
3/4

Sign up to view the full document!

lock_open Sign Up
End of Preview - Want to read all 4 pages?
Access Now
Unformatted Attachment Preview
5. Write a C program to read matrix A and matrix B from the screen then calculate their product based on matrix multiplication rules. Solution Program: #include #include int main() { int m, n, p, q, c, d, k, sum = 0; int A[10][10], B[10][10], product[10][10]; printf(\"Enter the number of rows and columns of matrix A:\ \"); scanf(\"%d%d\", &m, &n); printf(\"Enter the elements of matrix A\ \"); for (c = 0; c < m; c++) //row of ma trix A for (d = 0; d < n; d++)//column of matrix A scanf(\"%d\", &A[c][d]);//scanning elements of matrix A printf(\"Enter the number of rows and columns of matrix B:\ \"); scanf(\"%d%d\", &p, &q); if (n != p)// checking the column of matrix A is sam e as row of matrix B { printf(\"Matrix mutiplication is not possible \"); printf(\"\ Column of matrix A must be same as row of matrix B\"); } else { printf(\"Enter the elements of matrix B\ \"); for (c = 0; c < p; c++)//row of matrix B for (d = 0; d < q; d++)//column of matrix B scanf(\"%d\", &B[c][d]);//scanning elements of matrix B for (c = 0; c < m; c++) { for (d = 0; d < q; d++) { for (k = 0; k < p; k++) { sum = sum + A[c][k]*B[k][d];// multiplying rows of matrix A with column of ...
Purchase document to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.
Studypool
4.7
Indeed
4.5
Sitejabber
4.4