-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrixMult.c
More file actions
executable file
·67 lines (57 loc) · 1.59 KB
/
matrixMult.c
File metadata and controls
executable file
·67 lines (57 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <stdio.h>
#include "./matrixMult.h"
// source: https://www.programmingsimplified.com/c-program-multiply-matrices
int main()
{
int m, n, p, q, c, d, k, sum = 0;
int counter = 0; // to give each of the matrices different values
printf("Enter number of rows and columns of first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter elements of first matrix\n");
int first[m][n];
for (c = 0; c < m; c++)
for (d = 0; d < n; d++){
first[c][d] = counter;
counter += 1;
}
printf("Enter number of rows and columns of second matrix\n");
scanf("%d%d", &p, &q);
int second[p][q];
int multiply[m][q];
if (n != p)
printf("The matrices can't be multiplied with each other.\n");
else
{
//printf("Enter elements of second matrix\n");
counter = 0; // it will get large really fast if we don't reset it to 0.
for (c = 0; c < p; c++)
for (d = 0; d < q; d++){
second[c][d] = counter;
counter += 1;
}
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++) {
for (k = 0; k < p; k++) {
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}
printf("Product of the matrices without function:\n");
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++)
printf("%d\t", multiply[c][d]);
printf("\n");
}
printf("Product of the matrices with function:\n");
setMatricesDimensions(m, p, n, q);
matrixMult(first, second, multiply);
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++)
printf("%d\t", multiply[c][d]);
printf("\n");
}
}
return 0;
}