1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <malloc.h>
  5. #include <time.h>
  6.  
  7. //Diffulculy Section
  8. #define EASY 1
  9. #define MED 2
  10. #define HARD 3
  11. #define VHARD 4
  12. #define IMPOS 5
  13.  
  14. //Limits
  15. #define COL_SIZE 9
  16. #define ROW_SIZE 9
  17. #define TOPRANGE 9
  18. #define LOWRANGE 1
  19.  
  20. //Test Parameters
  21. #undef PRINT
  22. #undef TEST
  23. #undef LOOPTEST
  24. #define RANDCHECK
  25. #define SQUARE
  26.  
  27. //Returns
  28. #define SUCCESS 0
  29. #define FAIL 1
  30. #define TRUE 0
  31. #define FALSE 1
  32. #define true 0
  33. #define false 1
  34.  
  35. //Typedef
  36. typedef int bool;
  37.  
  38. //Prototypes
  39. int iPrint(int [][]);
  40. int iCheck(int,int [][]);
  41. int (*iGenerate(void))[];
  42.  
  43.  
  44. int main()
  45. {
  46. //iPrint(iGenerate);
  47. }
  48. /*
  49.   iCheck
  50.  
  51.   This function will check the Matrix and make sure
  52.   that the numbers 1 - 9 appear once in each row and
  53.   once in each column
  54.  
  55.   Accesses:
  56.   i -> The Random Number
  57.   (*iMatrix)[] -> The Matrix
  58.  
  59.   Calls:
  60.   NONE
  61.  
  62.   Returns:
  63.   SUCCESS or FAIL
  64.  
  65.   Written:
  66.   Date: May 12th 2009
  67. */
  68.  
  69. int iCheck(int i,int iMatrix[9][9])
  70. {
  71. #ifdef TEST
  72. printf("\n[Checking Function]: ");
  73. #endif
  74. //Sanity Check just for Safety
  75. if(i != 0 || i <= 9)
  76. {
  77. for(int iLoop = 0;iLoop<=8;iLoop++)
  78. {
  79. // Look in iMatrix at offset [ROW] [COL]
  80. // if the number at offset is the same
  81. // as the number passed in so i
  82. // then we just fail and exit
  83. if( iMatrix[iLoop][0] == i )
  84. {
  85. iMatrix[iLoop][0] = -1;
  86. return FAIL;
  87. }
  88. }
  89. }//End If i != 0 || i <= 9
  90. return SUCCESS;
  91. }
  92.  
  93. /*
  94.   iGenerate
  95.  
  96.   This function will Generate the 9x9 Matrix need and
  97.   fill it with random number 1 - 9. This function
  98.   Does not check the Matrix just fills it.
  99.  
  100.   Accesses:
  101.   void
  102.  
  103.   Returns:
  104.   iMatrix -> (*iMatrix)[]
  105.  
  106.   Calls:
  107.   iCheck
  108.  
  109.   Written:
  110.   Date: May 12th 2009
  111. */
  112.  
  113. int (*iGenerate(void))[]
  114. {
  115. //Make a double array of 81 total allocations
  116. //9x9 and make sure to zero of the intire Matrix
  117. int iMatrix[8][9];
  118. int iRand = 0;
  119.  
  120. //Seed the randomizer
  121. srand(time(NULL));
  122.  
  123. //Get a index value [iLoop][jLoop] from [0][0] to [9][9]
  124. for(int iLoop = 0; iLoop <= 8; iLoop++)
  125. {
  126. for(int jLoop = 0; jLoop <= 8; jLoop++)
  127. {
  128. //Initalize the Matrix
  129. //While the offset of the pointer to the array is in bound
  130. //of [0][0] to [9][9], Place a random value from 1 - 9 into
  131. //The matrix as long as the max value randomilized is not
  132. //more then 9 which is the max value
  133. Rand: while(TOPRANGE < (iRand = LOWRANGE + rand() / (RAND_MAX / TOPRANGE)));
  134. //The value did not appear in the Matrix
  135. //So because it didn't appear, we can add
  136. //Then number into the Matrix
  137. if( iCheck(iRand,iMatrix) == SUCCESS)
  138. {
  139. #ifdef RANDCHECK
  140. printf("Number put in Matrix = %d\n",iRand);
  141. #endif
  142. iMatrix[iLoop][jLoop] = iRand;
  143. }
  144. //The number didn't appear
  145. //We need to take one off the jLoop so we dont expire
  146. //The loop to fast
  147. else if(iCheck(iRand,iMatrix) == FAIL)
  148. {
  149. #ifdef RANDCHECK
  150. printf("Wont Randize Number Here\n");
  151. #endif
  152. //If we get here we know that we found a random number
  153. //So we need to take 1 from the index coordinates
  154. }
  155. #ifdef LOOPCHECK
  156. //Loop at the loop varible
  157. printf("jLoop = %d\n",jLoop);
  158. #endif
  159. }
  160. }
  161. return *iMatrix;
  162. }
  163.  
  164. /*
  165.   iPrint
  166.  
  167.   This function will print any Matrix passed to it
  168.  
  169.   Accesses:
  170.   (*iMatrix)[] -> The Matrix
  171.  
  172.   Returns:
  173.   2 on Success
  174.  
  175.   Calls:
  176.   NONE
  177.  
  178.   Written:
  179.   Date: May 12th 2009
  180. */
  181.  
  182. int iPrint(int iMatrix[9][9])
  183. {
  184. printf("\n");
  185. for(int iLoop = 0; iLoop <= (sizeof(iMatrix) * sizeof(short)); iLoop++)
  186. {
  187. for(int jLoop = 0; jLoop <= (sizeof(iMatrix) * sizeof(short)); jLoop++)
  188. printf("%d ",iMatrix[iLoop][jLoop]);
  189. #ifdef SQUARE
  190. printf("\n");
  191. #endif
  192. }
  193. return 2;
  194. }
  195.