Monday, May 24, 2010

How 2 write a programmetha generate array of 101 random numbers in c++?

I won't do the work for you, since this smells like homework, but I can give you pseudocode:





define int array random_number of 101 integers


define int i // loop counter





seed random number generator





for i = 0 to 100


__ random_number[i] = generated random number


next i





It's up to you to get the C++ syntax and functions to generate the random numbers.

How 2 write a programmetha generate array of 101 random numbers in c++?
You posted in the wrong section, it should be in Computers %26gt; Programming %26amp; Design (anyway, that shouldn't be much a problem since this is simple and some mathematician are programmers too)





Ok, in C++, you can generate random numbers by calling rand(), but first you've got to #include %26lt;cstdlib%26gt;. Since computers can only generate pseudorandom numbers, most of the time it's good to seed the random number by calling srand(); generally feeding it the current time as current time is always unique.





You can read around here for a bit more about random numbers:


http://www.daniweb.com/forums/thread1769...





You can declare array by using the square bracket:





int anarray[101];





this will create an int array that have 101 members and is named anarray.





Now, one bit you should know, C++'s array is zero-based so the lowest indices start with 0 (anarray[0]) and the highest indices is length of array - 1, that means anarray[100]





Next, you'll need a loop. In C++, there are several kinds of loop, for this one, for-loop is the best choice, I won't talk about the rest for now. The syntax for a for-loop is:





for (initialsituation ; conditiontoloop; operation) {


...codes...


}





You'll need something like this:


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





This i variable would be used as the index for your random number array, you'll generate the random number one-by-one, and put it in the next array location.





anarray[i] = rand();





That covers the basic building block. You should be able to write your own code with this and llafer's pseudocode. I hope you could appreciate programming, as it's a fun thing to do once you learned the basic.
Reply:In BASIC:





Dim A(101)


For I = 1 to 101


A(I) = RAND


Next I


No comments:

Post a Comment