-
Notifications
You must be signed in to change notification settings - Fork 0
/
2darray_mul_sum_div_sub.cpp
87 lines (74 loc) · 1.63 KB
/
2darray_mul_sum_div_sub.cpp
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int m, n, p, sum, array2d[4][4], array2d2[4][4], array2dans[4][4];
printf("Array Size: \n");
cin >> p;
m = n = p;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
cin >> array2d[i][j];
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
cin >> array2d2[i][j];
}
}
printf("\n\n");
/*
//2d traversing in the right way using my thought
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
printf("\t%d ", array2d[i][j]);
}
printf("\n");
}
printf("\n\n");
for (int i = 0; i < m; i++) {
for (int k = 0; k < n; k++) {
printf("\t%d ", array2d2[i][k]);
}
printf("\n");
}
*/
//multiplication of two 2d array elements
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
sum = 0;
for (int k = 0; k < p; k++) {
sum = sum + (array2d[i][k] * array2d2[k][j]);
}
array2dans[i][j] = sum;
}
}
printf("Multiplication Matrix: \n");
for (int i = 0; i < m; i++) {
for (int k = 0; k < n; k++) {
printf("\t%d ", array2dans[i][k]);
}
printf("\n");
}
//summation
printf("Summation Matrix: \n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
array2dans[i][j] = (array2d[i][j] + array2d2[i][j]);
printf("\t%d ", array2dans[i][j]);
}
printf("\n");
}
//Subtraction
printf("Subtracted Matrix: \n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
array2dans[i][j] = (array2d[i][j] - array2d2[i][j]);
printf("\t%d ", array2dans[i][j]);
}
printf("\n");
}
return 0;
}