Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 78077ba

Browse files
Merge pull request #8 from razzat008/master
added matrix multiplication in Array Questions
2 parents 3466a07 + e7a814d commit 78077ba

File tree

2 files changed

+540
-154
lines changed

2 files changed

+540
-154
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// matrix multiplication
2+
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
int mult_matrix(int, int, int, int);
6+
7+
int main(int argc, char *argv[]) {
8+
int row1, column1, row2, column2;
9+
printf("================================\n");
10+
printf("Enter number of rows and columns for the first matrix:\n");
11+
scanf("%d%d", &row1, &column1);
12+
printf("================================\n");
13+
printf("Enter number of rows and columns for the second matrix:\n");
14+
scanf("%d%d", &row2, &column2);
15+
printf("================================\n");
16+
// check to see if any of the rows and columns are negative
17+
if (((row1 * column1) < 1) && ((row2 * column2) < 1)) {
18+
printf("Enter non-negative value.\n");
19+
exit(EXIT_FAILURE);
20+
} else {
21+
if (column1 != row2) {
22+
// check to see if the number of rows of first matrix is equal to the
23+
// number of columns of second matrix;
24+
printf("Number of column of first matrix must be equal the row of second "
25+
"matrix.\n");
26+
exit(EXIT_FAILURE);
27+
28+
} else {
29+
// if all the conditions are true, then call the mult_matrix function
30+
mult_matrix(row1, column1, row2, column2);
31+
}
32+
}
33+
34+
return 0;
35+
}
36+
37+
int mult_matrix(int row1, int column1, int row2, int column2) {
38+
int arr1[row1][column1], arr2[row2][column2];
39+
// take input for first matrix
40+
for (int i = 0; i < row1; i++) {
41+
for (int j = 0; j < column1; j++) {
42+
printf("For first matrix:\n");
43+
printf("Enter element number [%d][%d]:\n", i + 1, j + 1);
44+
scanf("%d", &arr1[i][j]);
45+
}
46+
}
47+
48+
printf("================================\n");
49+
// take input for second matrix
50+
for (int i = 0; i < row2; i++) {
51+
for (int j = 0; j < column2; j++) {
52+
printf("For second matrix:\n");
53+
printf("Enter element number [%d][%d]:\n", i + 1, j + 1);
54+
scanf("%d", &arr2[i][j]);
55+
}
56+
}
57+
printf("================================\n");
58+
59+
// multiplying the two matrixes
60+
int result[row1][column2];
61+
for (int i = 0; i < row1; i++) {
62+
for (int j = 0; j < column2; j++) {
63+
result[i][j] = 0;
64+
for (int k = 0; k < column1; k++) {
65+
// either column1 or row2 can be used
66+
result[i][j] += arr1[i][k] * arr2[k][j];
67+
}
68+
}
69+
}
70+
71+
// muliplied matrix
72+
// printf("Result of the product of those two matrixes\n");
73+
for (int i = 0; i < row1; i++) {
74+
for (int j = 0; j < column2; j++) {
75+
printf("%d\t", result[i][j]);
76+
}
77+
printf("\n");
78+
}
79+
80+
return 0;
81+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /