Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdbool.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #define TICKET_SIZE 6
- #define TICKET_COST 2
- #define UPPER_LIMIT 49
- void gen_unique_arr(int *src) {
- size_t elems = TICKET_SIZE;
- while (elems) {
- int temp = rand()%UPPER_LIMIT+1;
- int i = 0;
- for (; i < TICKET_SIZE; ++i) {
- if (temp == src[i]) {
- break;
- }
- }
- if (i == TICKET_SIZE) {
- src[TICKET_SIZE-elems] = temp;
- --elems;
- }
- }
- }
- bool match_unique_arr(int *first, int *second) {
- bool found = false;
- for (size_t i = 0; i < TICKET_SIZE; ++i) {
- found = false;
- for (size_t j = 0; j < TICKET_SIZE; ++j) {
- if (first[i] == second[j]) {
- found = true;
- break;
- }
- }
- if (found == false) {
- break;
- }
- }
- return found;
- }
- int main(int argc, char** argv) {
- srand(time(0));
- int ticket[TICKET_SIZE] = {0};
- int winner[TICKET_SIZE] = {0};
- uint64_t dollars_spent = 0;
- uint64_t tickets_bought = 0;
- gen_unique_arr(ticket);
- while (match_unique_arr(ticket, winner) == false) {
- gen_unique_arr(winner);
- dollars_spent += TICKET_COST;
- ++tickets_bought;
- }
- printf("\nWon the lottery!\n");
- printf("You spent $%u ", dollars_spent);
- printf("on %u tickets.\n", tickets_bought);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement