This is the mail archive of the libc-help@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Help With School Assignment


Greetings other developers,
	I am having some issues with these rather simple program due to be over thinking it and it would be
great to get some fresh eyes. In addition I am pasting my code below with a link to my professor's page for the
assignment. I just really need help with the genSudokuBoard function the others as you can I have finished already
and work fine.
Cheers Nick
https://scs.senecac.on.ca/~danny.abesdris/
/*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 */
}

  	  	


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]