Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Checking my array error

Credits go to gnolam, Jonatan Hedborg, and Kris Asick for helping out!
This thread is locked; no one can reply to it. rss feed Print
Checking my array error
beeb105
Member #7,790
September 2006

I just want to make an array that places numbers 1-9 in a grid without any number being on the same line, vertically or horizonally.

Here is one of many different ways I have been trying to do this...

1int array[8][8];
2 
3for(int y = 0; y <= 8; y++)
4 {for(int x = 0; x <= 8; x++)
5 while(array[y][x] == 0)//Loop until array's value changes.
6 {changer = rand()%9 + 1;//Random 1-9 number
7 succeed = 1;
8 for(int x2 = 0; x2 <= 8; x2++)//Checks for that number right and left.
9 {if(array[y][x2] == changer)
10 succeed = 0;}
11 for(int y2 = 0; y2 <= 8; y2++)//Checks for the number up and down.
12 {if(array[y2][x] == changer)
13 succeed = 0;}
14 if(succeed == 1)//If the value of succeed did not change, it changes the array because that number can go there.
15 array[y][x] = changer;}}

Now...I don't get why this doesn't work, when I run it it just stays at the blank screen, I believe because it's still trying to make the grid. I've tried it so the checking part of the code only does the search in an area of [4][4] in the whole grid and it seems to work most of the time. But when the search gets bigger it doesn't work at all or only sometimes if the numbers are small enough. The funny thing is, if I comment out this part of the code from above and only let one of the loops stay it works...

        for(int x2 = 0; x2 <= 8; x2++)
        {if(array[y][x2] == changer)
        succeed = 0;}
       // for(int y2 = 0; y2 <= 8; y2++)
       //   {if(array[y2][x] == changer)
       //   succeed = 0;}

But I need both. Does anyone know why this doesn't work? Is the code getting stuck in a loop because it's impossible to make a grid this way or something?

Alright, thanks.

Arthur Kalliokoski
Second in Command
February 2005
avatar

They all watch too much MSNBC... they get ideas.

gnolam
Member #2,030
March 2002
avatar

Since that code is too spaghettiish to make sense to my tired brain, I'll just point out this: you're overrunning your buffer. Change your "int array[8][8];" to "int array[9][9];". Also consider using the more readable "< 9" notation instead of "<= 8" in your for loops.

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

beeb105
Member #7,790
September 2006

Arthur, It's supposed to be a code for a Sudoku game.

and Gnolam, my array should be fine, an arrray of [8][8] is 9 because you count 0.

Kris Asick
Member #1,424
July 2001

What you're trying to do is very difficult at first glance, but not too hard in practice. The best way to handle it is to make the grid ahead of time and then shuffle the rows and columns. For example, start with a nice, sorted grid like this:

123456789
234567891
345678912
456789123
567891234
678912345
789123456
891234567
912345678

Then, randomly shuffle one whole row with another, then one whole column with another, then another pair of rows, another pair of columns, and do this say 50 or 100 times. For example:

Start     -> Swap Rows 3&8 -> Swap Cols 2&5
123456789     123456789        153426789
234567891     234567891        264537891
345678912     891234567        831294567
456789123     456789123        486759123
567891234     567891234        597861234
678912345     678912345        618972345
789123456     789123456        729183456
891234567     345678912        375648912
912345678     912345678        942315678

Works much better that way! ;)

And your arrays are NOT fine, because [8] does NOT inclue 8! ( [8] = 0..7 )

--- Kris Asick (Gemini)
--- http://www.pixelships.com

--- Kris Asick (Gemini)
--- http://www.pixelships.com

Jonatan Hedborg
Member #4,886
July 2004
avatar

my array should be fine, an arrray of [8][8] is 9 because you count 0.

No.. An array of [8] means it has 8 entries. 0-7.

beeb105
Member #7,790
September 2006

Huh, that's a really good idea Kris. I'll use that idea but would still like to know why this code I have now doesn't work, I'm dumb!.

Wow...I must of forgotten about the array thing or something.

Thanks guys.

Kris Asick
Member #1,424
July 2001

The code you currently have doesn't work partly because of the array dimensions being set wrong, but also because it quickly and easily ends up in situations where no matter what numbers it randomly selects, it cannot place them anywhere, and thus ends up in an infinite loop.

If creating such number puzzles were as easy as randomly assigning the numbers, people wouldn't find it so challenging to fill them in, would they? :P

--- Kris Asick (Gemini)
--- http://www.pixelships.com

--- Kris Asick (Gemini)
--- http://www.pixelships.com

beeb105
Member #7,790
September 2006

Kris, the code I showed wasn't my main code, the other code does have the array set to [9][9]. About the endless loop, I don't get why it's endless...There are 81 number positions and there is 9 possible numbers for each row and each position should have 1 spot that doesn't cross another number.

Kris Asick
Member #1,424
July 2001

Consider the following circumstance:

123456789
234567891
345978.12
456789123
567891234
678.12345
789123456
891234567
912345678

The grid looks perfect as far as you can see, but those two blank spots cannot have any numbers placed in them.

That's what's happening with your code. Your grid is being randomized in such a way that before it reaches the end of its randomization, it more than likely creates an impossible conundrum that cannot be rectified.

What you should try doing is adding drawing code to your existing randomization so that you can see the grid at every step of randomization. Then, when it stalls, you can see for yourself that it's stalling because no more numbers can properly be placed.

--- Kris Asick (Gemini)
--- http://www.pixelships.com

--- Kris Asick (Gemini)
--- http://www.pixelships.com

beeb105
Member #7,790
September 2006

Alright, I believe I get it now, thanks a lot.

Go to: