% vs. / beginner question

This is a discussion on % vs. / beginner question within the C++ Programming forums, part of the General Programming Boards category; kinda new here...hello all! Anyway i am using the book c++ without fear to learn c++ and so far it ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    1

    % vs. / beginner question

    kinda new here...hello all!
    Anyway i am using the book c++ without fear to learn c++ and so far it is working well but, i came across something in one of the exercises. Basically, the point of the code is to pick a random item (ball or a cube) of a random color (red, blue, green or orange). The book is trying to teach me how to set up a code so it only calls the random number generator once to select one of the 8 possibilties ( a cube of 1 of the 4 colors or a ball of 1 of the 4 colors).

    Now here is the part of the code i am intrested in...the book has:

    Code:
    // Draw-an-item function
    // Performs one item-pick by getting a random 0-4 and a random
    //  0-1. These are then used to index the string arrays, colors
    //  and shapes
    //
    void draw_an_item() {
        int c;         // Random index (0 thru 4) into colors array
        int s;         // Random index (0 thru 1) into shapes array
        int item;
    
        item = rand_0toN1(8);    // Get random number from 0 to 7
        c = item % 4;            // c = random 0 to 3
        s = item / 4;            // s = random 0 to 1
        cout << colors[c] << " " << shapes[s] << endl;
    }
    Here is what I have ( i have different variables...sorry about that)

    Code:
    void draw_an_item()
    {
         int i;
         int c;
         int shape;
         
         shape = rand_0toN1(8);
         
         i = shape % 2;
         c = shape % 4;
         cout << item[i] << " of " << color[c] << endl;
    }
    is there any difference from the books using:
    Code:
    s = item / 4;            // s = random 0 to 1
    vs my using
    Code:
      i = shape % 2;
    They both produce an answer that is between 0 and , so i dont think it matters, but i am sure you all know better.

    Thanks!
    ~Rokraja

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Well using shape % 2; means you only end up with red balls or blue cubes (for example).
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User nenpa8lo's Avatar
    Join Date
    Jan 2008
    Posts
    42
    In case of
    Code:
    item/4
    you get result 0 for item 0-3 and result 1 for item 4-7. In case of
    Code:
    item%2
    you get result 0 for item 0,2,4,6 and result 1 for item 1,3,5,7. Therefore final output is different in both cases.

    I would also suggest to do some reading on modulo.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner: Linked List question
    By WeatherMan in forum C++ Programming
    Replies: 2
    Last Post: 04-03-2008, 07:16 AM
  2. Quick IF statement question (beginner)
    By jim.rattlehead in forum C Programming
    Replies: 23
    Last Post: 11-29-2007, 05:51 AM
  3. beginner question
    By Barrot in forum C++ Programming
    Replies: 4
    Last Post: 08-19-2005, 02:17 PM
  4. Question About External Files (Beginner)
    By jamez05 in forum C Programming
    Replies: 0
    Last Post: 08-11-2005, 07:05 AM
  5. Beginner on Win32 apps, lame question.
    By Templario in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 07:39 PM

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