fork download
  1. // make BINGO card
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <time.h>
  7.  
  8. // (int fr) = 1:center free
  9. int *make_bingo_card(int fr) {
  10. int *b;
  11. int i, j, loop;
  12.  
  13. b = (int *)malloc(sizeof(int) * 25);
  14. if (b) {
  15. for (i = 0; i < 25; i++) {
  16. loop = 1;
  17. while (loop) {
  18. b[i] = (rand() % 15) + 1 + ((i / 5) * 15);
  19. if (fr && (i == 12)) {
  20. b[i] = 0;
  21. }
  22. loop = 0;
  23. for (j = 0; j < i; j++) {
  24. if (b[i] == b[j]) {
  25. loop = 1;
  26. }
  27. }
  28. }
  29. }
  30. }
  31. return b;
  32. }
  33.  
  34. int print_bingo_card(int *b) {
  35. int i, j;
  36. char *t1 = "+----+----+----+----+----+\n";
  37. char *t2 = " B I N G O \n";
  38. char str[5][32];
  39.  
  40. if (!b) return 1;
  41. printf("%s", t2);
  42. printf("%s", t1);
  43. for (i = 0; i < 5; i++) {
  44. if (i) {
  45. printf("%s", t1);
  46. }
  47. for (j = 0; j < 5; j++) {
  48. if (b[i + (j * 5)]) {
  49. snprintf(str[j], 31, " %2d ", b[i + (j * 5)]);
  50. } else {
  51. snprintf(str[j], 31, "%s", "FREE");
  52. }
  53. }
  54. printf("|%s|%s|%s|%s|%s|\n", str[0], str[1], str[2], str[3], str[4]);
  55. }
  56. printf("%s", t1);
  57. printf("\n");
  58. return 0;
  59. }
  60.  
  61. int free_bingo_card(int *b) {
  62. if (b) {
  63. free(b);
  64. }
  65. return 0;
  66. }
  67.  
  68. int main(void) {
  69. // your code goes here
  70. int *bingo, i;
  71.  
  72. srand(time(NULL));
  73.  
  74. for (i = 0; i < 5; i++) {
  75. bingo = make_bingo_card(1);
  76. print_bingo_card(bingo);
  77. free_bingo_card(bingo);
  78. }
  79.  
  80. return 0;
  81. }
  82.  
Success #stdin #stdout 0s 4260KB
stdin
Standard input is empty
stdout
   B    I    N    G    O  
+----+----+----+----+----+
|  6 | 28 | 37 | 53 | 67 |
+----+----+----+----+----+
| 11 | 17 | 45 | 48 | 73 |
+----+----+----+----+----+
|  1 | 26 |FREE| 49 | 72 |
+----+----+----+----+----+
| 12 | 22 | 33 | 56 | 63 |
+----+----+----+----+----+
|  9 | 18 | 39 | 47 | 70 |
+----+----+----+----+----+

   B    I    N    G    O  
+----+----+----+----+----+
| 15 | 24 | 45 | 46 | 67 |
+----+----+----+----+----+
|  9 | 27 | 39 | 60 | 63 |
+----+----+----+----+----+
| 11 | 28 |FREE| 58 | 75 |
+----+----+----+----+----+
|  1 | 26 | 31 | 49 | 70 |
+----+----+----+----+----+
|  5 | 25 | 41 | 56 | 65 |
+----+----+----+----+----+

   B    I    N    G    O  
+----+----+----+----+----+
| 13 | 17 | 40 | 49 | 71 |
+----+----+----+----+----+
|  5 | 26 | 34 | 59 | 66 |
+----+----+----+----+----+
|  4 | 25 |FREE| 57 | 70 |
+----+----+----+----+----+
|  6 | 23 | 35 | 46 | 73 |
+----+----+----+----+----+
| 10 | 22 | 44 | 52 | 72 |
+----+----+----+----+----+

   B    I    N    G    O  
+----+----+----+----+----+
| 12 | 25 | 31 | 48 | 70 |
+----+----+----+----+----+
| 13 | 26 | 38 | 58 | 71 |
+----+----+----+----+----+
|  6 | 23 |FREE| 46 | 73 |
+----+----+----+----+----+
|  8 | 21 | 44 | 59 | 72 |
+----+----+----+----+----+
|  2 | 28 | 39 | 52 | 61 |
+----+----+----+----+----+

   B    I    N    G    O  
+----+----+----+----+----+
| 14 | 18 | 31 | 58 | 70 |
+----+----+----+----+----+
| 10 | 30 | 33 | 46 | 63 |
+----+----+----+----+----+
| 12 | 26 |FREE| 53 | 69 |
+----+----+----+----+----+
| 11 | 21 | 45 | 51 | 64 |
+----+----+----+----+----+
|  9 | 29 | 43 | 52 | 73 |
+----+----+----+----+----+