-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA20magicsquare.cpp
186 lines (146 loc) · 3.85 KB
/
A20magicsquare.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Magic Square
// By Emily Dayanghirang
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
const int ROWS = 3;
const int COLUMNS = 3;
const int DIAGONALS = 2;
const int NUMBER_OF_TOTALS = ROWS + COLUMNS + DIAGONALS;
int main()
{
const int UPPER_LIMIT = 9;
const int LOWER_LIMIT = 1;
const int RANGE = UPPER_LIMIT - LOWER_LIMIT + 1;
/*
// Grid for testing
// Use by making a comment block around
// the code for randomly generating a number
// for each square
int grid[ROWS][COLUMNS] = {{4, 9, 2},
{3, 5, 7},
{8, 1, 6}};
*/
int grid[ROWS][COLUMNS];
int totals[NUMBER_OF_TOTALS];
bool sumsAreEqual = 0;
int rowTotal(int [][COLUMNS], int);
int columnTotal(int [][COLUMNS], int);
int diagonalTopLeftToRightTotal(int [][COLUMNS]);
int diagonalTopRightToLeftTotal(int [][COLUMNS]);
bool check(int [], int);
srand(time(0));
// Randomly generate a number for each square
for(int i = 0; i < ROWS; i++)
{
for(int j = 0; j < COLUMNS; j++)
{
grid[i][j] = rand() % RANGE + LOWER_LIMIT;
}
}
// Print grid
for (int k = 0; k < ROWS; ++k)
{
for (int l = 0; l < COLUMNS; ++l)
{
cout << grid[k][l] << ' ';
}
cout << endl;
}
// Store totals in an array
for(int count = 0; count < 5; count++)
{
if(count < 3)
{
totals[count] = rowTotal(grid, count);
totals[count + 3] = columnTotal(grid, count);
}
else if(count < 4)
totals[count + 3] = diagonalTopLeftToRightTotal(grid);
else
totals[count + 3] = diagonalTopRightToLeftTotal(grid);
}
// Print row totals
cout << "\nTotal in each row: ";
for(int m = 0; m < ROWS; m++)
{
cout << totals[m] << " ";
}
// Print column totals
cout << endl
<< "\nTotal in each column: ";
for(int n = 0; n < COLUMNS; n++)
{
cout << totals[n + 3] << " ";
}
// Print diagonal totals
cout << endl
<< "\nTotal in each diagonal: ";
for(int o = 0; o < DIAGONALS; o++)
{
cout << totals[o + 6] << " ";
}
cout << endl;
// Check if totals are equal to one another
sumsAreEqual = check(totals, NUMBER_OF_TOTALS);
// Print result
if(sumsAreEqual)
cout << "\nThe grid is a Lo Shu Magic Square.\n";
else
cout << "\nThe grid is not a Lo Shu Magic Square.\n";
}
// Get row total
int rowTotal(int grid[][COLUMNS], int row)
{
int rowTotal = 0;
for(int column = 0; column < COLUMNS; column++)
{
rowTotal += grid[row][column];
}
return rowTotal;
}
// Get column total
int columnTotal(int grid[][COLUMNS], int column)
{
int columnTotal = 0;
for(int row = 0; row < ROWS; row++)
{
columnTotal += grid[row][column];
}
return columnTotal;
}
/* credits to Jesus Hilarioh for the
* functions for getting the sum of the
* diagonal lines
*/
// Get first diagonal total
int diagonalTopLeftToRightTotal(int grid[][COLUMNS])
{
int diagonalTopLeftToRightTotal = 0;
for(int i = 0; i < ROWS; i++)
diagonalTopLeftToRightTotal += grid[i][i];
return diagonalTopLeftToRightTotal;
}
// Get second diagonal total
int diagonalTopRightToLeftTotal(int grid[][COLUMNS])
{
int diagonalTopRightToLeftTotal = 0;
int column = (ROWS - 1);
for (int row = 0; row < ROWS; row++)
{
diagonalTopRightToLeftTotal += grid[row][column];
column--;
}
return diagonalTopRightToLeftTotal;
}
// Check if totals are equal to one another
bool check(int totals[], int number)
{
for(int count = 0; count < number; count++)
{
if(totals[count] != totals[0])
return false;
}
return true;
}