The Code-Bin
Links
Home
Add your code!
All Listings
About
Latest Entry
Featured Scripts
Author's Website
Latest Entries
FFMPEG Thumbnail Scr...
PHP, 0.8KB
Jul. 29, 10:24pm
John
Z80 Assembler, 190 bytes
Feb. 17, 3:36am
John
Z80 Assembler, 176 bytes
Sep. 13, 2:19am
John
Z80 Assembler, 77 bytes
Sep. 13, 2:18am
John
Z80 Assembler, 209 bytes
Sep. 13, 2:17am
Program
Posted by: docmur | May 18, 2009 @ 7:52pm
C Code
[
Download
]
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <malloc.h> #include <time.h> //Diffulculy Section #define EASY 1 #define MED 2 #define HARD 3 #define VHARD 4 #define IMPOS 5 //Limits #define COL_SIZE 9 #define ROW_SIZE 9 #define TOPRANGE 9 #define LOWRANGE 1 //Test Parameters #undef PRINT #undef TEST #undef LOOPTEST #define RANDCHECK #define SQUARE //Returns #define SUCCESS 0 #define FAIL 1 #define TRUE 0 #define FALSE 1 #define true 0 #define false 1 //Typedef typedef int bool; //Prototypes int iPrint(int [][]); int iCheck(int,int [][]); int (*iGenerate(void))[]; int main() { //iPrint(iGenerate); } /* iCheck This function will check the Matrix and make sure that the numbers 1 - 9 appear once in each row and once in each column Accesses: i -> The Random Number (*iMatrix)[] -> The Matrix Calls: NONE Returns: SUCCESS or FAIL Written: Date: May 12th 2009 */ int iCheck(int i,int iMatrix[9][9]) { #ifdef TEST printf("\n[Checking Function]: "); #endif //Sanity Check just for Safety if(i != 0 || i <= 9) { for(int iLoop = 0;iLoop<=8;iLoop++) { // Look in iMatrix at offset [ROW] [COL] // if the number at offset is the same // as the number passed in so i // then we just fail and exit if( iMatrix[iLoop][0] == i ) { iMatrix[iLoop][0] = -1; return FAIL; } } }//End If i != 0 || i <= 9 return SUCCESS; } /* iGenerate This function will Generate the 9x9 Matrix need and fill it with random number 1 - 9. This function Does not check the Matrix just fills it. Accesses: void Returns: iMatrix -> (*iMatrix)[] Calls: iCheck Written: Date: May 12th 2009 */ int (*iGenerate(void))[] { //Make a double array of 81 total allocations //9x9 and make sure to zero of the intire Matrix int iMatrix[8][9]; int iRand = 0; //Seed the randomizer srand(time(NULL)); //Get a index value [iLoop][jLoop] from [0][0] to [9][9] for(int iLoop = 0; iLoop <= 8; iLoop++) { for(int jLoop = 0; jLoop <= 8; jLoop++) { //Initalize the Matrix //While the offset of the pointer to the array is in bound //of [0][0] to [9][9], Place a random value from 1 - 9 into //The matrix as long as the max value randomilized is not //more then 9 which is the max value Rand: while(TOPRANGE < (iRand = LOWRANGE + rand() / (RAND_MAX / TOPRANGE))); //The value did not appear in the Matrix //So because it didn't appear, we can add //Then number into the Matrix if( iCheck(iRand,iMatrix) == SUCCESS) { #ifdef RANDCHECK printf("Number put in Matrix = %d\n",iRand); #endif iMatrix[iLoop][jLoop] = iRand; } //The number didn't appear //We need to take one off the jLoop so we dont expire //The loop to fast else if(iCheck(iRand,iMatrix) == FAIL) { #ifdef RANDCHECK printf("Wont Randize Number Here\n"); #endif //If we get here we know that we found a random number //So we need to take 1 from the index coordinates } #ifdef LOOPCHECK //Loop at the loop varible printf("jLoop = %d\n",jLoop); #endif } } return *iMatrix; } /* iPrint This function will print any Matrix passed to it Accesses: (*iMatrix)[] -> The Matrix Returns: 2 on Success Calls: NONE Written: Date: May 12th 2009 */ int iPrint(int iMatrix[9][9]) { printf("\n"); for(int iLoop = 0; iLoop <= (sizeof(iMatrix) * sizeof(short)); iLoop++) { for(int jLoop = 0; jLoop <= (sizeof(iMatrix) * sizeof(short)); jLoop++) printf("%d ",iMatrix[iLoop][jLoop]); #ifdef SQUARE printf("\n"); #endif } return 2; }
Syntax Highlighting
[
Open in new window
]
Author Comments
none
Rating
4.54 / 8
146 Votes
http://codebin.yi.org/284
page generated in 0.00 seconds