At the moment i have a lbl asking the user how many numbers they want, with a txtbox named txtInput. When they click a cmd button, id like it to print out all the numbers between the number they chose in a random order with no repeats, in a "Bubble Sort". Im assuming id use a loop to loop through each of the numbers, but im not exactly sure.
*Thanks - Line
wilbert 09-10-2003, 08:55 AM Use a loop to go through all the different numbers, also use an array to store all the numbers you used in. Then check the array if the chosen number allready exists. If not add it to the array then print it.
Wilbert
Yes, I was quite sure id have to use a loop somewhere, but Id also need a "Bubble Sort" in it as well.
This is what Im guessing it needs to have.. *shrug*
Get the number they put in the txtbox (simple variable).
Take the number and put it into a unsorted array with no repeats. (no idea)
Use a "Bubble Sort" (no idea)
Time how long it took to do it all (timer)
If anyone has any help/coding/whatever, itd be appriciated, thx :]
* - Line
wilbert 09-10-2003, 11:22 PM For the array:
Use 'Redim Preserve Myarray(1 to MyNumbers)'
to expand your array. And use a loop
'For X = 1 to Ubound(MyNumbers)' to check if a number allready exists.
If you don't know these statements, do a search on the forum.
Wilbert
blindwig 09-11-2003, 12:03 AM This is what Im guessing it needs to have.. *shrug*
Get the number they put in the txtbox (simple variable).
Take the number and put it into a unsorted array with no repeats. (no idea)
Use a "Bubble Sort" (no idea)
Time how long it took to do it all (timer)
Use IsNumeric() and Val() functions
Put a bunch of sequential numbers into an array, then "shuffle" them (like a reverse insertion sort). Like this (http://visualbasicforum.com/showpost.php?postid=451259&postcount=4) or this (http://visualbasicforum.com/showpost.php?postid=451263&postcount=5)
A bubble sort is one of the easiest sorting methods to understand and implement, and for small arrays it is usually the fastest, due to it's lack of overhead and recursion. See Squirm's sorting tutorial (http://visualbasicforum.com/t78889.html) for details on different sorting methods.
I wouldn't use a timer control, but use the timer function to record the start and finish times, then subtract them to get the difference, which will be the time taken.
Option Explicit
Dim numInput As Integer
Dim x As Integer
Private Sub cmdProcess_Click()
numInput = Val(txtInput)
End Sub
Private Sub txtInput_KeyPress(KeyAscii As Integer)
If Not IsNumeric(Chr(KeyAscii)) And (KeyAscii <> 8) Then
KeyAscii = 0
End If
End Sub
is the basis of what i have, ive taken a look at the sorting/array coding, but i wouldnt have the slightest idea on how to impliment it into my coding, like what to subsitute for the txtbox that the user will be typing in thier number.
* - Line
Ok, let me try to rephrase simply:
All of the coding examples you've shown, I have no idea how I would impliment that into my coding for it to work.
blindwig 09-11-2003, 01:33 PM Ok, let me try to rephrase simply:
All of the coding examples you've shown, I have no idea how I would impliment that into my coding for it to work.
Well, let's take a look at what you want.
You want to generate a list of sequential numbers, the length of which is user-specified, that will be out of numerical order.
'First step is to get the user input
numInput = Val(txtInput)
if numInput > 0 then 'make sure that the number is valid
'Now create the array
dim MyNumberList() as long
redim MyNumberList(numInput) as long
'And populate the array with sequential numbers
dim LoopVar as long
for LoopVar = LBound(MyNumberList) to UBound(MyNumberList)
MyNumberList(LoopVar)=LoopVar
next LoopVar
'Now you want to shuffle the list
dim ShuffleNum as long, TempVar as long
for LoopVar = LBound(MyNumberList) to UBound(MyNumberList)
'pick an item to swap the current one with
ShuffleNum=Int(rnd*(UBound(MyNumberList)-LBound(MyNumberList)+1))+LBound(MyNumberList)
'swap the 2 items
TempVar = MyNumberList(LoopVar)
MyNumberList(LoopVar) = MyNumberList(ShuffleNum)
MyNumberList(ShuffleNum) = Temp
next LoopVar
end if 'numInput > 0
Ok, there are no errors.. but lets say i type in "5"
it comes out as..
4
0
0
3
0
0
blindwig 09-11-2003, 02:00 PM Ok, there are no errors.. but lets say i type in "5"
it comes out as..
4
0
0
3
0
0
Yes that's normal. Arrays normally start with a 0 (depending on your Option Base setting), so if you tell it to dim an array up to 5, it dims an array with elements 0-5, which is 6 elements. To fix it:
'you either need to reduce the size of the array
numInput = numInput - 1
'or you need to force the array to start at 1
ReDim MyNumberList(1 to numInput) As Long
but don't do both, or your array will be 2 short ;)
Yes, that did reduce it to 5 characters, but there shouldnt b 2 of the same numbers in the array, it comes out to like 2 or 3 0's
Recap:
Ask How Many #'s.. then make an unsorted array with no repeats, and include a bubble sort :p
technicly speaking the only thing that needs to be fixed is the "more than 1 of the same number" "error"
blindwig 09-11-2003, 02:29 PM Yes, that did reduce it to 5 characters, but there shouldnt b 2 of the same numbers in the array, it comes out to like 2 or 3 0's
Recap:
Ask How Many #'s.. then make an unsorted array with no repeats, and include a bubble sort :p
technicly speaking the only thing that needs to be fixed is the "more than 1 of the same number" "error"
Well there shouldn't be more than 1 of any number in the array, so there must be a bug in the code. Put in a routine to print out the whole array, and then put that in different points of the programs to see at what point does the problem show up, and then troubleshoot it from there.
Public Sub ShuffleItUp(ToShuffle() As Long)
Dim i As Long, temp As Long, index As Long
Randomize
For i = LBound(ToShuffle) To UBound(ToShuffle)
index = Int((UBound(ToShuffle) - LBound(ToShuffle) + 1) * Rnd + LBound(ToShuffle))
temp = ToShuffle(index)
ToShuffle(index) = ToShuffle(i)
ToShuffle(i) = temp
Next i
End Sub
Public Sub TehSortMachine(ToSort() As Long)
Dim i As Long, j As Long, temp As Long
For i = LBound(ToSort) To UBound(ToSort)
For j = i To UBound(ToSort)
If ToSort(j) < ToSort(i) Then
temp = ToSort(i)
ToSort(i) = ToSort(j)
ToSort(j) = temp
End If
Next j
Next i
End Sub
Private Sub cmdProcess_Click()
Dim MyNumberList() As Long, i As Long
ReDim MyNumberList(0 To CLng(txtInput.Text))
For i = LBound(MyNumberList) To UBound(MyNumberList)
MyNumberList(i) = i
Next i
Call ShuffleItUp(MyNumberList())
For i = LBound(MyNumberList) To UBound(MyNumberList)
lstFinal.AddItem MyNumberList(i)
Next i
Call TehSortMachine(MyNumberList())
'add it to a box somewhere.
For i = LBound(MyNumberList) To UBound(MyNumberList)
lstResults.AddItem MyNumberList(i)
Next i
End Sub
Private Sub txtInput_KeyPress(KeyAscii As Integer)
If Not IsNumeric(Chr(KeyAscii)) And (KeyAscii <> 8) Then
KeyAscii = 0
End If
End Sub
Everything there works fine, (i decided to use a different sort)
1 last thing remains..
How would I go about timing this? (the time it takes to sort all of this)
blindwig 09-11-2003, 02:58 PM How would I go about timing this? (the time it takes to sort all of this)
check the timer function before and after, then compare the two:
StartTime = Timer
'do some stuff here
FinishTime = Timer
'See how long that took:
TotalTime = FinishTime - StartTime
'trap midnight roll-over
If TotalTime < 0 then TotalTime = TotalTime + 86400
If you're timing something that you think will take more than 24 hours, then use the NOW and DATEDIFF functions:
StartTime = Now
'do some stuff here
FinishTime = Now
'See how long that took:
TotalTime = DateDiff("s", StartTime, FinishTime)
In either case, the TotalTime variable will contain the number of seconds.
Agent707 09-11-2003, 03:15 PM VB:
--------------------------------------------------------------------------------
Public Sub ShuffleItUp(ToShuffle() As Long)
Dim i As Long, temp As Long, index As Long
Randomize
.....
You should put Randomize in the program load routine. It only needs called once.
Agent707 09-11-2003, 03:30 PM Here is some code that's close to what you need - I think.
Only problem is there is some bug in it where it dup's some numbers and leaves some numbers off.
But with the Guru's here, I'm sure that can be resolved. Hope this gives you ideas.
Private Sub Form_Load()
Randomize
End Sub
Private Sub Command1_Click()
Dim Stack1() As Long
Dim Stack2() As Long
Dim N As Long
Dim R As Long
Dim lNum As Long
On Error Resume Next
iNum = CLng(InputBox("Enter Your Number", "Enter Number"))
If Not IsNumeric(lNum) Then
Exit Sub
End If
ReDim Stack1(lNum)
ReDim Stack2(lNum)
List1.Clear
For N = 1 To lNum
Stack1(N) = N
Next
For N = lNum To 1 Step -1
R = Rnd * (N + 1)
Stack2(N) = Stack1(R)
Stack1(R) = Stack1(N)
List1.AddItem CStr(Stack2(N))
Next
End Sub
Only problem is there is some bug in it where it dup's some numbers and leaves some numbers off.
It doesnt leave any numbers off... the prog works fine from what i can see..
If i type in 21.. it gives me all the numbers from 0 to 21 without dropping a single number... *shrug*
Ok, completed.
Id like to thank all who helped mainly "blindwig" for your fast and accurate repsonses. :) Thx.
Edit:
Public Sub TehSortMachine(ToSort() As Long)
Dim i As Long, j As Long, temp As Long
For i = LBound(ToSort) To UBound(ToSort)
For j = i To UBound(ToSort)
If ToSort(j) < ToSort(i) Then
temp = ToSort(i)
ToSort(i) = ToSort(j)
ToSort(j) = temp
End If
Next j
Next i
End Sub
Quick Question : The above is a bubble sort, correct?
and.. #2
I just had a crazy thought.. if im displaying random numbers from 0 to the number the user types in.. so letz say they type in 5.. and it comes out as..
2
4
1
0
3
5
but.. a bubble sort, sorts the numbers from smallest to biggest.. so it would be in order wouldnt it? And when i take out my Call BubbleSort line, and run the app, to my knowledge nothing changes.. :\
- Line
Now that im getting really confused.. ive noticed that the program works WITHOUT "TehSortMachine" .. so im guessing thats not doing anything.. or something..
*very lost*
blindwig 09-11-2003, 04:36 PM Id like to thank all who helped mainly "blindwig" for your fast and accurate repsonses. :) Thx.
The faster my responces, the slower my day at work must be going :)
Quick Question : The above is a bubble sort, correct?
No, that looks more like a selection sort. A bubble sort is similiar, but you only compare adjacent values. See Squirm's sorting tutorial (http://visualbasicforum.com/t78889.html) for more info on different sorting methods.
|