-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMagicSquareMatrix.cpp
86 lines (75 loc) · 1.74 KB
/
MagicSquareMatrix.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
#include<iostream>
using namespace std;
class magic_square {
int a[10][10];
int n;
public:
magic_square() {
cout << "\nEnter 'N': ";
cin >> n;
}
void get() {
cout << "\nEnter the elements: ";
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> a[i][j];
}
}
}
bool row() {
int sum[10];
for (int i = 0; i < n; i++) {
sum[i] = 0;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
sum[i] = sum[i] + a[i][j];
}
}
for (int i = 0; i < n; i++) {
if (sum[0] != sum[i])
return false;
}
return true;
}
bool column() {
int sum[10];
for (int i = 0; i < n; i++) {
sum[i] = 0;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
sum[i] = sum[i] + a[j][i];
}
}
for (int i = 0; i < n; i++) {
if (sum[0] != sum[i])
return false;
}
return true;
}
bool diagnol() {
int d1, d2;
d1 = d2 = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j)
d1 = d1 + a[i][j];
if (i + j == n - 1)
d2 = d2 + a[i][j];
}
}
if (d1 != d2)
return false;
return true;
}
};
int main() {
magic_square m;
m.get();
if (m.diagnol() && m.row() && m.column())
cout << "\nIt is a magic matrix.";
else
cout << "\nIt is not a magic matrix.";
return 0;
}