Advertisement
Guest User

Untitled

a guest
Jan 28th, 2014
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.51 KB | None | 0 0
  1. #include <stdbool.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6.  
  7. #define TICKET_SIZE 6
  8. #define TICKET_COST 2
  9. #define UPPER_LIMIT 49
  10.  
  11. void gen_unique_arr(int *src) {
  12.     size_t elems = TICKET_SIZE;
  13.  
  14.     while (elems) {
  15.         int temp = rand()%UPPER_LIMIT+1;
  16.         int i = 0;
  17.  
  18.         for (; i < TICKET_SIZE; ++i) {
  19.             if (temp == src[i]) {
  20.                 break;
  21.             }
  22.         }
  23.  
  24.         if (i == TICKET_SIZE) {
  25.             src[TICKET_SIZE-elems] = temp;
  26.             --elems;
  27.         }
  28.     }
  29. }
  30.  
  31. bool match_unique_arr(int *first, int *second) {
  32.     bool found = false;
  33.  
  34.     for (size_t i = 0; i < TICKET_SIZE; ++i) {
  35.         found = false;
  36.         for (size_t j = 0; j < TICKET_SIZE; ++j) {
  37.             if (first[i] == second[j]) {
  38.                 found = true;
  39.                 break;
  40.             }
  41.         }
  42.  
  43.         if (found == false) {
  44.             break;
  45.         }
  46.     }
  47.  
  48.     return found;
  49. }
  50.  
  51. int main(int argc, char** argv) {
  52.     srand(time(0));
  53.  
  54.     int ticket[TICKET_SIZE] = {0};
  55.     int winner[TICKET_SIZE] = {0};
  56.  
  57.     uint64_t dollars_spent = 0;
  58.     uint64_t tickets_bought = 0;
  59.  
  60.     gen_unique_arr(ticket);
  61.  
  62.     while (match_unique_arr(ticket, winner) == false) {
  63.         gen_unique_arr(winner);
  64.         dollars_spent += TICKET_COST;
  65.         ++tickets_bought;
  66.     }
  67.  
  68.     printf("\nWon the lottery!\n");
  69.     printf("You spent $%u ", dollars_spent);
  70.     printf("on %u tickets.\n", tickets_bought);
  71.  
  72.     return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement