Rand.Int NOt working
Author |
Message |
nick4563
|
Posted: 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>
Turing: |
var number : array 1..100 of int number := Rand.Int(0,100)
%It says that Rand.Int is the wrong type
|
Please specify what version of Turing you are using
4.1.1 |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Tony
![](http://wiki.compsci.ca/images/f/f4/OniTony.gif)
|
Posted: 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?) |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
![](images/spacer.gif) |
nick4563
|
Posted: 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 |
|
|
|
|
![](images/spacer.gif) |
chaos
|
Posted: 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 |
|
|
|
|
![](images/spacer.gif) |
Tony
![](http://wiki.compsci.ca/images/f/f4/OniTony.gif)
|
Posted: 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. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
![](images/spacer.gif) |
TokenHerbz
![](http://compsci.ca/v3/uploads/user_avatars/717170395558e628d9452b.jpg)
|
Posted: Wed Jan 19, 2011 8:41 pm Post subject: RE:Rand.Int NOt working |
|
|
Turing: |
var number : array 1 .. 100 of int var sum: int := 0
loop cls for i : 1 .. 100 number (i ) := Rand.Int (0, 100) put number (i ), " ".. sum + = number (i ) end for
put "" put "TOTAL SUM: ",sum, " //// AVERAGE RANDOM NUMBER: ", sum div 100 put "" put "PRESS ANY KEY TO RESTART!" Input.Pause sum := 0 end loop
|
|
|
|
|
|
![](images/spacer.gif) |
Coldkick
|
Posted: 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
Turing: |
var size : int
put "Please enter the size of the array you want to calculate the total and average for: "..
get size
cls
var number : array 1 . . size of int
var sum : int := 0
for i : 1 .. size
number (i ) := Rand.Int (0, 100)
put number (i ), " "..
sum + = number (i )
end for
put ""
put "Sum: ", sum
put "Average: ", sum / size
|
Not much different from Tokens, also, do you want the average integer, or the average real? Either way, hope this helped. |
|
|
|
|
![](images/spacer.gif) |
Tony
![](http://wiki.compsci.ca/images/f/f4/OniTony.gif)
|
Posted: 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. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
|
|