-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagicalSquare.cpp
66 lines (64 loc) · 1.4 KB
/
magicalSquare.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
/**********************
Program Name : Generate magical square using cpp
Author : Rajendra Pancholi
Date of creation : 19/10/2024
Organization : NIT Patna
***********************/
#include <iostream>
#include <iomanip>
using namespace std;
void magicSquare(int n, int row, int col)
{
int i = row;
int j = col;
int mSquare[n][n];
// Initial value is 0
for (int in = 0; in < n; in++)
{
for (int jn = 0; jn < n; jn++)
{
mSquare[in][jn] = 0;
}
}
// Insert the value in magic square.
for (int num = 1; num <= n * n; num++)
{
mSquare[i][j] = num;
int newi = (i - 1 + n) % n;
int newj = (j + 1) % n;
if (mSquare[newi][newj])
{
i = (i + 1) % n;
}
else
{
i = newi;
j = newj;
}
}
// print the mSquare
for (int in = 0; in < n; in++)
{
for (int jn = 0; jn < n; jn++)
{
cout << setw(2) << mSquare[in][jn] << " ";
}
cout << endl;
}
int sum = 0;
for (int i = 0; i < n; i++)
{
sum += mSquare[0][i];
}
cout << "sum is: " << sum << endl;
}
int main()
{
int n, i, j;
cout << "Enter magical square size: ";
cin >> n;
cout << "Enter starting point(i,j) of the square: ";
cin >> i >> j;
magicSquare(n, i, j);
return 0;
}