Thread: Lottery - C Programm

  1. #1
    Registered User
    Join Date
    Nov 2014
    Posts
    9

    Lightbulb Lottery - C Programm

    Hey folks, i've got a little problem with arrays. I progged this today but it won't work for me.. the debuger says it's an endless loop about the first "for" and "comparison" is the greatest number of integer right at the beginning.
    May be you got an idea? Numbers shouldn't be doubled.
    Thanks for helping..
    and sorry for my bad english

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    #define number_lottery 6
    
    int main(int argc, char** argv) {
    
        srand(time(NULL));
        int comparison = 0;
        int position = 0;
        int counter = 0;
        int check = 0;
    
        int lottery_output[number_lottery];
    
        for (position = 0; position <= number_lottery; position++) {
            do {
                int random = rand() % 49 + 1;
    
                for (counter = 0; counter <= position; counter++) {
                    comparison = lottery_output[counter];
                    if (random == comparison) {
                        check = 0;
                    } else {
                        check = 1;
                        lottery_output[position] = random;
                    }
                }
            } while (check == 0);
        }
        printf("%i ", lottery_output[0, 1, 2, 3, 4, 5]);
        printf("\n");
    
        return (EXIT_SUCCESS);
    }
    Last edited by Gooody; 11-19-2014 at 06:16 PM.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Your compiler should be giving you warnings:

    Code:
    /*
    main.c||In function 'main':|
    main.c|33|warning: left-hand operand of comma expression has no effect|
    main.c|33|warning: left-hand operand of comma expression has no effect|
    main.c|33|warning: left-hand operand of comma expression has no effect|
    main.c|33|warning: left-hand operand of comma expression has no effect|
    main.c|33|warning: left-hand operand of comma expression has no effect|
    ||=== Build finished: 0 errors, 5 warnings ===|
    */
    That is from this line:

    Code:
    printf("%i ", lottozahlen[0, 1, 2, 3, 4, 5]);
    I'm not sure what you're trying to accomplish there, but I can tell you that it isn't what you think.

    If you're just trying to print out all values in the array, you'd need to use a loop.

    Code:
    for(stelle = 0; stelle <= anzahl_lottozahlen; stelle++)
         for(zaehler=0; zaehler <= stelle; zaehler++)
    Arrays of size 'N' run from zero to 'N'-1. You're likely exceeding the bounds of your arrays since you're using <= in your check. The more proper idiom would be to just use <.

    I am having a hard time following the loop logic due to the unfamiliar variable names. I suspect the value of "check" mostly/always comes out to be zero at the end of the inner "for()" loop, and only then is the condition checked at the "while()".

    I'd recommend carefully reconsidering your logic, perhaps with the help of a flow chart, to help make code that is more concise and easier to follow.

  3. #3
    Registered User
    Join Date
    Nov 2014
    Posts
    9
    Thanks for answering my question.
    Now I've got this:
    It works partly but i will only recieve one lottery number six times.
    An array mistake must be there.
    I just wanted to keep you up to date but if there any suggestions i would take them lovely.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    #define number_lottery 6
    
    int main(int argc, char** argv) {
    
        srand(time(NULL));
        int comparison = 0;
        int position = 0;
        int counter = 0;
        int check = 0;
        int i;
    
        int lottery_output[number_lottery];
    
        for (position = 0; position < number_lottery; position++) {
            do {
                int random = rand() % 49 + 1;
    
                for (counter = 0; counter <= position; counter++) {
                    comparison = lottery_output[counter];
                    if (random == comparison) {
                        check = 0;
                    } else {
                        check = 1;
                        lottery_output[position] = random;
                    }
                }
            } while (check == 0);
        }
        
        /*  Output  */
        printf("The lottery numbers are: ");
     
        for(i=0; i < 6; i++)
        {
           printf("%i ", lottery_output[i]); 
        }
        printf("\n");
    
        return (EXIT_SUCCESS);
    }
    Last edited by Gooody; 11-19-2014 at 08:04 PM.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    88
    at line 26 you need to get out of the for loop or the next iteration of for loop will set check =1;

  5. #5
    Registered User
    Join Date
    Nov 2014
    Posts
    9
    Here we are:
    Thanks for help


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    #define number_lottery 6
    
    int main(int argc, char** argv) {
        
        /*  random   */
        srand(time(NULL));
        int position = 0;
        int lottery_output[number_lottery];
        
        /*  algorithmus */
    
        for (position = 0; position < number_lottery; position++) {
            int check = 0;
            do {
                int random = rand() % 49 + 1;
                int comparison = 0;
                int counter = 0;
                
                for (counter = 0; counter <= position; counter++) {
                    comparison = lottery_output[counter];
                    if (random != comparison) {
                        lottery_output[position] = random;
                        check = 1;
                    }
                }
            } while (check == 0);
        }
        
        /*  Output  */
        printf("The lottery numbers are: ");
        int counter;
        for(counter=0; counter < 6; counter++)
        {
           printf("%i ", lottery_output[counter]); 
        }
        printf("\n");
    
        return (EXIT_SUCCESS);
    }

  6. #6
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    That's great but your logic is still very much flawed.

    Here is what I'm assuming you're trying to do:
    Create a list of random numbers between 1 and 49 (inclusive), with NO repeating numbers

    Currently your code does not do that. You need to check the "check" variable, AFTER your for loop. Your if (random != comparison) needs to be inverted and then you check the check variable AFTER your loop. This will tell you if any of the elements up to the current element, is equal to the current element. If check variable hasn't changed after the inner for() loop, then you have a unique random number, if the check variable HAS changed, then you need to get a new random number (rand reset the check variable, which you don't do in the do while loop, like you need to).

    Here is an example of what I'm talking about:
    Code:
        for (position = 0; position < sizeof lottery_output/sizeof lottery_output[0]; position++) {
            int check; /* declare check variable */
            do {
                int random = rand() % 49 + 1;
                int comparison = 0;
                int counter = 0;
                check = 1; /* set check to success by default */
    
                for (counter = 0; counter < position; counter++) {
                    comparison = lottery_output[counter];
                    /* We found a variable that matches our current random number */
                    if (random == comparison) { /* Inverted condition (was !=) */
                        check = 0; /* We failed to find a unique value, try again */
                        break; /* DONE no need to keep searching */
                    }
                }
    
                /* We got a unique random number, assign it! */
                if (check)
                    lottery_output[position] = random;
    
            } while (check == 0); /* Check is 1 so we exit the loop */
        }
    EDIT: Also now that your check is defaulted to success, your loop condition can now have the PROPER condition of: count < position insttead of <= like you had previously which is wrong. The reason it was infinite looping with < was that your check variable was never getting set to 1 to exit the loop
    Last edited by nonpuz; 11-19-2014 at 11:14 PM.

  7. #7
    Registered User
    Join Date
    Nov 2014
    Posts
    9
    You're right. My lottery programm doesn't give me always 6 different numbers. Yours works perfectly and i admit that it is the better way. Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. lottery program
    By rmorrell23 in forum C Programming
    Replies: 1
    Last Post: 10-28-2014, 05:32 PM
  2. Needs help making a lottery
    By DecoratorFawn82 in forum C++ Programming
    Replies: 14
    Last Post: 04-21-2013, 04:54 PM
  3. Lottery game
    By got1sleeve in forum C++ Programming
    Replies: 3
    Last Post: 02-16-2008, 01:55 PM
  4. Lottery game
    By geetard in forum C++ Programming
    Replies: 2
    Last Post: 12-20-2005, 12:50 AM
  5. Yet another lottery program
    By BungleSpice in forum C Programming
    Replies: 10
    Last Post: 04-01-2004, 02:08 PM

Tags for this Thread