-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
104 lines (80 loc) · 2.35 KB
/
main.c
File metadata and controls
104 lines (80 loc) · 2.35 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
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
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 15
typedef struct{
int row;
int col;
}LOC;
void PrintSquare(int square[][MAX_SIZE], int size){
int row, col;
printf("Print Magic Square!\n\n");
for(row = 0; row < size; row++){
for(col = 0; col < size; col++)
printf("%5d", square[row][col]);
printf("\n");
}
}
void FillSquare(int square[][MAX_SIZE], int size, int curNum, LOC curLoc){//find next location to fill compared with curLoc and fill the curNum.
if(curNum == (size * size) + 1){
return;
}
else{
curLoc.row = ((curLoc.row - 1) < 0) ? (size - 1) : (curLoc.row - 1);
curLoc.col = ((curLoc.col - 1) < 0) ? (size - 1) : (curLoc.col - 1);
if(square[curLoc.row][curLoc.col]){//occupied.
curLoc.row = (curLoc.row + 2) % size;
curLoc.col = (curLoc.col + 1) % size;
}
square[curLoc.row][curLoc.col] = curNum;
FillSquare(square, size, curNum + 1, curLoc);
}
}
void InitSquare(int square[][MAX_SIZE], int size){
int row, col;
for(row = 0; row < size; row++)
for(col = 0; col < size; col++)
square[row][col] = 0;
}
int IsAcceptable(int size){ //return 1 to right, return -1 out of range, return -2 for even
if(size > MAX_SIZE || size < 1){
return -1;
}
else if(size % 2 == 0){
return -2;
}
else{
return 1;
}
}
int GetSquareSizeByUser(){
int size;
printf("Enter the size of the square: ");
scanf("%d", &size);
switch(IsAcceptable(size)){
case 1:
return size;
break;
case -1:
fprintf(stderr, "ERROR! Size is out of range\n");
exit(EXIT_FAILURE);
break;
case -2:
fprintf(stderr, "ERROR! Size is even\n");
exit(EXIT_FAILURE);
break;
}
fprintf(stderr, "ERROR! Abnormal exit in GetSquareSizeByUser()\n");
exit(EXIT_FAILURE);
}
int main(){
int square[MAX_SIZE][MAX_SIZE];
int size;
LOC curLoc;
size = GetSquareSizeByUser();
InitSquare(square, size);
curLoc.row = 0;
curLoc.col = (size - 1)/2;
square[curLoc.row][curLoc.col] = 1;
FillSquare(square, size, 2, curLoc);
PrintSquare(square, size);
}