-
Notifications
You must be signed in to change notification settings - Fork 1
/
magicSquare.c
58 lines (44 loc) · 1.27 KB
/
magicSquare.c
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
// magic_square.c
// To generate a magic square for nxn matrix (n is odd integer)
#include <stdio.h>
#define MAX_SIZE 15
void printMagic(int [][MAX_SIZE], int);
void generateMagic(int [][MAX_SIZE], int);
int main(void) {
int magic[MAX_SIZE][MAX_SIZE] = { {0} };
int size;
printf("Enter size of matrix: ");
scanf("%d", &size);
printf("%d\n", size);
generateMagic(magic, size);
printMagic(magic, size);
return 0;
}
// To generate the magic square
void generateMagic(int arr[][MAX_SIZE], int size) {
int num,row,col,next_row,next_col;
row = 0;
col = size/2;
arr[row][col] = 1;
for(num = 2; num<= size*size; num++){
next_row = (row -1 + size) % size;
next_col = (col + 1) % size;
//printf("Current next matrix: %d,%d\n",next_row,next_col);
if(arr[next_row][next_col] > 0)
row = (row + 1)%size; //Bring it back to top of matrix if necessary
else{
row = next_row;
col = next_col;
}
arr[row][col] = num;
}
}
// To print the magic square
void printMagic(int arr[][MAX_SIZE], int size) {
int row, col;
for (row=0; row<size; row++) {
for (col=0; col<size; col++)
printf("%5d", arr[row][col]);
printf("\n");
}
}