MedVision ad

Random Number Program (1 Viewer)

A J C

New Member
Joined
Feb 14, 2003
Messages
9
Random Number Program (BROWSE DOWN, I POSTED MY ANSWER)

I am trying to create a program that will display 1000 Random Numbers in a Text Box, but the code MUST utilise arrays

any help would be appreciated
 
Last edited:

del

Member
Joined
Jul 8, 2002
Messages
412
Gender
Undisclosed
HSC
N/A
declare an array with 1000 elements.....
using a FOR - NEXT loop cycle through each element, generating a random number, then assigning it to the coresponding element of the array.

then to display in a text box, just loop through the array again.....

to generate an assign:

array[1 to 1000] as integer
FOR index = 1 to 1000
array[index] = int((Rnd * 1000) + 1)
NEXT index


to print:

FOR index = 1 to 1000
textbox = textbox & array[index] & \n
NEXT index

just using \n to represent to start a new line

if that's what you mean?
 
Last edited:

A J C

New Member
Joined
Feb 14, 2003
Messages
9
REPLY

Hey while i was waiting i did the program in vb, got bored

Basically just a Command button and List Box

Private Sub cmdGenerate_Click()
Dim RandomArray(1 To 1000) As Integer
Dim index As Integer
For index = 1 To 1000
Randomize
RandomArray(index) = Int((Rnd * 1000) + 1)
ArrayList.AddItem (RandomArray(index))
Next index
End Sub

What i forgot to put in the initial question was that the
random numbers must be sorted using the bubble sort
method.

Thanks for the info anyways
 

del

Member
Joined
Jul 8, 2002
Messages
412
Gender
Undisclosed
HSC
N/A
hmm good point Bon.

Sorts aren't hard AJC. Once you know the logic, it pretty much stays with you.

Simply pass the array to a sort procedure to do the sorting... well its own procedure isn't completley necessary, but it is good to practise writing nice and neatley structured/organised code.

have a look on the net... theres heaps coded sorts with explanations of how they work... actually if you get a deck of cards and work through an algorithm with the cards as elements... it makes more sense..

but sorts (and also searches) are *must knows*.....
 

A J C

New Member
Joined
Feb 14, 2003
Messages
9
REPLY TO BON AND ALIKE

Not to dis you guys/gals

But i know no one will be next to me in an exam.

The algorithm that i was given was completely screwed up, it followed logic that would drive you nuts. That is why i had a post up here. Not to get people to do my work.

I finally said to the algorithm that i was supposed to follow, SCREW IT, and made my own program.

Here is my code, proving i finished it and did it by myself

-------------------------------------------------------

Dim RandomArray(1 To 1000) As Integer
Dim index As Integer

Private Sub cmdGenerate_Click()

ArrayList.Clear

For index = 1 To 1000
Randomize
RandomArray(index) = Int((Rnd * 1000) + 1)
ArrayList.AddItem (RandomArray(index))
Next index


End Sub

Private Sub cmdSort_Click()

Sort.Clear

For Outer = 1 To 1000
For Inner = 1 To 999
If (RandomArray(Inner) < RandomArray(Inner + 1)) Then
temp = RandomArray(Inner)
RandomArray(Inner) = RandomArray(Inner + 1)
RandomArray(Inner + 1) = temp
End If
Next Inner
Next Outer

For j = 1 To 1000
Sort.AddItem RandomArray(j)
Next j
End Sub

--------------------------------------

I know algorithms and just to re-inforce the one that i was given follow was ridiculous and that is why i was having trouble.
 

del

Member
Joined
Jul 8, 2002
Messages
412
Gender
Undisclosed
HSC
N/A
cool congrats......

but to the original algorithm that you were given which was f$%^d up....

did you mean the one I posted up the top?, or you saying like one you got from school?

'cos me quite confused if it was the one I posted... 'cos it sure makes sense
 
Last edited:

A J C

New Member
Joined
Feb 14, 2003
Messages
9
this thread is getting longer by me, the person who started the thread, muhaha

I was referrring to an algorithm i recieved from school, Del
 

del

Member
Joined
Jul 8, 2002
Messages
412
Gender
Undisclosed
HSC
N/A
ahh ic... hehe i was lost

hehe, but in the process your boosting your post count.... :D
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top