Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > General Coding
You last visited: Today at 17:03

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



JAVA MAGIC SQUARE

Discussion on JAVA MAGIC SQUARE within the General Coding forum part of the Coders Den category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: Dec 2009
Posts: 12
Received Thanks: 0
JAVA MAGIC SQUARE

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
zepolzirk is offline  
Old 12/03/2011, 14:19   #2
 
elite*gold: 115
Join Date: Oct 2007
Posts: 9,390
Received Thanks: 12,344
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;
}
ms​ is offline  
Thanks
1 User
Reply


Similar Threads Similar Threads
[JAVA Error] Could not create the java virtual machine
07/21/2013 - Technical Support - 10 Replies
Schönen Abend! Leider hat es sich aus einem unerfindlichen Grund ergeben, dass sobald ich die Minecraft.exe starten will die Errormeldung kommt. Die Tips auf Minecraft.net habe ich schon ohne Erfolg befolgt. Hoffe ihr könnt mir weiterhelfen... Mein PC:
[Java] Could not create the Java virtual machine
06/22/2011 - Minecraft - 1 Replies
hallo ihr minecraftler ^^ habe seit heute das problem das wenn ich minecraft starte original als auch cracked das diese fehlermeldung kommt: Java virtual machine Launcher Could not create the Java virtual machine
Square Map (DM)
01/04/2010 - S4 League - 25 Replies
YouTube - S4 League Square map (DM)



All times are GMT +2. The time now is 17:03.


Powered by vBulletin®
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2024 elitepvpers All Rights Reserved.