# Thread: help on magic square

1. ## Help on magic square

Code:
```#include <stdio.h>
#include <time.h>
#include <math.h>
#include <stdlib.h>

int RandomInt(int n);
void FillSquare (int sq, int n);
int IsMagic(int sq, int n);
void PrintSquare(int sq, int n);

int
main(){

int n;
srand(time(NULL));

/* Ask for the size of the magic square */

printf("Enter the size of magic square, between 3 and 5: ");
scanf("%d", &n);

/*  if the number entered isn't between 3 and 5 the program will exit. */

if(n < 3 || n >5)
{printf("number not between 3 and 5, please try again.");
return 0;
}
else
{
int sq;
int IsMagicTrue=0;
while (!IsMagicTrue)
{
FillSquare(sq, n);
IsMagicTrue=IsMagic(sq, n);
}
PrintSquare(sq,n);
return 0;
}}

/* Creating random integers from 1 to n */

int RandomInt(int n)
{
int r;

r=1+rand()%n;
return r;
}

/* Placing the integers in random spots of the Magic Square that are unoccupied */

void FillSquare (int sq, int n) {
int x;
int y;
int currentnum;
for(x = 0; x < n; x++)
{
for (y = 0; y < n; y++)
{sq[x][y]=0;
}}

currentnum = (n*n);
x = RandomInt(n)-1;
y = RandomInt(n)-1;
while(currentnum > 0)

/* This loop continues until magic square is complete */

{if(sq[x][y] == 0)
{sq[x][y] = currentnum;
currentnum--;
}
x = RandomInt(n)-1;
y = RandomInt(n)-1;
}}

int IsMagic(int sq, int n)
{

/* Checking each row to see if it is equal to (((n^3)+n)/2) */

int total=0;
int x;
int y;
int total2=0;
for(x = 0; x < n; x++)
{for(y = 0; y < n; y++)
{
total += sq[x][y];
}
if(total != (((n*n*n)+n)/2))
{return 0;}
total=0;
}

/* Checking each column to see if it is equal to (((n^3)+n)/2) */
total=0;
for(y = 0; y < n; y++)
{for(x = 0; x < n; x++)
{
total += sq[x][y];
}
if(total != (((n*n*n)+n)/2))
{return 0;}
total=0;
}

/* Checking each diagonal to see if it is equal to (((n^3)+n)/2) */

total =0;

for(x = 0; x < n; x++)
{
total += sq[x][x];
total2 += sq[n-1-x][x];
}
if(total != (((n*n*n)+n)/2))
{return 0;}

/* Finished all the checks */
return 1;
}

/* This will print the magic square */

void PrintSquare(int sq, int n)
{
int x;
int y;
for(x = 0; x < n; x++)
{
for(y = 0; y < n; y++)
{
printf(" %d ", sq[x][y]);
}
printf("\n");
}
}``` 2. ## help on magic square

i got this code to make magic squares, but how do i make it so that they aren't the same each time?
Code:
```#include <stdio.h>

enum {
MAXIMUM_SIZE = 5
};
enum{
MINIMUM_SIZE = 3
};

int
main(void) {
int square[MAXIMUM_SIZE][MAXIMUM_SIZE];
int x,y,i,j,size;

printf("Enter the size of the magic square you wish to create: ");
scanf("%d", &size);

if (size > MAXIMUM_SIZE) {
printf("Sorry the size has to be smaller than %d\n", MAXIMUM_SIZE);
return 1;
}

if (size < MINIMUM_SIZE ) {
printf("Sorry the size has to be greater than %d\n", MINIMUM_SIZE);
return 1;
}

x = size*size + size/2;
y = size*size;

for (i = 1; i <= size*size; i++) {
square[x % size][y % size] = i;
if (i % size == 0) {
y++;
} else {
x--;
y--;
}
}

for (i = 0; i < size; i++) {
for (j = 0; j < size; j++)
printf("%4d ", square[j][i]);
printf("\n");
}

return 0;
}``` 3. In the future please keep all posts that relate to the same problem in one thread. Popular pages Recent additions 