fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define DIGITS 4
  6.  
  7. int question[DIGITS]; /* store the 4 digits of the question */
  8. void generateQuestion();
  9. void guess(int your_answer[]);
  10. int compare(int your_answer[]);
  11.  
  12. int main()
  13. {
  14. int your_answer[DIGITS]; /* your_answer is for user input. */
  15. int is_correct; /* is_correct is to check whether the user entered the correct number. */
  16. generateQuestion();
  17. printf("Enter 4 different digits with the first digit nonzero.\n");
  18.  
  19. do{
  20. guess(your_answer);
  21. is_correct = compare(your_answer);
  22. }while(is_correct == 0);
  23.  
  24. printf("congratulation! \nYou guessed correct number!\n");
  25.  
  26. system("pause");
  27. return 0;
  28. }
  29.  
  30. /* randomly generate 4 digits without repeation. */
  31. void generateQuestion()
  32. {
  33. int is_repeat; /* for check whether we generate the repeated digit. */
  34. int i, j;
  35. srand( time(NULL) );
  36.  
  37. question[0] = rand() % 9 + 1; /* the first digit should be 1-9 */
  38.  
  39. /* generate the other 3 digits. */
  40. for(i = 1; i < DIGITS; i++){
  41. /* generate the new digit until no repeated value occurred. */
  42. do{
  43. is_repeat = 0; /* suppose we do not generate repeated digit. */
  44. question[i] = rand() % 10; /* the other digit can be 0-9. */
  45. for(j = 0; j < i; j++){
  46. if(question[i] == question[j]){
  47. is_repeat = 1; /* repeation occurred. */
  48. break;
  49. }
  50. }
  51. }while(is_repeat == 1);
  52. }
  53.  
  54. /* display the generated four digits to make debug convenient, so
  55. you can remove the printf statement.
  56. */
  57. printf("%d%d%d%d\n", question[0], question[1], question[2], question[3]);
  58. }
  59.  
  60. /* get the number the user guessed and store it into your_answer */
  61. void guess(int your_answer[])
  62. {
  63. int value, i;
  64. scanf("%d", &value); /* view the four digits as a number. */
  65.  
  66. /* store the four digits. */
  67. for(i = DIGITS-1; i >=0; i--){
  68. your_answer[i] = value % 10;
  69. value = value / 10;
  70. }
  71. }
  72.  
  73. /* compare the question with the user input number
  74.   if the user guessed the correct number, return 1.
  75.   otherwise, return 0.
  76. */
  77.  
  78. int compare(int your_answer[])
  79. {
  80. int a_count = 0, b_count = 0, i, j;
  81.  
  82. for(i = 0; i < DIGITS; i++){
  83. if(your_answer[i] == question[i]){
  84. a_count++;
  85. }
  86. else{
  87. for(j = 0; j < DIGITS; j++){
  88. if(your_answer[i] == question[j] && j != i){
  89. b_count++;
  90. break;
  91. }
  92. }
  93. }
  94. }
  95.  
  96. printf("%d A %d B\n", a_count, b_count);
  97.  
  98. if(a_count == 4)
  99. return 1;
  100. else{
  101. printf("Try again.\n");
  102. return 0;
  103. }
  104. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty