Quantcast
Channel: VBForums - Visual Basic 6 and Earlier
Viewing all articles
Browse latest Browse all 21848

Problem with sorting algorithm

$
0
0
Basically my program is supposed to sort a 2d array of scores from a spreadsheet and display them in a picture box, but the sorted scores are not being displayed, any help would be appreciated. The code is from my separate high score form:

Dim username As String



Private Sub Form_Load()
Call getscore(username)
Call sortscores((username))
End Sub


Sub getscore(ByRef username)
username = InputBox("Congralutions, your score was " & QuizForm.score & ". Please enter your name for the list of high scores.")
End Sub


Sub sortscores(ByVal username)

Set xlwbook = xl.Workbooks.Open("H:\Computing Stuff\Advanced Higher Computing\Project\High Scores.xls")
Set xlsheet = xlwbook.Sheets.Item(1)

'Counts the records in the spreadsheet
Counter = 1
Do
If xlsheet.Cells(Counter, 1) <> "" Then
Record = True
Counter = Counter + 1
Else
Record = False
End If
Loop Until Record = False

Dim highscores(1 To 20, 1 To 2) As Variant

'Loads the scores to the 2d array
For times = 1 To Counter

highscores(times, 1) = xlsheet.Cells(times, 1)
highscores(times, 2) = xlsheet.Cells(times, 2)

Next times

'Adds the user's record to the spreadsheet and 2d array

xlsheet.Cells(Counter + 1, 1) = username
highscores(Counter + 1, 1) = username
xlsheet.Cells(Counter + 1, 2) = QuizForm.score
highscores(Counter + 1, 2) = QuizForm.score
xlwbook.Save

Dim sortedscores(1 To 20, 1 To 2) As Variant
Dim firstloop As Integer, secondloop As Integer, position As Integer

'Sorts the scores

For firstloop = 1 To Counter

position = firstloop

For secondloop = 1 To Counter

If highscores(secondloop, 2) >= highscores(position, 2) Then

position = secondloop

End If

Next secondloop

sortedscores(firstloop, 1) = highscores(position, 1)
sortedscores(firstloop, 2) = highscores(position, 2)

highscores(position, 1) = " "
highscores(position, 2) = -1

Next firstloop

'This subroutine is suppose to display the top ten sorted scores, but it doesn't
For outloop = 1 To 10
picHighScores.Print sortedscores(outloop, 1); ""; sortedscores(outloop, 2)
Next outloop

xl.ActiveWorkbook.Close False, "H:\Computing Stuff\Advanced Higher Computing\Project\High Scores.xls"
xl.Quit
End Sub

Private Sub Form_Unload(Cancel As Integer)

Set xlwbook = Nothing
Set xl = Nothing

End Sub

Viewing all articles
Browse latest Browse all 21848

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>