Saturday, May 22, 2010

C++ program..... help needed plz... urgently????

#include%26lt;iostream.h%26gt;


#include%26lt;stdlib.h%26gt;


#include%26lt;conio.h%26gt;


void main()


{


clrscr();


randomize();


int num,rndnum;


cin%26gt;%26gt;num;


rndnum=random(num)+5;


for(int n=1;n%26lt;=rndnum;n++)


cout%26lt;%26lt;n%26lt;%26lt;" ";


cout%26lt;%26lt;"\n";


getch();


}








Can somebody please explain that for the same value of 'num', why does this program give a different output everytime???





Please explain clearly. This is a board question. Also please explain the working of random() and randomize() functions.





They have also given output options. Perhaps you could explain which output(s) is/are correct for num=5::(There can be more than one):::





1) 1 2 3 4


2) 1 2


3) 1 2 3 4 5 6 7 8 9


4) 1 2 3





Please hurry!!!! Board exam day after on 4th!!!!

C++ program..... help needed plz... urgently????
I'm also a board student.


randomize() is used to initiate the random no. generator so that everytime a new random no. gets generated. The function of ranom() is to generate a random no. between 0 and n-1 (for random(n); )


So everytime a random no. is generated which may be different or same as the previous. This is the reason why with the same value of num, we get different outputs.


For num = 5, random(5); will give either of: 0, 1, 2, 3, 4.


Hence, the possible output is 3) 1 2 3 4 5 6 7 8 9
Reply:Randomize randomizes the seed of random(). random() generates a random number between 0 and num-1. If num has any size at all (i.e. %26gt;2 or 3) then you should be getting different answers all the time.





So if num = 5, then you will enumerate all numbers up to x where x is 5,6,7,8,9, or 10 (randomly selected by random) so only one of those answers is correct (I'll leave it to you to infer the one) since you will enumerate *at least* 0-5.

augustifolia

No comments:

Post a Comment