fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define CARDS 52
  6. #define DRAW 5
  7.  
  8. void showcard(int card);
  9.  
  10. int main() {
  11. int deck[CARDS];
  12. int c;
  13.  
  14. /* initialize the deck */
  15. for (int x = 0; x < CARDS; x++) deck[x] = 0;
  16. srand((unsigned)time(NULL));
  17. for (int x = 0; x < DRAW; x++) {
  18. for(;;) { /* loop until a valid card is drawn */
  19. c = rand() % CARDS; /* generate random drawn */
  20. if(deck[c] == 0) { /* has card been drawn? */
  21. deck[c] = 1; /* show that card is drawn */
  22. showcard(c); /* display card */
  23. break; /* end the loop */
  24. }
  25. } /* repeat loop until valid card found */
  26. }
  27. }
  28.  
  29. void showcard(int card) {
  30. char *suit[4] = { "Spades", "Hearts", "Clubs", "Diamonds" };
  31. switch (card % 13) {
  32. case 0:
  33. printf("%2s","A");
  34. break;
  35. case 10:
  36. printf("%2s","J");
  37. break;
  38. case 11:
  39. printf("%2s","Q");
  40. break;
  41. case 12:
  42. printf("%2s","K");
  43. break;
  44. default:
  45. printf("%2d",card%13+1);
  46. }
  47. printf(" of %s\n",suit[card/13]);
  48. }
  49.  
  50. //https://pt.stackoverflow.com/q/41829/101
Success #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
 3 of Diamonds
 3 of Clubs
 A of Hearts
 5 of Clubs
10 of Hearts