What is wrong with my subroutine
Author |
Message |
JKJones
|
Posted: Sun Dec 05, 2010 10:10 pm Post subject: What is wrong with my subroutine |
|
|
Hello i'm making minesweeper and i thought itd be cool to make a high score board so the way i did it is every time they win the time it adds there score to an array. I redim the array to add 1 to the bounds of the array every time they win so it can store all the wins. Then i made a new form that has 1 to 5 listed and beside 1 is "highscore(1)"
beside 2 is "highscore(2)" and so on. The only thing i was left to do was rearange the array so "highscore(1)" is the lowest and then 2 in the next lowest and so on. I wasn't able to find a function or subroutine that would do this correctly for me so i decided to make my own. This is what i came up with...
VisualBASIC: |
Private Sub LowHighSort(ByVal array As System.Array)
Dim switchhold As Double
Dim highval As Double
highval = UBound(array)
For j As Double = 0 To 100
Dim k As Double
k = j
For i As Double = 0 To (highval - 1)
Dim e As Double
e = i
If array(e) > array(e + 1) Then
switchhold = array(e)
array(e) = array(e + 1)
array(e + 1) = switchhold
Else
If array(e) < array(e + 1) Then
Else
If array(e) = array(e + 1) Then
End If
End If
End If
Next
Next
End Sub
|
I'd say i did something wrong because it will randomly have the highest number at the top or i don't know it won't work could any body edit it so that it will work indefinitely help would be greatly appreciated thnks i'm using 2005 by the way |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
DemonWasp
|
Posted: Sun Dec 05, 2010 11:28 pm Post subject: RE:What is wrong with my subroutine |
|
|
You seem to be trying to sort, and you've invented some kind of bizarre sort on your own. I highly recommend you do the following two things:
1. Look for existing "sort" algorithms written into your VB platform. I recall VB being a ... deficient ... platform, but not so bad that it wouldn't have a relatively accessible sorting algorithm.
2. If that fails, look up Bubblesort on Wikipedia and try to translate that algorithm into VB.
Actually, I can probably translate relatively well:
code: |
Private Sub Sort ( ByRef data As System.Array )
Dim temp as Integer
Dim hasSwapped as Boolean
Loop
hasSwapped = False
For i as Integer = 0 to UBound ( data ) - 1
If data(i) > data(i+1) Then
temp = data(i)
data(i) = data(i+1)
data(i+1) = temp
hasSwapped = True
End If
End For
If Not hasSwapped Then
Exit Loop
End If
End Loop
End Sub
|
My VB is half-remembered from about 7 years ago, so you'll probably have to fix up my syntax, if you bother to use this at all. The big part I don't think I'm right on is the "Exit Loop" bit. The basic idea here is that you go through the array, swapping adjacent pairs of values that aren't in the correct order. Once you make it through the array without needing to perform any swaps, the data is sorted.
Note: I'm using ByRef, you were using ByVal. It's occurred to me that this is probably the problem: you may be sorting a copy of the data you passed in to your method. |
|
|
|
|
![](images/spacer.gif) |
JKJones
|
Posted: Mon Dec 06, 2010 1:27 am Post subject: RE:What is wrong with my subroutine |
|
|
Thanks i had tried others before they didn't do what i wanted. I tried yours and got pretty much the same results and i made sure that its the sort and not some other part of the program that is messing up. (P.S i searched bubble sort its basically the same thing i was trying to implement) |
|
|
|
|
![](images/spacer.gif) |
TerranceN
|
Posted: Mon Dec 06, 2010 4:37 pm Post subject: RE:What is wrong with my subroutine |
|
|
Try taking 5 or so out of order integers, then go through your subroutine by hand/on paper.
Edit: I could have swore there was another post before mine that I was replying to... Maybe I'm losing my mind, maybe I'm switching between tabs too fast, IDK. |
|
|
|
|
![](images/spacer.gif) |
JKJones
|
Posted: Mon Dec 06, 2010 4:53 pm Post subject: RE:What is wrong with my subroutine |
|
|
There was but i deleted it because the whole i figured that out. And if it makes any difference i realized my array was declared as a string instead of a double so i changed that but am still getting the same problems. I'll try going through it on paper.
Edit i went through it on paper it got through it and worked easily which of course isn't the case in reality
Um im not sure how but somehow i made it work thnks guys |
|
|
|
|
![](images/spacer.gif) |
|
|