Thread: Suduko Grid For Solving From Already Generated Grid Function

  1. #1
    Registered User
    Join Date
    Nov 2014
    Posts
    11

    Suduko Grid For Solving From Already Generated Grid Function

    -3 down vote favorite I am having issues with the function below and am wondering how to fix it so it works
    for my assignment. All of my code so far for the assigment has been posted.

    void genSudokuBoard(int grid[ ], int display[ ]) This function is to return 4 elements of each square in my Sudoku grid like the example below in order to display the grid. An example with the first square numbers is below as 4 random numbers in that square are 02,10,11,19. I have already coded the rest of the program and add probably over thinking how to do this. Also the first argument is an already printed grid of Sudoku that is generated for me.
    entire 3x3 block: 0, 1, 2, 9, 10, 11, 18, 19, 20
    4 random indexes might be: 2, 10, 11, 19
    Code:
    /*a3Primer.c
       date:    november 3, 2014
       author:  danny abesdris
       purpose: primer for ipc144 (fall 2014) assignment #3
                including solution to the
                void genSudokuNumbers(int grid[ ]) function.
    */
    
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    
    #define N 9
    /* NOTE: You will have to code the 2 functions BELOW "checkDups" and "clearArray"
             before running this program to see the generated output.
    */
    int checkDups(int [ ], int, int);
    void clearArray(int [ ], int);
    
    int genSudokuNumbers(int grid[ ]); /* already coded */
    
    void genSudokuBoard(int grid[ ], int display[ ]);
    void displaySudokuBoard(int display[ ], int bigOh);
    void getRandNum(int low, int high, int *pn);
    void printSudokuBoardToFile(int display[ ], const char *fileName);
    
    int main( ) {
       int grid[N*N],display[N*N];
       int loops;
       int p;
    
        /* students must add code to return the correct
        value for all instructions executed in all loops */
       loops = genSudokuNumbers(grid); /* students must add code to return the correct*/
       displaySudokuBoard(grid,loops);
       genSudokuBoard(grid,display);
       displaySudokuBoard(display,loops);
       return 0;
    }
    void genSudokuBoard(int grid[],int display[]) {
        clearArray(display,81);
        int x,y,c=0;
        int random;
        for(x=1;x<=N;x+=9){
           for(c=0;c<=4;c++){
               getRandNum(1,9,&random);
           display[x]=grid[random];
                }
            }
    }
    void displaySudokuBoard(int display[],int bigOh){
       printf("PLAY IPC144 SUDOKU\n");
       printf("+-----+-----+-----+\n");
       int i,loops,lines=0;
       int colunms =0;
       for(i=0; i<N*N; i++) {  
          if(i%N==0 && i!=0) {
         printf("|");
             printf("\n");
         lines++;
         if (lines % 3 == 0)
            printf("+-----+-----+-----+\n");
          }
          if (colunms % 3 == 0 )
          printf("|%d",display[i]);
    
          else
          printf(" %d", display[i]);
          colunms ++;
       }
          printf("|");
          printf("\n"); 
          printf("+-----+-----+-----+\n");
          printf("Total Instructions:%d\n",bigOh);
    
    
    }
    /* given an array of 'n' elements, this function determines if the
       value 'num' is already present within the array 'nums' and if
       so, sends back a true value (1) or false (0) otherwise */
    
    int checkDups(int nums[ ], int search, int n) {
        int start = 0;
        for(start=0;start<n;start++){
            if(nums[start] == search)
                return 1;
        }
        return 0;
    }
    
    /* Sets all 'n' values in the 'nums' array to 0 */
    void clearArray(int nums[ ], int n) {
            int start =0;
        for(start =0; start <n;start++)
            nums[start]=0;
    }
    
    void getRandNum(int low,int high,int *pn){
        *pn = (rand() % (high+1-low))+low;
    }
    int genSudokuNumbers(int grid[ ]) {
       int c, i, j, rowNum, colNum, blockNum;
       int test[N], dup, temp, valid, cnt, iterations=0;
    
       srand(time(NULL));         /* seeding the random number generator */
       for(i=0; i<N*N; i++) {     /* initializing the grid array to all 0's */
          grid[i] = 0;
          iterations ++;
       }
    
       for(c=0; c<N*N; c++) {     /* main loop to generate N*N numbers for the grid */
          iterations ++;
          temp = rand( ) % N + 1; /* generate random # from 1 to N inclusive */
    
          valid = dup = cnt = 0;
          iterations += 7;
          while(!valid) {         /* keep looping as long as the 'valid' flag is false */
         iterations ++;
             clearArray(test, N);
             iterations +=9;
    
                                  /* the calculations below determine the row, col,
                                     and block numbers (index 0 to 8) based on the
                                     linear index variable 'c' */
             rowNum = c / N;
             colNum = c % N;
             blockNum = (rowNum / 3) * 3 + (colNum / 3);
         iterations +=8;
    
                                  /* now check to see if the number 'temp' is a
                                     duplicate in the row, column, and block to which
                                     'c' corresponds */
    
             for(j=0; j<colNum; j++) {    /* fill row (but only up to colNum) */
            iterations ++;
                test[j] = grid[rowNum*N+j];
            iterations +=4; 
    
             }
             dup += checkDups(test, temp, colNum);
         iterations += 9;
    
             if(!dup) {                   /* row is valid, now check column */
                clearArray(test, N);
            iterations +=9;
                for(j=0; j<rowNum; j++) { /* fill column (but only up to rowNum) */
               iterations ++;
                   test[j] = grid[j*N + colNum];
               iterations +=4;
                }
                dup += checkDups(test, temp, rowNum);
                iterations +=9;
    
                if(!dup) {                /* column is valid now check block */
                   clearArray(test, N);
    
                   for(j=0; j<N; j++) {
              iterations ++;
                      test[j] = grid[((blockNum/3)*N*3) + (colNum/3)*3 + (j/3)*N + j%3];
                      iterations +=14;
                   }
                                          /* equation used to generate array
                                             coordinates for all N blocks
                                             (i.e.)
                                             0, 1, 2,   9, 10, 11,  18, 19, 20  {blk 0}
                                             3, 4, 5,  12, 13, 14,  21, 22, 23  {blk 1}
                                             6, 7, 8,  15, 16, 17,  24, 25, 26  {blk 2}
    
                                             27, 28, 29, 36, 37, 38, 45, 46, 47 {blk 3}
                                             30, 31, 32, 39, 40, 41, 48, 49, 50
                                             33, 34, 35, 42, 43, 44, 51, 52, 53
    
                                             54, 55, 56, 63, 64, 65, 72, 73, 74
                                             57, 58, 59, 66, 67, 68, 75, 76, 77
                                             60, 61, 62, 69, 70, 71, 78, 79, 80 {blk 8}
                                          */
                   dup+= checkDups(test, temp, N);
               iterations +=9;
                }
             }
             if(!dup) { /* no duplicates in row, column, or block, so number
                           can be inserted into the grid */
                grid[c] = temp;
                valid = 1;
                cnt = 0;
            iterations +=3;
             }
             else {     /* duplicate number found, so reset flags and generate
                           new random number */
                temp = rand( ) % N + 1;
                dup = 0;
                cnt++;
            iterations +=6;
             }
             if(cnt > N*N) {
                        /* if after N*N attempts, no possible value is found
                           then reset the entire array and start over
                           (brute force algorithm)
                           average runtime: 50000 iterations */
                valid = dup = cnt = 0;
                clearArray(grid, N*N);
                temp = rand( ) % N + 1;
                c = -1; /* will be reset to 0 in for loop */
            iterations +=86;
             }
          }
       }
       return iterations; /* this value must be updated by within this function BEFORE it is returned */
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Ugh...cross-poster
    arrays - Sudoku Function for C Program Assignment - Stack Overflow
    https://www.marshut.net/krxqrs/help-...ssignment.html
    Nick, cross-posting is considered poor forum etiquette, as it results in lots of duplicated/wasted effort on our part. I suggest you read this article (don't take the title personally).
    How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2014
    Posts
    11
    I would really like to see a working function as I am stressing out about this seriously. In addition sorry about the bad etiquette.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Do you realize how many millions of programmers have solved the sudoku problem before you? Just google it. There are solutions already out there. Stop demanding help from people, when you're too lazy to help yourself.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I have no idea what you are asking help to do.

    I believe the problem is that you have no idea what the function is supposed to do; or, you are very bad in asking for help.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    Nov 2014
    Posts
    11
    I will reword it here, void genSudokuBoard(int grid[ ], int display[ ]) is the function I am to create
    It takes the first agrument as a generated Sudoku Board and it turn takes 4 random elements for each
    3 by 3 square in the grid array and passes then into the display array in order to get a solvable Sudoku
    board.
    Last edited by Nick Krause; 11-13-2014 at 02:04 PM.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I will reword it here, void genSudokuBoard(int grid[ ], int display[ ]) is the function I am to create
    You still haven't really asked a question. Are we supposed to guess as to what is wrong with the function you've written?

    Jim

  8. #8
    Registered User
    Join Date
    Nov 2014
    Posts
    11
    No sorry, I need help writing the function. The version above doesn't work and I am trying to figure out why. If people are unclear about the function I am posting a link to the assignment.
    https://scs.senecac.on.ca/~danny.abesdris/
    It's this function,
    void genSudokuBoard(int grid[ ], int display[ ]) ) (second one on the list)
    Last edited by Nick Krause; 11-13-2014 at 02:41 PM.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    The verison above doesn't work and I am trying to figure out why.
    Again being cryptic. Are we supposed to guess why the function doesn't work? What exactly are you trying to do with that function? What exactly is/isn't it doing? Do you get some kind of error message, if so what is it? Does your program crash and burn? If so have you run the program with your debugger?



    Jim

  10. #10
    Registered User
    Join Date
    Nov 2014
    Posts
    11
    Sorry , I am not getting correct output for the grid. I need 4 random numbers per square
    from the array passed in the grid array argument.
    This is what I am getting for the second grid
    +-----+-----+-----+
    |0 0 0|0 0 0|0 0 0|
    |0 0 0|0 0 0|0 0 0|
    |0 0 0|0 0 0|0 0 0|
    +-----+-----+-----+
    |0 0 0|0 0 0|0 0 0|
    |0 0 0|0 0 0|0 0 0|
    |0 0 0|0 0 0|0 0 0|
    +-----+-----+-----+
    |0 0 0|0 0 0|0 0 0|
    |0 0 0|0 0 0|0 0 0|
    |0 0 0|0 0 0|0 0 0|
    +-----+-----+-----+
    I need to be getting something like this
    PLAY IPC144 SUDOKU
    +-----+-----+-----+
    | 2|4 5|8 1|
    |5 1| 9 | 4|
    | 6 |1 |2 |
    +-----+-----+-----+
    | 1 | 7 | 2 3|
    |6 7| 2 | 8|
    |8 |5 9| 6|
    +-----+-----+-----+
    | 5 |7 2|6 3 |
    |2 4 | | 9|
    | 6|9 8 |4 |
    +-----+-----+-----+

    Using elements for the grid array.
    Code:
    void genSudokuBoard(int grid[],int display[]) {
    clearArray(display,81);
    }
    If it helps this is what my teacher wants :
    void genSudokuBoard(int grid[ ], int display[ ])
    This function uses the 81 element 'grid' array (containing a fully
    completed list of valid Sudoku values) and randomly copies 4 numbers
    from each 3x3 block into the corresponding positions in the 81
    element 'display' array. The random numbers chosen will be by array
    position (index), so for example, given that the top left 3x3 block consists
    of the array indexes:
    entire 3x3 block: 0, 1, 2, 9, 10, 11, 18, 19, 20
    4 random indexes might be: 2, 10, 11, 19

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Start by generating 4 unique random numbers between, say, 1-9 or better yet, 0-8.

  12. #12
    Registered User
    Join Date
    Nov 2014
    Posts
    11
    Anduril,
    Thanks for the reply but I am really lost here and it would make my life much easier if I can see a working function for this using my already existing code.

  13. #13
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Well you can't. We can't give you a working function for at least two reasons. One, it's against our homework policy, and two, it would prevent you from really learning much.

    Start even simpler then. Can you write a program that generates 4 random numbers, regardless of repeats? This at least should be within your grasp.

  14. #14
    Registered User
    Join Date
    Nov 2014
    Posts
    11
    Code:
    void genSudokuBoard(int grid[],int display[]) {
    clearArray(display,81);
    int x =0;
    int y;
    for(x=0;x<4;x++){
            getRandNum(0,8,&y);
            printf("%d\n",y);
         }
    }
    This is the function so far.

  15. #15
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Okay, good. Now, how might you solve this if you had to do it with paper and pencil? Remember, if you can't solve a problem, you can't program a computer to do it. Working things out with paper and pencil is usually very, very, very helpful. You could actually do this yourself, either using the getRandNum function, or by having a friend give you random numbers. Pay attention to each little step, each piece of data you need to complete this. That will form the basis of your algorithm and variables you need. Some questions to think about when you do this: How do you store what numbers you already have? How do you check if you already have that number? How would you know when to stop the process?

    I'll give you one hint: you need to keep track of several object of the same type, so an array might be a good choice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generic Function to show grid.
    By sergioms in forum C Programming
    Replies: 2
    Last Post: 07-20-2011, 12:41 PM
  2. help with the grid
    By wannabec++ in forum C++ Programming
    Replies: 2
    Last Post: 12-05-2010, 02:43 PM
  3. 2d grid
    By lord in forum C++ Programming
    Replies: 4
    Last Post: 02-03-2009, 08:06 PM
  4. grid
    By xlnk in forum Windows Programming
    Replies: 3
    Last Post: 12-14-2002, 08:40 PM
  5. hex grid
    By waterst in forum C Programming
    Replies: 2
    Last Post: 10-30-2002, 03:36 PM