Hi rmvprasad,

You need a recursive solution.

Consider a 3x3 square that will contain the digits 1 through 9. (You can also use 0 through 8 or any other combination of digits that you want.)

The easiest implementation is to use a buffer of N*N elements. In this case 3*3=9 so a buffer of 9 integers will suffice.

Now place the 9 digits into the buffer in any order that you want. (Sequentially is a good start because it makes for easy debugging.)

This array looks like:

Array[0] == 1;

Array[1] == 2;

Array[2] == 3;

Array[3] == 4;

...

Array[8] == 9;

Test the array. You'll have to implement two dimensions overlayed on top of the array. If the array is a magic square, print it.

If not, swap that last two digits. The array now contains 1 2 3 4 5 6 7 9 8.

Text the array. If the array is a magic square, print it.

If not, swap the next two digits. The array now contains 1 2 3 4 5 6 7 8 9.

etc..

You really need a recursive function to manage the position within the array and to perform the swapping.

Good Luck!

Kent

You need a recursive solution.

Consider a 3x3 square that will contain the digits 1 through 9. (You can also use 0 through 8 or any other combination of digits that you want.)

The easiest implementation is to use a buffer of N*N elements. In this case 3*3=9 so a buffer of 9 integers will suffice.

Now place the 9 digits into the buffer in any order that you want. (Sequentially is a good start because it makes for easy debugging.)

This array looks like:

Array[0] == 1;

Array[1] == 2;

Array[2] == 3;

Array[3] == 4;

...

Array[8] == 9;

Test the array. You'll have to implement two dimensions overlayed on top of the array. If the array is a magic square, print it.

If not, swap that last two digits. The array now contains 1 2 3 4 5 6 7 9 8.

Text the array. If the array is a magic square, print it.

If not, swap the next two digits. The array now contains 1 2 3 4 5 6 7 8 9.

etc..

You really need a recursive function to manage the position within the array and to perform the swapping.

Good Luck!

Kent