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; } }
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; } }
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.
What does grades[i] = rand() % 100 + 1 mean in this code?
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.
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.Originally Posted by IMMORTALX
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
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...
Really... 99 out of 100 questions of this kind can be answered by simple experimentation.Code:void fillArray ( int grades [] ) { int i; for ( i= 0; i < 100;i++) { grades[i] = rand() % 100 + 1; printf("%d ",grades[i]); } }