# Thread: Help with writing a program that creates an odd n*n magic square where n is...

1. ## Help with writing a program that creates an odd n*n magic square where n is...

I am in a beginning C programming class and I have this assignment: Write a program that creates an N*N magic square, i.e., a square arrangement of the numbers 1,2,…,N^2 in which the sum of rows, columns, and diagonals are the same. The user will specify the size of the square matrix: N. The value N must be an odd number between 1 and 15.

I am using visual studio 2010. Also I am only allowed to use C89. My program will not compile and I have been trying to fix my problems, but I still have a few left that I cannot figure out. I understand that my main problem is visual studio doesn't support variable array size so I tried to compensate for this, but clearly I failed. I would really appreciate any help because this program is really frustrating me!

Thank you!

PHP Code:
```  error C2057: expected constant expression1 error C2466: cannot allocate an array of constant size 0error C2057: expected constant expression error C2466: cannot allocate an array of constant size 0 error C2087: 'magic_square' : missing subscripterror C2133: 'magic_square' : unknown sizeerror C2143: syntax error : missing ';' before ')'error C2143: syntax error : missing ';' before ')'  ```

Code:
```#include <stdio.h>#define array_size 15

int main (void)
{
int row, column, row1, column1, size, n = 2, magic_square [array_size][array_size];

for ( ; ; ) {

printf ("Enter size of magic square: ");

scanf ("%d", &size);

if (size % 2 == 0)
printf ("Enter an odd number!\n");

else if (size <= 0 || size > 15)
printf ("Enter a number between 1 and 15!\n");

else {

int magic_square [size][size];

for (row = 0; row < size; row++)
for (column = 0; column < size; column++)
magic_square [row][column] = 0;

row = 0;
column = (size/2);
magic_square [row][column] = 1;

for (n, n<= size*size, n++) {
if (--row < 0)
row = (size -1);
if (++column > size -1)
col = 0;
if (magic_square [row][column] != 0) {
if (++row > (size -1))
row = 0;
if (--column < 0)
col = size - 1;
while (magic_square [row][column] != 0)
if (++row > (size -1))
row = 0;
}

magic_square [row][column] = n;
}

for (row = 0; row < size; row++)
for (column = 0; column < size; column++)
printf ("%4d\n", magic_square [row][column]);
}
}

return 0;
}```

2. Easy solution: just define the array with a big enough size (15x15) and ignore the unused elements if the user asks for a smaller one.

3. for (n, n<= size*size, n++) You sure you want to do this? Just a suggestion, but start with a 3X3 array to see if you have the code working properly

4. I thought I did define the array with my header: #define array_size 15

5. Okay actually I think I understsand what you're saying. I changed my magic_square [size][size] to magic_square [array_size][array_size], but I still have this error twice: error C2143: syntax error : missing ';' before ')'

6. Hint: Refer to post #3 above.