do while loop and modulus question - lotto picker

This is a discussion on do while loop and modulus question - lotto picker within the C Programming forums, part of the General Programming Boards category; reference: C ALL IN ONE DESK REFERENCE pg 40. The code below creates random numbers and displays them. It keeps ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    141

    do while loop and modulus question - lotto picker

    reference: C ALL IN ONE DESK REFERENCE pg 40.

    The code below creates random numbers and displays them. It keeps a track of the numbers generated so they are not repeated. Similar to the lotto

    The trouble I'm having is with the do while loop specifically while(numbers[ball]). Does it mean to say while numbers[ball]=1 it should repeat ?

    Also, lets say a random number 20 is created. What does ball=rand()%RANGE be equal to? 0.4?

    Code:
    #include <stdio.h> 
    #include "stdafx.h" 
    #include <stdlib.h> 
    #include <time.h> 
    
    #define RANGE 50 
    #define BALLS 6 
    int main() 
    { 
       int numbers[RANGE]; 
       int c,ball; 
    
       puts("LOTTO PICKER\n"); 
    
       srand((unsigned)time(NULL)); 
    
       //initialize the array 
       for(c=0;c<RANGE;c++) 
          numbers[c]=0; //all 50 elements are initialized to 0 
    
       printf("Press Enter to pick this week's numbers:"); 
       getchar(); 
    
       //drawing numbers 
       puts("Here they come"); 
    
       for (c=0;c<BALLS;c++) 
       { 
          do 
          { 
          ball=rand()%RANGE; 
          }while (numbers[ball]); 
           
       numbers[ball]=1; 
    
       printf("%2d ",ball+1); 
       } 
        
    
    getchar(); 
    getchar(); 
    }
    Last edited by bos1234; 01-25-2011 at 01:56 AM.

  2. #2
    Registered User
    Join Date
    Jun 2010
    Posts
    9
    In C/C++ boolean assignments are basically broken down into this
    Code:
    if( a ) is really saying if ( a != 0)
    and
    Code:
     if ( !a ) is really saying if ( a== 0 )
    The do-while loop's condition is verifying that numbers[ball] is nonzero.

    So the loop will continue until numbers[ball] (a random member in the array) is zero.
    Then it apparently assigns a value of 1 to it. (probably boolean for chosen)
    spits out the index of that random number+1

    so in pseudo
    I have fifty indexes, so long as none of them have been picked before print the value of this random index
    Last edited by Scramble; 01-25-2011 at 02:15 AM.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    19
    Quote Originally Posted by bos1234 View Post
    Does it mean to say while numbers[ball]=1 it should repeat ?
    yeah, so it will terminate only if if number[ball] is zero. i.e. the number did not come up in the draw before.
    Also, lets say a random number 20 is created. What does ball=rand()%RANGE be equal to? 0.4?
    ball is an integer, so you always have to get rid of the fraction..so 20/50 = 0;

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,498
    Quote Originally Posted by alecodd View Post
    ball is an integer, so you always have to get rid of the fraction..so 20/50 = 0;
    Looks like you got a little mixed up. This is the modulus operator, which is just the remainder in an integer division. 17%5 = 2, 20%50 = 20, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Modulus to Bitwise
    By blurrymadness in forum C Programming
    Replies: 1
    Last Post: 12-01-2009, 11:49 AM
  2. How expensive is modulus in a loop?
    By kermit in forum C Programming
    Replies: 1
    Last Post: 02-03-2009, 07:11 PM
  3. using modulus & weighting factors
    By task in forum C Programming
    Replies: 4
    Last Post: 09-11-2002, 05:52 PM
  4. This one is so annoying(Problem with my while loop)
    By datainjector in forum C Programming
    Replies: 5
    Last Post: 06-27-2002, 10:25 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21