-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 */
}
|