JAVA MAGIC SQUARE

12/01/2011 01:53 zepolzirk#1
Guys can you help me out with these? i use JAVA language

i want to make a magic square that the initial value would be 1, the 1 should always be at the 1st row and middle column and it should be look like this it only accepts odd numbers. I'm just a student and really wants to do this thing in our activity at school but so hard.

Input size of magic square:2
Invalid. Size should be odd

Input size again: 3

6 1 8
7 5 3
2 9 4

the logic in solving this magic square should be down down left only.

i tried to search in google but can't find anything that'll match what i'm searching for. Please help me out with these. Thanks!!! and more power to us
12/03/2011 14:19 ms​#2
One simple way to accomplish this is described in the Wikipedia-Article.

I've written a small program in C which does exactly that.

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

int main(int argc, char *argv[])
{
    unsigned size = 0;
    if (argc < 2 || sscanf(argv[1], "%u", &size) != 1 || size < 3 || size % 2 == 0) {
        char strnum[10];
        do {
            printf("\nEnter an odd number (> 1): ");
            fgets(strnum, 10, stdin);
        }
        while (sscanf(strnum, "%u", &size) != 1 || size < 3 || size % 2 == 0);
    }
    {
        unsigned long area = (unsigned long)size * (unsigned long)size;
        unsigned edge = size - 1;
        unsigned x = size >> 1;
        unsigned y = 0;
        unsigned long i = 2;
        unsigned long *c = calloc(area, sizeof (unsigned long));
        if (c == NULL) {
            printf("%s\n", "Failed to allocate memory!");
            return -1;
        }
        c[x] = 1;
        for (; i <= area; i++) {
            unsigned long offset;
            unsigned newx;
            unsigned newy;
            if (x != edge)
                newx = x + 1;
            else
                newx = 0;
            if (y != 0)
                newy = y - 1;
            else
                newy = edge;
            offset = newy * size;
            if (c[offset + (unsigned long)newx] != 0) {
                newx = x;
                if (y != edge)
                    newy = y + 1;
                else
                    newy = 0;
                offset = newy * size;
            }
            c[offset + (unsigned long)newx] = i;
            x = newx;
            y = newy;
        }
        {
            unsigned char padding = ceil(log10(area));
            char formatstr[10];
            sprintf(formatstr, "%%%dd ", padding);
            for (y = 0; y < size; y++) {
                unsigned offset = y * size;
                for (x = 0; x < size; x++)
                    printf(formatstr, c[offset + x]);
                printf("\n");
            }
        }
    }
    return 0;
}