Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Quick Question On feasibillity (rand command, with use from a multidimentional arrays)
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
GZA




PostPosted: Tue Dec 11, 2007 1:58 pm   Post subject: Quick Question On feasibillity (rand command, with use from a multidimentional arrays)

Hello there,

I'm having an interesting time trying to figure out something for my program, and i was wondering if someone more experienced could help me if this type of thing was possible?

What im asking is using the "Rand.Int" with substripts from a multidimentional array. So the outcome would be a randomly generated integer from my multidimentional array.

I know it sounds alittle confusing (i know its confusing me). however if anyone could shed some light on this, that'd be smashing Smile

- Geoff.
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Tue Dec 11, 2007 2:13 pm   Post subject: RE:Quick Question On feasibillity (rand command, with use from a multidimentional arrays)

It seems pretty trivial -- just randomly pick an index of the array, and take the element from that location.

Consider 2D array:

[[a,b,c],
[d,e,f]]

z := Rand.Int(0,1)
x := Rand.Int(0,2)

then 2d_array[z][x] = one of {a,b,c,d,e,f}
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
GZA




PostPosted: Tue Dec 11, 2007 5:02 pm   Post subject: Re: Quick Question On feasibillity (rand command, with use from a multidimentional arrays)

Alright,

I think i understand somewhat. Could you show me what it would look like in actual code? Not that i like to be spoonfed, my syntax seems to be wrong at the moment.

- Geoff.
Tony




PostPosted: Tue Dec 11, 2007 6:58 pm   Post subject: Re: Quick Question On feasibillity (rand command, with use from a multidimentional arrays)

GZA @ Tue Dec 11, 2007 5:02 pm wrote:
my syntax seems to be wrong at the moment.

We generally prefer to see some of that wrong syntax. It shows that you've tried, and you'll get to see where exactly the problems are.

It would also put all of our examples into a better context Smile
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
GZA




PostPosted: Wed Dec 12, 2007 1:27 pm   Post subject: (No subject)

Quote:
var cards :array 1 .. 10 of string := init ("Ace of hearts","Ace of spades","Ace of clubs","Ace of Dimonds","2 of hearts","2 of spades","2 of clubs","2 of diamonds","3 of hearts","3 of spades")

put Rand.Int (%???)


Well, basicly that is the code im working with (quoted without errors.) What im tring to do is display a card randomly using the Rand.Int command... Im also using this in another program to display a money value from a mulitdimentional array, and i am comming up with the same problem.

it seems if i try the Rand.Int with a subscript of (cards (1 ,10)) it will error, or if i were to use a for statment where i would equal 1 .. 10, etc.

Could you help me out?

- Geoff.
Tony




PostPosted: Wed Dec 12, 2007 2:57 pm   Post subject: RE:Quick Question On feasibillity (rand command, with use from a multidimentional arrays)

code:

put cards[Rand.Int(1,10)]

should work... otherwise state your errors. I don't actually have Turing to test any of this out.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Clayton




PostPosted: Wed Dec 12, 2007 3:44 pm   Post subject: RE:Quick Question On feasibillity (rand command, with use from a multidimentional arrays)

Almost Tony:

Turing:
puts cards (Rand.Int (1, 10))


or better yet:

Turing:

puts cards (Rand.Int (1, upper (cards))


allowing for any size of array to be used without having to change hard-coded values Smile
GZA




PostPosted: Thu Dec 13, 2007 2:09 pm   Post subject: Re: Quick Question On feasibillity (rand command, with use from a multidimentional arrays)

It works fantasticly Laughing, I appreciate your help.

Although, while im asking questions towards this topic, I was wondering if i could add the random integer value displayed from the Rand command, to an empty INT varible that is seperate from my array.End resulting in, a random money value displayed, and added to my "amount" varible.

(I.E money (Rand.Int (1, 18) added to var number1 :int :=0)

I have tried; put "random sentence here..", amount1 + money (Rand.Int (1, 18))
resulting in (amount1 = false) when amount1 is displayed in a procedure.

Additionally, for a similar program, I want to display a specific picture (using Pic.Load command) with the randomly generated number from my array. Which is using the same (cards (Rand.Int (1,18) ) code. end resulting in, displaying a random card, with a picture of that random card.

its really confusing.

- Geoff.
Sponsor
Sponsor
Sponsor
sponsor
richcash




PostPosted: Fri Dec 14, 2007 1:32 pm   Post subject: Re: Quick Question On feasibillity (rand command, with use from a multidimentional arrays)

It looks like you want to use the random value over and over again. In that case, you should make a variable that equals this random number in the first place.
code:
var random := Rand.Int (1, 18)
puts cards (random)
amount += money (random)
Pic.Draw (cards (random), x, y, picCopy)

I think you wan these to all be the same card number, right?

Also, I don't get what you mean by (amount1 = false) when displayed in a procedure. Could you explain that further, possibly with code?

And do you need help with loading the pictures into the array or have you already done that?
Tony




PostPosted: Fri Dec 14, 2007 2:44 pm   Post subject: Re: Quick Question On feasibillity (rand command, with use from a multidimentional arrays)

GZA @ Thu Dec 13, 2007 2:09 pm wrote:

(I.E money (Rand.Int (1, 18) added to var number1 :int :=0)

you need to actually assign the new value to the variable
code:

number1 := number1 + Rand.Int(1,18)

if you just display the result of the sum in a put statement, it's not saved anywhere, so the number1 variable will keep its original value.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
GZA




PostPosted: Mon Dec 17, 2007 1:10 pm   Post subject: Re: Quick Question On feasibillity (rand command, with use from a multidimentional arrays)

richcash @ Fri Dec 14, 2007 1:32 pm wrote:

...And do you need help with loading the pictures into the array or have you already done that?


I need alittle help, i know that it needs to be in an array ( 1 .. 52) but im not sure how to declare it syntax wise... since the array would be need to be full of the picture files, and or the Pic.Screenload command. I wouldnt know where to begin, on making this array for the picture files.

Everthing else is going smoothly, so far.

- Geoff.
Nick




PostPosted: Mon Dec 17, 2007 1:49 pm   Post subject: RE:Quick Question On feasibillity (rand command, with use from a multidimentional arrays)

while using Pic.FileNew and Pic.Draw is usually better the Pic.ScreenLoad but to create the array you ould say
code:
var picArray:array 1..52 of int
for i:1..upper(picArray)
picArray(i):=Pic.FileNew("pic"i+ntstr(i)+".bmp")
end for


this will create an array with each element owning a diffrent picture
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 12 Posts ]
Jump to:   


Style:  
Search: