Like Tree1Likes
  • 1 Post By quzah

Fill the array help

This is a discussion on Fill the array help within the C Programming forums, part of the General Programming Boards category; Could someone please explain to me what this for loop does in reference to the fill array with random numbers? ...

  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.
    CommonTater likes this.
    Hope is the first step on the road to disappointment.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,415
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 11:38 AM
  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, 05:18 AM
  5. Trying to fill an int array
    By boojus in forum C++ Programming
    Replies: 3
    Last Post: 11-21-2003, 01:31 PM

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