Tuesday, July 14, 2009

1.Write a C++ program that defines an array of 30 integers. Your program should put 30 random values (between

Im really spent on this question. Someone please help.

1.Write a C++ program that defines an array of 30 integers. Your program should put 30 random values (between
declare an array of 30 integers.


example:


int integers[30];





to get the random values;


use the rand() function in the stdlib header file;


however it will give you the same values everytime, if you want different values seed it first





to seed the rand(), use the srand(seed);





you decide the seed value or it can be defined by the system by calling time to return the number of seconds the computer has been turned on.





then just loop it with a "for" loop





srand(seed)


for (int i= 0; i %26lt; 30; i++) {


integers[i] = rand(); //convert this first to a valid integer


}





rand() gives you random numbers not necessarily within the range of an integer type.
Reply:What have you done so far?


No comments:

Post a Comment