#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    1
    Rep Power
    0

    A bit confused...


    Hello,

    I am currently a novice in C programming with some knowledge in Java. I have been recently reading C for Dummies by Dan Gookin (Pretty good so far) and I came across a program that I just can't seem to grasp. Well, more like a fragment of the code. Here it is below.

    Code:
    /* see if a number has already been drawn*/
    for(c=0;c<BALLS;c++)
    {
         do
         {
                ball = random() % RANGE;
          }while[ball];
    
         /*Number drawn*/
    
         numbers[ball] = 1;
         
         printf("%2d ", ball+1);
    }
    Basically this fragment is supposed to indicate which number has already been chosen in a lotto sort of game. The part that confuses me is in the do-while loop and then the proceeding line. I see it as it is replacing the elements value with 1 rather than keeping the value in the element and making all the elements the same number. He goes over it in the book but I can't seem to grasp it. Can any of you enlighten me? :D
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,974
    Rep Power
    510
    I've avoided the Dummies books. Maybe you should too.

    while[ball]

    is syntactically wrong.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    248
    It probably should read like this:

    Code:
    /* see if a number has already been drawn*/
    for(c=0;c<BALLS;c++)
    {
         do
         {
                ball = random() % RANGE;
          }while (numbers[ball]);
    
         /*Number drawn*/
    
         numbers[ball] = 1;
         
         printf("%2d ", ball+1);
    }
    When you see such a clear typo like this, the rest of the book become suspect. all the code in any educational book should be taken from compiled, tested code. Also, all the code should be available for download, so check their site and see if that abomination is there as well. If so, get a new book.

    Comments on this post

    • Schol-R-LEA agrees : Wasn't it [i]Elements of Programming Style[/i] where Kernighan and Plauger used code from other people's textbooks to illustrate poor programming techniques?

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    55
    Rep Power
    7
    Perhaps post the full project and we can assist in a better solution.

IMN logo majestic logo threadwatch logo seochat tools logo