Results 1 to 7 of 7
  1. #1
    aspic is offline Member
    Join Date
    Dec 2013
    Posts
    2
    Rep Power
    0

    Default need help for magic square

    here is the code..

    public class Start {

    public static void main(String [] args){
    Scanner sc = new Scanner(System.in);
    int num = 1;
    int input;
    System.out.println("Enter number:");
    input = sc.nextInt();
    int limit = input;
    int a = input/2;
    int b = 0;
    int [][] arr = new int[input][input];
    do{
    for( num = 1; num<=limit; num++){
    if(arr[b][a]==0){
    arr[b][a] = num;
    if(b == 0){
    b = input;
    }
    if(a == input -1){
    a=-1;

    }
    a = a+1;
    b = b-1;

    }

    else{
    b=b+2;
    a = a-1;
    }
    if(b == input){
    b = 0;
    }
    if(b > input){
    b =1;
    }
    if(a == -1){
    a = input-1;
    }
    arr[b][a] = num;

    }
    num++;
    }while(num <= limit);

    for(int x =0;x<input;x++){
    for(int y = 0;y<input;y++){
    System.out.print(arr[x][y]);

    }
    System.out.println(" ");
    }

    }
    }

  2. #2
    aspic is offline Member
    Join Date
    Dec 2013
    Posts
    2
    Rep Power
    0

    Default need help for magic square

    please i need help!
    this is how i know the code work but definitely there's something wrong..
    with the operations and calculations


    public class Start {

    public static void main(String [] args){
    Scanner sc = new Scanner(System.in);
    int num = 1;
    int input;
    System.out.println("Enter number:");
    input = sc.nextInt();
    int limit = input;
    int a = input/2;
    int b = 0;
    int [][] arr = new int[input][input];
    do{
    for( num = 1; num<=limit; num++){
    if(arr[b][a]==0){
    arr[b][a] = num;
    if(b == 0){
    b = input;
    }
    if(a == input -1){
    a=-1;

    }
    a = a+1;
    b = b-1;

    }

    else{
    b=b+2;
    a = a-1;
    }
    if(b == input){
    b = 0;
    }
    if(b > input){
    b =1;
    }
    if(a == -1){
    a = input-1;
    }
    arr[b][a] = num;

    }
    num++;
    }while(num <= limit);

    for(int x =0;x<input;x++){
    for(int y = 0;y<input;y++){
    System.out.print(arr[x][y]);

    }
    System.out.println(" ");
    }

    }
    }

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,001
    Blog Entries
    7
    Rep Power
    20

    Default Re: need help for magic square

    Here's my old entry for the IOCCC; it doesn't even uses arrays for the generation of an odd sized magic square:

    Java Code:
    main(o,O0)char**O0;{int OO,O;O=--o?atoi(O0[!0]):!
    0;for(o=((OO=O*O)-O+!0+!0)>>!0;OO;o+=((--OO%O)?-!
    0:((((o-!0)%O)?O:0)+!0))-(((o-!0)%O)?O:0)){printf
    ("\n%*d "+!!(OO%O),!0<<!0<<!0,o+=(o<!0)?O*O:0);}}
    kind regards,

    Jos (<--- loves clean, readable code)
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,083
    Rep Power
    4

    Default Re: need help for magic square

    lol :) For shame, posting C code in a Java forum.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,001
    Blog Entries
    7
    Rep Power
    20

    Default Re: need help for magic square

    Quote Originally Posted by gimbal2 View Post
    lol :) For shame, posting C code in a Java forum.
    Java is a sissie language; you can't even use it for a proper obfuscation of your stuff; C, otoh, is the mother of all obfuscating languages ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default Re: need help for magic square

    Also posted at: Magic Square
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,931
    Rep Power
    4

    Default Re: need help for magic square

    Quote Originally Posted by JosAH View Post
    C, otoh, is the mother of all obfuscating languages ;-)
    Well, there's APL. Even a well formated APL program looks like someone tried to print a binary file.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. need help for magic square
    By aspic in forum New To Java
    Replies: 6
    Last Post: 12-31-2013, 11:36 AM
  2. Magic Square
    By danthegreat in forum New To Java
    Replies: 7
    Last Post: 10-06-2011, 01:53 AM
  3. Need Help with Magic Square
    By easybe in forum New To Java
    Replies: 10
    Last Post: 04-23-2010, 09:39 PM
  4. Magic Square!!!... :D
    By joms999 in forum New To Java
    Replies: 4
    Last Post: 02-25-2010, 07:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •