Thread: Fill the array help

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    13

    Fill the array help

    Could someone please explain to me what this for loop does in reference to the fill array with random numbers? Thanks.

    Code:
    void fillArray ( int grades [] )
    {
      
        int i;
        
        
        
        
        for ( i= 0; i < 100;i++)
        {
            grades[i] = rand() % 100 + 1;
        }
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use the FAQ, Luke! Cprogramming.com FAQ > Generate random numbers? (Also known as RTFM.)


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    13
    What does grades[i] = rand() % 100 + 1 mean in this code?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Do that thing where you actually click on the link I provided. Then do the part where you read what the link says. If you still don't understand, go read up on arrays, and while you are there, go back and read the part about math operators.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by IMMORTALX
    What does grades[i] = rand() % 100 + 1 mean in this code?
    It reduces the pseudorandom numbers generated into the range [1, 101), though it probably introduces a slight bias (that might not matter to you here) in the process.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by laserlight View Post
    It reduces the pseudorandom numbers generated into the range [1, 101), though it probably introduces a slight bias (that might not matter to you here) in the process.
    Which he would know if he had read the link I posted and saw:
    Code:
      /*
       * Formula:  
       *    rand() % N   <- To get a number between 0 - N-1
       *    Then add the result to min, giving you 
       *    a random number between min - max.
       */  
      rc = (rand() % (max - min + 1) + min);
      
      return (rc);
    }

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    13
    thanks

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by IMMORTALX View Post
    thanks
    Next time you want to know what something that butt simple does... use a printf() statement in your code to put it on the screen...
    Code:
    void fillArray ( int grades [] )
    {
      
        int i;
        
        
        
        
        for ( i= 0; i < 100;i++)
        {
            grades[i] = rand() % 100 + 1;
    
    printf("%d  ",grades[i]);
    
    
        }
    }
    Really... 99 out of 100 questions of this kind can be answered by simple experimentation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fill an array with numbers from 1 to 10
    By bos1234 in forum C Programming
    Replies: 2
    Last Post: 01-26-2011, 12:38 PM
  2. Replies: 2
    Last Post: 04-27-2008, 03:39 AM
  3. fill array with number
    By nevrax in forum C Programming
    Replies: 9
    Last Post: 03-28-2007, 04:53 PM
  4. random fill array
    By zeromx in forum C Programming
    Replies: 7
    Last Post: 11-07-2006, 06:18 AM
  5. Trying to fill an int array
    By boojus in forum C++ Programming
    Replies: 3
    Last Post: 11-21-2003, 02:31 PM