-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1022_EX02.cpp
More file actions
68 lines (56 loc) · 1.25 KB
/
Copy path1022_EX02.cpp
File metadata and controls
68 lines (56 loc) · 1.25 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
68
#include <iostream>
#include <iomanip>
#include<stdio.h>
#include<stdlib.h>
#define SIZE 20
using namespace std;
int main(){
int order, i, j, k, p, q, mid, magic[SIZE][SIZE];
cout << "Enter order of magic square (order must be odd): ";
cin >> order;
if(order%2==0){
cout << "Order must be odd.\n";
cout << "Exiting...";
exit(0);
}
mid = order/2;
for(i=0;i< order; i++){
for(j=0; j< order; j++){
magic[i][j]=0;
}
}
k=mid;
j=0;
for(i=1; i<= order*order; i++){
magic[j][k] = i;
p = j--;
q = k++;
if(j< 0){
j = order-1;
}
if(k>order-1){
k=0;
}
if(magic[j][k] != 0){
k = q;
j = p+1;
}
}
cout << "Generated MAGIC SQUARE is:\n";
for(i=1;i<=6*order;i++){
cout << "-";
}
cout << "\n";
for(j=0;j< order;j++){
cout << "|";
for(k=0; k< order; k++){
cout << setw(5) << magic[j][k] << " ";
}
cout << "\n";
for(i=1; i<=6*order; i++){
cout << "-";
}
cout << "\n";
}
return 0;
}