Thread: magic square program

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    8

    magic square program

    Hey it's me again.

    I'm still working on the magic square program, and the program is not working how I think it should be working. (I know I'm in error though )

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <time.h>
    #include <stdbool.h>
    #include "random.h"
    int main()
    {
     bool test;
     int x, y, z, w, p, l, randomer;
     test= false;
     int square[3][3];
     for(x=0;x<3;x++)
       for(y=0;y<3;y++)
         square[x][y]=0;
     time_t seconds;
     time(&seconds);
     srand((unsigned int) seconds);
     for(z=1;z<10;z++)
     {
      while(test==false)
      {
      w=random();
      switch(w)
      {
       case 1: if(square[0][0]!=0){ square[0][0]=w; test=true;} break;
       case 2: if(square[1][0]!=0){ square[1][0]=w; test=true;} break;   
       case 3: if(square[2][0]!=0){ square[2][0]=w; test=true;} break;
       case 4: if(square[0][1]!=0){ square[0][1]=w; test=true;} break;   
       case 5: if(square[1][1]!=0){ square[1][1]=w; test=true;} break;   
       case 6: if(square[2][1]!=0){ square[2][1]=w; test=true;} break;
       case 7: if(square[0][2]!=0){ square[0][2]=w; test=true;} break;
       case 8: if(square[1][2]!=0){ square[1][2]=w; test=true;} break;
       case 9: if(square[2][2]!=0){ square[2][2]=w; test=true;} break;            
      }
      }
        test=false; 
     }
     
     for(l=0;l<3;l++)
     {
       for(p=0;p<3;p++)
          printf("%d",square[l][p]);
       printf("\n");
     }
     getchar();
    }
    I believe it is stuck in an infinite loop somewhere... Because no display ever comes up and "Enter" does not end it as getchar(); should do..

    also
    Code:
    int random()
    {
     return (int)(rand()%9 + 1);
    }
    is the random header. Just my experiment with headers. I know that works right. Unless you must use srand() in the place you use rand()

    Thanks,
    Zarakava.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You probably want ==0, not !=0.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    8
    crap... I missed that... Thanks....

    EDIT: Works now... wow what a stupid error. Thanks though.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    8
    More problems. Thanks for your patience

    Now this probably has to do with my randomizing.

    Code:
    int main()
    {
     bool test;
     int x, y, z, w, p, l, randomer;
     test= false;
     int square[3][3];
     for(x=0;x<3;x++)
       for(y=0;y<3;y++)
         square[x][y]=0;
     time_t seconds;
     time(&seconds);
     srand((unsigned int) seconds);
     for(z=1;z<10;z++)
     {
      while(test==false)
      {
      w=random();
      switch(w)
      {
       case 1: if(square[0][0]==0){ square[0][0]=w; test=true;} break;
       case 2: if(square[1][0]==0){ square[1][0]=w; test=true;} break;   
       case 3: if(square[2][0]==0){ square[2][0]=w; test=true;} break;
       case 4: if(square[0][1]==0){ square[0][1]=w; test=true;} break;   
       case 5: if(square[1][1]==0){ square[1][1]=w; test=true;} break;   
       case 6: if(square[2][1]==0){ square[2][1]=w; test=true;} break;
       case 7: if(square[0][2]==0){ square[0][2]=w; test=true;} break;
       case 8: if(square[1][2]==0){ square[1][2]=w; test=true;} break;
       case 9: if(square[2][2]==0){ square[2][2]=w; test=true;} break;      
      }
      }
        test=false; 
     }
     
     getchar();
    }

    Always prints out

    147
    258
    369


    While magic square... I shouldn't get that every time...

    EDIT: There is code in there for printing, but I was reorganizing and forgot to copy it into the post.
    Last edited by Zarakava; 01-27-2009 at 06:42 PM.

  5. #5
    Banned
    Join Date
    Jan 2009
    Posts
    30
    Its not even a magic square anyway... is this the whole program?

    [edit]Ooops I see your random() function.[/edit]
    Last edited by sphynxter; 01-27-2009 at 06:49 PM.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    8
    doh. Yeah its not a magic square. But yes thats the whole program. I get that output every time.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    Code:
      w=random();
      switch(w)
      {
       case 1: if(square[0][0]==0){ square[0][0]=w; test=true;} break;
       case 2: if(square[1][0]==0){ square[1][0]=w; test=true;} break;   
       case 3: if(square[2][0]==0){ square[2][0]=w; test=true;} break;
       case 4: if(square[0][1]==0){ square[0][1]=w; test=true;} break;   
       case 5: if(square[1][1]==0){ square[1][1]=w; test=true;} break;   
       case 6: if(square[2][1]==0){ square[2][1]=w; test=true;} break;
       case 7: if(square[0][2]==0){ square[0][2]=w; test=true;} break;
       case 8: if(square[1][2]==0){ square[1][2]=w; test=true;} break;
       case 9: if(square[2][2]==0){ square[2][2]=w; test=true;} break;      
      }
    You don't see any problems with this code at all? Really?? Read it out loud and try to translate the code into English.

    For example: If 'w' equals 1 then if square[0][0] is zero then ....

  8. #8
    Banned
    Join Date
    Jan 2009
    Posts
    30
    Here is how I would do it....

    Code:
    int random(void)
    {
      static short used = 0;
      int r;
    
      while(used & (1 << (r = (rand() % 9))))
        ; // do nothing.
    
      used |= 1 << r;
      return r+1;
    }
    Or I suppose if that is too nasty looking and hard to follow...

    Code:
    int random(void)
    {
      static short used = 0;
      int r;
    
      do
      {
         r = rand() % 9;
      } while(used & (1 << r));
    
      used |= 1 << r;
      return r+1;
    }

  9. #9
    Banned
    Join Date
    Jan 2009
    Posts
    30
    Code:
    for(x = 0;x<3;++x)
      for(y = 0; y<3; ++y)
        square[x][y] = random();
    To fix your initial problem--assuming you use my random.

    I also just noticed a boo-boo with my while loop....

    Code:
    int random(void)
    {
      static int used = 0;
      int r;
    
      if(used == 15)
        return -1;
    
      do
      {
        r = rand() % 9;
      } while(used & (1 << r));
    
      used |= 1 << r;
    
      return r + 1;
    }
    Last edited by sphynxter; 01-27-2009 at 07:08 PM.

  10. #10
    Registered User
    Join Date
    Jan 2009
    Posts
    8
    Quote Originally Posted by arpsmack View Post
    Code:
      w=random();
      switch(w)
      {
       case 1: if(square[0][0]==0){ square[0][0]=w; test=true;} break;
       case 2: if(square[1][0]==0){ square[1][0]=w; test=true;} break;   
       case 3: if(square[2][0]==0){ square[2][0]=w; test=true;} break;
       case 4: if(square[0][1]==0){ square[0][1]=w; test=true;} break;   
       case 5: if(square[1][1]==0){ square[1][1]=w; test=true;} break;   
       case 6: if(square[2][1]==0){ square[2][1]=w; test=true;} break;
       case 7: if(square[0][2]==0){ square[0][2]=w; test=true;} break;
       case 8: if(square[1][2]==0){ square[1][2]=w; test=true;} break;
       case 9: if(square[2][2]==0){ square[2][2]=w; test=true;} break;      
      }
    You don't see any problems with this code at all? Really?? Read it out loud and try to translate the code into English.

    For example: If 'w' equals 1 then if square[0][0] is zero then ....

    Oh dear gosh...

    square[2][2]=w;

    should be

    square[2][2]=z;

    EDIT: sorry for being so stupid.

    Works fine now.

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    Quote Originally Posted by Zarakava View Post
    Oh dear gosh...

    square[2][2]=w;

    should be

    square[2][2]=z;

    EDIT: sorry for being so stupid.

    Works fine now.
    You should see some of the crap I write.

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. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  4. Help with magic square program
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 07-15-2002, 05:57 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM