Web Design and Development Forums

Logic Problem involving loops and arrays

This is a discussion on "Logic Problem involving loops and arrays" within the Perl, Python, Ruby and Others section. This forum, and the thread "Logic Problem involving loops and arrays are both part of the Program Your Website category.


Go Back   Webforumz.com > Program Your Website > Perl, Python, Ruby and Others

Welcome to Webforumz.com.
Register Now Register now!

Reply
 
LinkBack Thread Tools Rate Thread
Old Oct 27th, 2007, 04:56   #1 (permalink)
Reputable Member
 
Join Date: Jan 2004
Location: California
Age: 19
Posts: 211
Send a message via AIM to tox0tes
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;
}
tox0tes is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Oct 27th, 2007, 11:10   #2 (permalink)
 
c010depunkk's Avatar
 
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 612
Blog Entries: 2
Send a message via MSN to c010depunkk
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!
__________________
Web design is the creation of digital environments that facilitate and encourage human activity; reflect or adapt to individual voices and content; and change gracefully over time while always retaining their identity.

~ www.c010depunkk.com ~ the hang-out of a web developer

c010depunkk is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Oct 27th, 2007, 18:39   #3 (permalink)
Reputable Member
 
Join Date: Jan 2004
Location: California
Age: 19
Posts: 211
Send a message via AIM to tox0tes
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.
tox0tes is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Oct 27th, 2007, 18:48   #4 (permalink)
Reputable Member
 
Join Date: Jan 2004
Location: California
Age: 19
Posts: 211
Send a message via AIM to tox0tes
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.
tox0tes is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Oct 27th, 2007, 19:42   #5 (permalink)
Administrator
 
alexgeek's Avatar
 
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 4,102
Blog Entries: 9
Send a message via MSN to alexgeek
Re: Logic Problem involving loops and arrays

Wow mind-boggling.
Is this solved now?
__________________
Languages: PHP, mySQL (queries), C#, (X)html, CSS, JS.


alexgeek is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Oct 28th, 2007, 03:11   #6 (permalink)
Reputable Member
 
Join Date: Jan 2004
Location: California
Age: 19
Posts: 211
Send a message via AIM to tox0tes
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.
tox0tes is offline  
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
Rate This Thread
Rate This Thread:

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 MySQL 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



Latest Updates

All Points SEO Security Advisory - CHECK YOUR SITE NOW!

Creative Coding :: February 2008

Webforumz is sponsored by: WESH UK Web Hosting
All times are GMT. The time now is 23:01.

Sleep Study Scoring :: Free Bet :: Website Templates :: Online Betting :: Bookmakers :: Funny Quotes :: Internet Recruitment Software :: Microsoft CRM Experts :: Online Casino :: Decorated Christmas Trees :: Midwife Forums :: Football Betting :: Ecommerce Software :: Web Hosting :: Football Stats :: Dry Cleaning Collection :: xtreme wales - extreme clothing :: Apuestas :: Sharepoint Consultants :: Website Optimisation :: Office Clearance London :: Sharepoint Experts :: Sports Betting :: Casino :: Website Templates :: Web Design Development India :: Online Gambling

Powered by: vBulletin Version 3.7, Copyright ©2000 - 2008, Jelsoft Enterprises Limited.
© 2003-2008 Webforumz.com : All Rights Reserved
Search Engine Friendly URLs by vBSEO 3.2.0 RC6


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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59