Thread: Help with magic square program

  1. #1
    Unregistered
    Guest

    Help with magic square program

    Hi All,
    I am writing a magic square code. I am doing a mistake somewhere because the only value that I can get in the square is the first number. Here is my code and my output. Any help and pointers would be greatly appreciated.Thanks

    #include<stdio.h>

    #define SIZE 7
    #define RANGE SIZE*SIZE

    void main(void)
    {

    int square[SIZE][SIZE]={0};
    int count=1;
    int col=SIZE/2;
    int row=0;
    square[row][col]=count;

    //build the magic square

    do{
    count++;
    row--;
    col++;

    if(row == -1 && col == SIZE)
    {
    row+=2;
    col--;
    }
    else if (row == -1)
    row=SIZE-1;
    else if(col == SIZE)
    col = 0;
    else if (square[row][col] != 0)
    {
    row+=2;
    col--;
    }
    }while (count < RANGE);

    //print magic square
    for(row=0;row<SIZE;row++){
    for(col=0;col<SIZE;col++)
    printf("%6d",square[row][col]);
    printf("\n");
    }
    }

    output
    0 0 0 1 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Doing

    square[row][col] = count;

    somewhere in your while loop would be a good idea IMO

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    28
    I got your magic box program to work. Here's the working version
    with a couple of comments to show you what I did. I did some reformatting of your source code because it's the only way I could
    debug it. Don't take it personally. If this is one of your first programs you have a good start. Try to cut down on the "else if's".

    Code:
    #include<stdio.h> 
    
    #define SIZE 7 
    #define RANGE SIZE*SIZE 
    
    // changed void main(void) to int main(void)
    int main(void) 
    { 
    
     // extra bracketing required here
     // see K&R second edition page 111
     int square[SIZE][SIZE]={{0}};
    
     int count=1;
     int col=SIZE/2;
     int row=0;
    
    //build the magic square
     do{
      count++;
      row--;
      col++;
    
      if(row == -1 && col == SIZE)
      {
       row+=2;
       col--;
      }
      else if (row == -1)
       row=SIZE-1;
      else if(col == SIZE)
       col = 0;
      else if (square[row][col] != 0)
      {
       row+=2;
       col--;
      }
    
      // this line is moved from above
      square[row][col] = count; 
    
     }while (count < RANGE);
    
     //print magic square
     for(row=0;row<SIZE;row++)
     {
      for(col=0;col<SIZE;col++)
      printf("%6d",square[row][col]);
      printf("\n");
     }
    } // end void main(void)
    Last edited by jerryvtts; 07-16-2002 at 12:22 AM.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    And just to highlight the obvious in the original code:
    >void main(void)
    is wrong... main returns an int.

    Now this next comment is a personal opinion, so ignore it if you want..... Unreg, I try and avoid using do-while loops that have a lot of code in them. I believe it makes the program harder to read, because until I've read the complete loop, I've no idea what's controlling it. If you think your code must have do-while's, I'd suggest putting a comment at the top of the loop to help the reader see what the controlling factors are.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Unregistered
    Guest
    Hi everyone,
    Thanks for the help. I got the code to work. Just as a not, the
    square[row][col] = count; needs to be a the beginning of the do-while.
    I just want to apologize for the lack of comments and the missed up indentation.
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loop seg error
    By Zishaan in forum Game Programming
    Replies: 2
    Last Post: 03-28-2007, 01:27 PM
  2. Windows crashing Magic Square
    By KoshiB in forum C++ Programming
    Replies: 9
    Last Post: 04-19-2006, 09:02 PM
  3. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  4. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM