简体   繁体   中英

How to? - Lottery Simulation (C)

i have written a C program that will simulate lottery draws of the course of X amount of years that were inputted by the user, once the the amount of years have been inputted it will simulate a lottery draw each week for however many years. the program also need to print out if the pre inputted numbers ( already in the code) match, and also to print out how many times the numbers matched eg

  • 6 numbers matched ()
  • 5 numbers matched ()
  • etc.

This is the code that i have so far, everything compiled and runs fine:

#include <stdio.h>

int main(int argc, char const *argv[])
{
    //Welcome the User to the Program
    puts("============================");
    puts("       WELCOME TO       ");
    puts("============================");
    puts("  PROJECT : JACKPOT DREAMS  ");
    puts("============================");

    //Rogers 6 Original Numbers
    int nums[6] = { 5, 11, 15, 33, 42, 43 };

    //Ask how many years to simulate
    int years = 0;
    printf("How many years would you like to sleep for? :\n");
    scanf("%d", &years);
    printf("Ok. I will now play the lottery %d year(s)\n",years);
    printf("Sleep Tight :)....\n");

    //Generate Random Numbers
    int ctr;
    int randnums[6];
    srand(time(NULL));
    while (years-- > 0) {
        for( ctr = 0; ctr < 6; ctr++ ) randnums[ctr] = (rand() % 50);

        //Check Numbers with Rogerns numbers
        int win = 1;
        for( ctr = 0; ctr < 6; ctr++ )
        {
            if(randnums[ctr] != nums[ctr])
            {
                win = 0;
                break; // if there's a mismatch we don't need to continue
            }
        }


        return 0;
    }

}

does anyone know how i would do this?

Firstly it seems that you return after looping through the first year. You should move the return statement outside the braces. Secondly as some comments mentioned, you should write blocks more carefully, and make correct indents.

Below I have rewritten your program, to print out if some numbers match for a given year. If all numbers match, "Winner!" is also printed. To do this I have added a few variables and print statements.

Hope this helps.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int
main(int argc, char const *argv[])
{
    //Welcome the User to the Program
    puts("============================");
    puts("         WELCOME TO         ");
    puts("============================");
    puts("  PROJECT : JACKPOT DREAMS  ");
    puts("============================");

    //Rogers 6 Original Numbers
    int nums[6] = { 5, 11, 15, 33, 42, 43 };

    //Ask how many years to simulate
    int years = 0;
    printf("How many years would you like to sleep for? :\n");
    scanf("%d", &years);
    printf("Ok. I will now play the lottery %d year(s)\n",years);
    printf("Sleep Tight :)....\n");

    //Generate Random Numbers
    int numberOfWins = 0;
    int ctr;
    int randnums[6];
    srand(time(NULL));

    int currYear = 0;
    while (years-- > 0) 
    {
        currYear++;
        for( ctr = 0; ctr < 6; ctr++ ) randnums[ctr] = (rand() % 50);

        //Check Numbers with Rogerns numbers
        int win = 1, matched = 0;
        for( ctr = 0; ctr < 6; ctr++ )
        {
            if(randnums[ctr] != nums[ctr])
            {
                win = 0;
            } else {
                matched++;
            }
        }
        numberOfWins += win;

        //If any numbers matched or win, print it.
        if (matched > 0) printf("In year: %d, %d number(s) matched\n", currYear, matched);
        if (win) printf("Winner!\n");
    }

    printf("You won %d time(s)\n", numberOfWins);
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM