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