Psuedocode leading to VB6 *Help needed*
Ok im pretty lost here about what im supposed to do. I have been given a task for school.
I need to assign 25 locations on a 15x15 2d array randomly. For these locations i would simply use value of 1, and the other locations a value of 0.
My first task is to write this in pseudocode, but this leads on to writing it in VB6 so i hoped this forum would be a good place to start for help. Any and all help would be appreciated. Thanks a bundle.
heres a kindof example of what i think i should be doing (mightn be right though, and its in javascript)
Randomise Array in Javascript
If you need me to rephrase or nething just ask and tell me which part and ill try.
ps. sorry if its in the wrong section, i had a look through all of em, and this looked to be the most correct one
:eek2: edit :eek2:
im slowly figuring out the fake code stuff. im kinda lost though
wat ive figured is
'fill with 0's
until Y = 15 ( (until X = 15 ( array ( X ) = 0 ) X + 1 repeat )( array( Y ) = 0 ) y + 1 repeat )
For Count 1 to 25
'grid location
$randomX = randomnumber() * 15
$randomY = randomnumber() * 15
IF array( $randomX,$randomY ) = 1
Generate new random grid location
Else array( $randomX,$randomY) = 1
would that fill my 2d array with randomly placed 1's? :confused:
Re: Psuedocode leading to VB6 *Help needed*
You could shorten it a bit
VB Code:
For Count 1 to 25
'grid location
do
randomX = randomnumber() * 15
randomY = randomnumber() * 15
until array(randomX,randomY) = 0
array(randomX,randomY) = 1
next Count
Re: Psuedocode leading to VB6 *Help needed*
thnx heaps. That was the tricky part, now to find a way to sensibly code that in vb6.
edit
*any help would be awesome*
Re: Psuedocode leading to VB6 *Help needed*
Quote:
Originally Posted by -=J_D=-
thnx heaps. That was the tricky part, now to find a way to sensibly code that in vb6.
Al42 pretty much did. (you also don't need to fill the array with 0, if you use a numeric type - a byte in this case presumably)
Re: Psuedocode leading to VB6 *Help needed*
Quote:
Originally Posted by bushmobile
Al42 pretty much did. (you also don't need to fill the array with 0, if you use a numeric type - a byte in this case presumably)
i pretty much have only just started with vb, and i get a syntax error when i put that code in
For Count 1 to 25
&
until array(randomX,randomY) = 0
array(randomX,randomY) = 1
come up in red, which i presume is where the error is??
Re: Psuedocode leading to VB6 *Help needed*
For line needs and "="
Until line needs a "Loop" in front of it (to complete the do loop)
the last line is red because Array is a reserved word, so you have to declared your array as something different.
Re: Psuedocode leading to VB6 *Help needed*
ok, im up to here, i think it works. is there some way to view the entire array to see ifit really did what it was supposed to?
this is where im up to with the code now
VB Code:
Option Explicit
Function Random(Lowerbound As Long, Upperbound As Long)
Randomize
Random = Int(Rnd * Upperbound) + Lowerbound
End Function
Private Sub Command1_Click()
Dim randomX As Integer
Dim randomY As Integer
Dim count As Integer
For count = 1 To 25
'grid location
Do
randomX = Random(1, 15)
randomY = Random(1, 15)
Loop Until tombset(randomX, randomY) = 0
tombset(randomX, randomY) = 1
Next count
End Sub
and the module
VB Code:
Public tombset(15, 15) As Integer
Public Random As Integer
Re: Psuedocode leading to VB6 *Help needed*
you could use something like this to display the result:
VB Code:
Private Sub Command2_Click()
Dim N As Long, I As Long
For N = LBound(tombset, 1) To UBound(tombset, 1)
For I = LBound(tombset, 2) To UBound(tombset, 2)
Debug.Print tombset(I, N);
Next I
Debug.Print
Next N
End Sub
a couple of comments on your code:
since arrays are (by default) 0-based, your tombset(15, 15) is actually a 16x16 grid, and as such your random number code won't ever put a 1 in the left hand column.
the function random will return a variable, you don't need the publicly declared random in the module. At the moment your function returns a Variant, but you should strongly type all your functions:
VB Code:
Private Function Random(Lowerbound As Long, Upperbound As Long)[b] As Long[/b]
Random = Int(Rnd * Upperbound) + Lowerbound
End Function
I have also removed the Randomize, it should only be called once as multiple calls can reduce the "randomness". Place it in Form_Load.
Re: Psuedocode leading to VB6 *Help needed*
okals, reason i had randomize was that when i was googling as ive been trying my very hardest, people kept saying that it comes up with the same random numbers and stuff, i didnt quite understand and i thought it would be good. but thnx for pointing me right direction.
this subprocedure
Private Sub Command2_Click()
i made a button for it, but i dont get any output from it? should i have a txtbox? listbox?
Re: Psuedocode leading to VB6 *Help needed*
it'll print out to the Immediate Window. Ctrl+G to bring it up, or View -> Immediate Window