assigning a rand number to each element in an array

This is a discussion on assigning a rand number to each element in an array within the C++ Programming forums, part of the General Programming Boards category; alright, im trying to write a very simple algorithm that creates a fairly random ID for an object in a ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    106

    assigning a rand number to each element in an array

    alright, im trying to write a very simple algorithm that creates a fairly random ID for an object in a binary tree..and heres the basic coding...

    Code:
       while (x<4) {
       	node->bookID[x]=rand()%9+1;
          x++;
       }
    any clue why it would output 0x008533cc? please, any help would be greatly appreciated. thanks a lot!

  2. #2
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    perhaps it's incrementing the memory address of the pointer?
    i don't know how you have your pointers set up and how and why you have to do certina things, but stripped down to bare bones, heres what i'd do:
    Code:
    int array[4];
    for (int i=0; i<4; i++)
    {
         array[i]=rand() %11; //0-10
    }
    Last edited by Waldo2k2; 12-15-2002 at 12:13 PM.
    PHP and XML
    Let's talk about SAX

  3. #3
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953

    Re: assigning a rand number to each element in an array

    I also think it's better to use for loops, sinse it's less confusing.
    Make sure about the data type of 'x', because it should work...
    none...

  4. #4
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    oops, i was looking at his code and mine at the same time...i edited it now
    PHP and XML
    Let's talk about SAX

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. count/display non-repeated element of array
    By azsquall in forum C++ Programming
    Replies: 15
    Last Post: 07-10-2008, 09:42 AM
  2. Replies: 4
    Last Post: 01-05-2008, 10:30 PM
  3. Problem with assigning value to array elements
    By sagitt13 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2004, 11:26 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM
  5. Cannot delete last element in array
    By Simon in forum C Programming
    Replies: 10
    Last Post: 09-12-2002, 08:29 PM

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