magic square program

• 01-25-2009
Zarakava
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.
• 01-25-2009
tabstop
You probably want ==0, not !=0.
• 01-25-2009
Zarakava
crap... I missed that... Thanks....

EDIT: Works now... wow what a stupid error. Thanks though.
• 01-27-2009
Zarakava
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.
• 01-27-2009
sphynxter
Its not even a magic square anyway... is this the whole program?

Ooops I see your random() function.[/edit]
• 01-27-2009
Zarakava
doh. Yeah its not a magic square. But yes thats the whole program. I get that output every time.
• 01-27-2009
arpsmack
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 ....
• 01-27-2009
sphynxter
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;
}

• 01-27-2009
sphynxter
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;
}

• 01-27-2009
Zarakava
Quote:

Originally Posted by arpsmack
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.
• 01-27-2009
arpsmack
Quote:

Originally Posted by Zarakava
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.