Sunday, July 12, 2009

C++ please help?

write a single C++ statement that will print a number at random from the set:


a) 4, 8, 12, 16.





b) 5, 7, 9, 11.





c) 5, 10, 15, 20.





need help... thanks a lot :D

C++ please help?
#include %26lt;iostream%26gt;


#include %26lt;cstdlib%26gt;


#include %26lt;ctime%26gt;





#define ARR_SZ 5





using namespace std;





int


main(void){


int arr[ARR_SZ]={11,4,8,2};


srand((unsigned)time(NULL));


cout %26lt;%26lt; arr[(rand()%ARR_SZ)] %26lt;%26lt; endl;


return(0);


}





// The shortest I could get!
Reply:I don't know how one would do it in C++, but I know what you would need to do from experience in other languages:





Create a array, with the needed numbers in it. That will assign each number to 1-4, ie 1 is 4, 2 is 8 and so on. Then generate a random number between 1 and 4, and use the generated number to call the entry from the array.


No comments:

Post a Comment