Thread: Help with a do...while array

  1. #1
    Registered User
    Join Date
    Feb 2021
    Posts
    2

    Help with a do...while array

    I started working my way through "C All-In-One Desk Reference For Dummies" by Dan Gookin. I've made it to page 240 and got stuck on a do...while statement. The program generates 6 lotto numbers and disregards any duplicates. The snip-it of code I'm stuck on is:


    Code:
    do
    {
        ball = random() % RANGE;
    }
    while(numbers[ball];
    This segment of the code is apparently checking to make sure the random number generated hasn't already been added to the array but I cannot figure out what calculation/check while(numbers[ball]) is performing to spin the loop. There is no operator present. Any help would be appreciated.

    Here is the full code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define RANGE 50
    #define BALLS 6
    
    int main()
    {
        int numbers[RANGE]; // 0 to 49
        int c,ball;
    
        puts("L O T T O   P I C K E R\n");
    
        srandom((unsigned)time(NULL));
    
        /* initialize the array */
    
        for(c=0;c<RANGE;c++) // if c < 50
            numbers[c]=0; 
    
        printf("Press Enter to pick this week's numbers:");
        getchar();
    
    /* draw the numbers */
        puts("Here they come:");
        for(c=0;c<BALLS;c++) 
        {
         
    /* see if a number has already been drawn */
            do
           {
                ball = random() % RANGE;
           }
            while(numbers[ball]);
    
    /* Number drawn */
            numbers[ball] = 1;
            printf(" %i ",ball+1);
        }
    
        printf("\n\nGood luck in the drawing!\n");
        return(0);
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    What do you think this line is doing?

    Code:
    numbers[ball] = 1;
    It is recording that the number that matches the value of ball has been picked. The while checks to verify the number has not yet been picked.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338

    Post

    what calculation/check while(numbers[ball]) is performing
    Code:
    #include <stdio.h>
    
    int value = 0 ;
    
    void printValue() {
      if (value) printf( "TRUE" );
      if (!value) printf( "FALSE" );
      printf( "\n" );
    }
    
    int main( int argc, char *argv[] ) {
      
      value = 0; printValue();
      value = 1; printValue();
      value = 10; printValue();
      value = -10; printValue();
      value = 0; printValue();
      value = 1; printValue();
      
      return 0;
    }
    Code:
    FALSE
    TRUE
    TRUE
    TRUE
    FALSE
    TRUE
    "without goto we would be wtf'd"

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > There is no operator present.
    But in the context of an expression requiring a boolean expression,
    while(numbers[ball])
    is the same as
    while(numbers[ball] != 0)

    Since you then go onto do numbers[ball] = 1, the next time you enter that loop, if you pick the same ball again, the while loop will do it's thing.
    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.

  5. #5
    Registered User
    Join Date
    Feb 2021
    Posts
    2
    Thank you, everyone! It makes sense now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 07-31-2013, 12:15 AM
  2. Replies: 2
    Last Post: 03-20-2012, 08:41 AM
  3. Replies: 9
    Last Post: 08-23-2010, 02:31 PM
  4. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  5. Replies: 6
    Last Post: 11-09-2006, 03:28 AM

Tags for this Thread