Thread: Lotto 6/49 program

  1. #16
    Registered User
    Join Date
    Feb 2019
    Posts
    639
    And, if you want a more "random" seed, if RDRAND is not available, you can change SRAND_() like this:
    Code:
    static void SRAND_(void) 
    { 
      int fd;
      unsigned int rnd;
      ssize_t size;
    
      if ( ( fd = open( "/dev/urandom", O_RDONLY ) ) < 0 )
      {
        perror("open");
        goto fallback;
      }
    
      if ( ( size = read( fd, &rnd, sizeof rnd ) ) != sizeof rnd )
      {
        if ( size < 0 )
          perror("read");
        close( fd );
        goto fallback;
      }
    
      close(fd);
    
      srand( rnd );
    
      return;
    
    fallback:
      // In case of failure, use the timestamp anyway...
      srand( time( NULL ) );
    }
    Of course, all this only works on "unix"...
    Last edited by flp1969; 2 Weeks Ago at 07:42 AM.

  2. #17
    Registered User
    Join Date
    Dec 2019
    Posts
    16
    Thanks you for all your comments but when I typed select 6 numbers I meant that you use the scanf function to input 6 numbers and to check if these are the winning or losing numbers.

  3. #18
    Registered User
    Join Date
    Dec 2019
    Posts
    16
    In all your examples you used arrays,functions,outputs but they seem too complicated. Don't you have steps that are a lot easier than what you showed me?

  4. #19
    Registered User
    Join Date
    Dec 2019
    Posts
    16
    Anyone?

  5. #20
    Registered User
    Join Date
    Dec 2019
    Posts
    16
    New advices....?

  6. #21
    Registered User
    Join Date
    Dec 2017
    Posts
    836
    pass
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

  7. #22
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,680
    Well if you've never written any code, then all the exuberance shown by the regulars probably isn't what you want.

    Here's an exercise for you.
    - create an array of 49 elements, and use a loop to fill that array with the numbers 1 to 49.
    Then print the array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #23
    Registered User
    Join Date
    Dec 2019
    Posts
    16
    Quote Originally Posted by Salem View Post
    Well if you've never written any code, then all the exuberance shown by the regulars probably isn't what you want.

    Here's an exercise for you.
    - create an array of 49 elements, and use a loop to fill that array with the numbers 1 to 49.
    Then print the array.
    It's my first time in programming and this is a partial code,it's not done:
    int numbers[49];
    for(i=1;i<=49;i++)
    numbers[i]=i;

    I don't know how to continue

  9. #24
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,680
    Right, first issue is that arrays start at subscript 0.

    So either make your array 50 elements long, or your loop
    for ( i = 0 ; i < 49 ; i++ )


    Then I asked you to print the array.
    Which would be the same kind of loop, and a printf() statement.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #25
    Registered User
    Join Date
    Dec 2019
    Posts
    16
    Quote Originally Posted by Salem View Post
    Right, first issue is that arrays start at subscript 0.

    So either make your array 50 elements long, or your loop
    for ( i = 0 ; i < 49 ; i++ )
    But you specified that I should be doing 49 elements and not 50. Am I not correct?

  11. #26
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,680
    Code:
    int numbers[49];
    for ( int = 0 ; i < 49 ; i++ ) numbers[i] = i+1;
    vs this, which wastes element 0 of the array.
    Code:
    int numbers[50];
    for ( int = 1 ; i <= 49 ; i++ ) numbers[i] = i;
    Which, if you replaced <=49 with <50, would be identical to
    Code:
    int numbers[50];
    for ( int = 1 ; i < 50 ; i++ ) numbers[i] = i;
    The idiomatic for loop used < N as the end condition, where N is the dimension of the array subscript being looped over.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #27
    Registered User
    Join Date
    Dec 2019
    Posts
    16
    Code:
    #include<stdio.h>
    int main() {
    int a[6],v[6];
    srand(time(0));
    int ok=1;
    for(int j=1;j<=6;j++){
    int n= 1+(rand()%49);
    int v[j] = n;
    }
    for(int i=1;i<6;i++)
    for(int j= i+1;j<=6;j++)
    if(v[i]==v[j])
    v[j]=(1+(rand()%49)+2)/2;
    for(int j=1;j<=6;j++)
    scanf("a[%d]",&a[j]);
    for(int i=1;i<6;i++)
           for(int j=i+1; j<=6; j++)
            if(a[i]==a[j])ok=2;
      for(int i=1; i<=6; i++)
          {if(a[i]>49||a[i]<1||ok==2)
    printf("Inappropriate list for Lottery!");
    ok=0;
    break;
    }
     if(ok==1){
           for(int i=1; i<=6; i++)
           {if(i==1)
           printf("Inappropriate list for Lottery!");
           ok=0;
           break;}}
           if(ok==1){
           for(int i=1; i<=6; i++)
           {if(i==1)
           printf("*Your numbers are: %d \n");
           if(a[i]<=49&&a[i]>=1)
           printf("a[i]");
           printf("Winning numbers this time : %d \n");
           for(int i=1;i<=6;i++)
           printf("v[i]");
           int nr=0;
           for(int i=1;i<=6;i++)
           for(int j=1; j<=6; j++)
               if(a[i]==v[j])nr++;
               int jk=0;if(nr==6)jk=1;
       if(jk==0){
           printf("You hit %d numbers");
           printf("You have (nr*100)/6 chances to win");
    }
          else{
          printf("YOU GOT THE JACKPOT");
          }
    return 0;
    }
    I tried to convert the c++ code in the c code and I want to know if what I wrote here is correct or there are some errors which I have to change. The code is indeed complicated but since this is the only thing I found...

  13. #28
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,509
    Quote Originally Posted by wolly
    I tried to convert the c++ code in the c code and I want to know if what I wrote here is correct or there are some errors which I have to change. The code is indeed complicated but since this is the only thing I found...
    The code desperately needs to be formatted properly and use descriptive variable names where appropriate, and possibly broken up into smaller functions that do one thing and do it well.

    But more importantly, the point is to understand and learn, not to blindly convert code from one programming language to another: if that is all you want to do, then forget it as I shall replace you with a program because you're nothing more than a compiler, and even less than a compiler because you most certainly cannot perform routine optimisations. "Blindly convert code from one programming language to another" isn't even a transferable skill that might come in handy outside of software: you're better off learning a second natural language + translation skills: at least you'll be able to find a job as a translator, even in a world where machine translation of human languages is approaching human standards.

    My suggestion is this: come up with an algorithm and describe it to us (e.g., in pseudocode, or even in prose). You can then get feedback as to whether the algorithm is feasible or needs fixing. After that, implement your algorithm, and test it,then you can post here again as for feedback on your code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #29
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,680
    > The code is indeed complicated but since this is the only thing I found...
    So it looks like wolly isn't interested in learning after all.
    Just another drive-by looking for easy google answers.

    If you're serious, start doing the mini exercise I posted a couple of days ago.
    It will lead to the answer eventually, and it might actually TEACH you a useful skill or two along the way.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #30
    Registered User
    Join Date
    Dec 2019
    Posts
    16
    Thank you for the help,@Salem. I'm a bit confused about what you said with arrays
    For example if I would create an array like this and I refuse to use the for loop would that be a problem?
    Code:
     int lotto[49] = 
          {
             1,  2,  3,  4,  5,  6,  7,
             8,  9, 10, 11, 12, 13, 14,
            15, 16, 17, 18, 19, 20, 21,
            22, 23, 24, 25, 26, 27, 28,
            29, 30, 31, 32, 33, 34, 35,
            36, 37, 38, 39, 40, 41, 42,
            43, 44, 45, 46, 47, 48, 49
          }
    When I try to increase the array's element lotto[i] = i+1 I don't get 48 or 49 numbers situated in a column and what I get is the number 42 with no reason at all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. lotto program homework help
    By apelletier in forum C Programming
    Replies: 14
    Last Post: 03-11-2018, 02:09 PM
  2. Lotto program
    By MrQ in forum C Programming
    Replies: 3
    Last Post: 06-24-2013, 12:35 AM
  3. lotto program in c
    By vyshant in forum C Programming
    Replies: 7
    Last Post: 11-07-2011, 12:19 PM
  4. Lotto problem
    By Roaring_Tiger in forum C Programming
    Replies: 11
    Last Post: 03-13-2003, 10:17 PM
  5. Lotto game in C
    By fun2sas in forum C Programming
    Replies: 2
    Last Post: 03-02-2003, 07:19 PM

Tags for this Thread