Computer Science Canada Rand.Int NOt working |
Author: | nick4563 [ Wed Jan 19, 2011 6:58 pm ] | ||
Post subject: | Rand.Int NOt working | ||
What is it you are trying to achieve? I want to write a code finding the average of an unspecified amount of random numbers and then display the random numbers after. What is the problem you are having? Assigned value is the wrong Type Describe what you have tried to solve this problem Researched topic Post any relevant code (You may choose to attach the file instead of posting the code if it is too long) <Answer Here>
Please specify what version of Turing you are using 4.1.1 |
Author: | Tony [ Wed Jan 19, 2011 7:07 pm ] |
Post subject: | Re: Rand.Int NOt working |
nick4563 @ Wed Jan 19, 2011 6:58 pm wrote: Assigned value is the wrong Type What Type is returned by Rand.Int? What Type is "number"? (Are those the same Type?) |
Author: | nick4563 [ Wed Jan 19, 2011 7:48 pm ] |
Post subject: | RE:Rand.Int NOt working |
I believe they are both integers as array of "int" and rand ."int"... I may be mistaken var number : array 1..100 of int Rand.Int(number,0,100) That is my new piece but it doesnt seem to work with arrays |
Author: | chaos [ Wed Jan 19, 2011 7:59 pm ] |
Post subject: | Re: Rand.Int NOt working |
Your problem is that you are assigning a random number to an entire array, which doesn't make sense in my opinion. Instead, you need to declare the array. Then, cycle throught the indices of the array and assign those indices a random number. It should look like this: var sum:int:=0 var average:int var number : array 1..100 of int for i:1..100 %This for loop cycled through the entire array and assigns each of the 100 elements a rnadom number from 1 to 100 number(i):=Rand.Int(1,100) sum:=sum+number(i) %This finds the total of all the random numbers. put number(i) %This is to output the random number end for average:=sum/100,5,2 %This outputs the average rounded to two decimal places put average |
Author: | Tony [ Wed Jan 19, 2011 8:09 pm ] |
Post subject: | Re: RE:Rand.Int NOt working |
nick4563 @ Wed Jan 19, 2011 7:48 pm wrote: var number : array 1..100 of int Here the type is the entire "array 1..100 of int", which is interpreted as: an array of size 1..100 where each item is of type int. |
Author: | TokenHerbz [ Wed Jan 19, 2011 8:41 pm ] | ||
Post subject: | RE:Rand.Int NOt working | ||
|
Author: | Coldkick [ Wed Jan 19, 2011 10:46 pm ] | ||
Post subject: | RE:Rand.Int NOt working | ||
Since you want an unspecified amount of numbers you should have the user input the amount of random numbers too. Or it could be randomly generated, your choice. Something like this
Not much different from Tokens, also, do you want the average integer, or the average real? Either way, hope this helped. |
Author: | Tony [ Wed Jan 19, 2011 11:13 pm ] |
Post subject: | RE:Rand.Int NOt working |
And since we never refer to any of the generated numbers outside of a any loop iteration, we don't actually need an array to store any of them. |