Logic Problem involving loops and arrays

This is a discussion on "Logic Problem involving loops and arrays" within the Other Programming Languages section. This forum, and the thread "Logic Problem involving loops and arrays are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > Other Programming Languages

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Oct 27th, 2007, 04:56
Reputable Member
Join Date: Jan 2004
Location: California
Age: 19
Posts: 232
Thanks: 0
Thanked 2 Times in 2 Posts
Logic Problem involving loops and arrays

Hey everyone,
I am trying to create a sudoku solver in C. I am working on this one function that is supposed to go through each square and compare it to every other square, looking to see if the value in the square matches the value in any other square in its 3x3 grid, row, or column. puzzle[x][y] is the 2dimensional array containing each value that has been submitted. For example, puzzle[4][2] contains the value on column 4 and row 2...

The code i am having a problem with is right here, where I want to test the current square to see whether it conflicts with the value in another square in either its 3x3 grid, row, or column.

Code: Select all
if((x == a && y != b) || (y == b && x != a) || (!((x == a && y != b) || (y == b && x != a)) && ((int)(x / 3) == (int)(a / 3) && (int)(y / 3) == (int)(b / 3))))
The entire code is below.

Code: Select all
int validator (int puzzle[][9])
{
    int x;
    int y;
    int a;
    int b;
    
   for(y=0; y<9; y++)
    {
       for(x=0; x<9; x++)
        {
           if(puzzle[x][y] != '0')
            {
               for(b=0; b<9; b++)
                {
                   for(a=0; a<9; a++)
                    {
                     if((x == a && y != b) || (y == b && x != a) || (!((x == a && y != b) || (y == b && x != a)) && ((int)(x / 3) == (int)(a / 3) && (int)(y / 3) == (int)(b / 3))))
                  {  
                     return 0;
                  }
                      else
                      {
                         return 1;
                      }
                   }
                } 
            }
            else
            {
               return -1;
         }
        }
    }
   return 2;
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Oct 27th, 2007, 11:10
Highly Reputable Member
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Logic Problem involving loops and arrays

I been trying this out a bit but I'm not getting anywhere. That's one huge IF statement you got there Haven't quite figured it out yet....

I'll get back to you when I have something to show!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Oct 27th, 2007, 18:39
Reputable Member
Join Date: Jan 2004
Location: California
Age: 19
Posts: 232
Thanks: 0
Thanked 2 Times in 2 Posts
Re: Logic Problem involving loops and arrays

basically what it does is it loops through each row and column, then loops through each "other" row and column (signified by a and b instead of x and y). For each one, it tests whether the first square (x,y) contains similar values as in the other square (a,b). If it does, then it returns 0, if not, then it returns 1.

I am trying to figure out how to only test squares that are in its current row, in its current column, and in its current 3x3 grid. That is what the if statement is testing for.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Oct 27th, 2007, 18:48
Reputable Member
Join Date: Jan 2004
Location: California
Age: 19
Posts: 232
Thanks: 0
Thanked 2 Times in 2 Posts
Re: Logic Problem involving loops and arrays

i just realized something: I was mistakenly continuing if puzzle[x][y] != '0', when it should actually be if puzzle[x][y] == '0'..... since an input of '0' means that that is an empty square that the program should consider. If it isn't '0', the the program should stop at the current puzzle[x][y] and return -1 since it is already occupied by a number 1-9.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Oct 27th, 2007, 19:42
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: Logic Problem involving loops and arrays

Wow mind-boggling.
Is this solved now?
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Oct 28th, 2007, 03:11
Reputable Member
Join Date: Jan 2004
Location: California
Age: 19
Posts: 232
Thanks: 0
Thanked 2 Times in 2 Posts
Re: Logic Problem involving loops and arrays

no still not solved, but I wasn't expecting anyone to be able to do it or even devote enough time to understand it. It's a really complex problem.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Free Flash music and ambient loops equinoxsounds Flash & Multimedia Forum 3 Oct 12th, 2007 09:57
Limiting While Loops Weird1993 Databases 5 Jul 12th, 2007 04:33
php While logic dcarson PHP Forum 3 Feb 7th, 2007 18:46
Logic help jayaime JavaScript Forum 4 Aug 31st, 2006 14:28
while loops? mikegu JavaScript Forum 1 Oct 26th, 2003 11:08


All times are GMT. The time now is 01:38.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42