|
-
Oct 16th, 2006, 10:00 PM
#1
Thread Starter
New Member
HELP! Sorting Numbers From Text File!
ok so there are 300 numbers in a text file, they are all out of order and im just trying to print all of them in order from smallest to biggest
i really need some help, please dont just direct me to another topic about this unless its exactly like the help i need
this is teh code i have so far, but it wont work
thank you so much, hopefully someone knows whats wrong
VB Code:
Dim nom(1 To 300)
Private Sub Form_Load()
Dim count As Single
Open "NUMBERS.TXT" For Input As #1
Do While EOF(1) = False
count = count + 1
Input #1, nom(count)
Loop
Close #1
End Sub
Private Sub cmdCalculate_click()
Call Sort
Call Show
End Sub
Private Sub Showz()
Dim i As Integer
picOutput.Cls
For i = 1 To 300
picOutput.Print nom(count)
Next i
End Sub
Private Sub Sort()
Dim passNum As Single, count2 As Single
'Bubble sort table in descending order by population
For passNum = 1 To 299
For count2 = 1 To 300 - passNum
If nom(count2) > nom(count2 + 1) Then
temp = nom(count2)
nom(count2) = nom(count2 + 1)
nom(count2 + 1) = temp
End If
Next count2
Next passNum
End Sub
-
Oct 16th, 2006, 10:26 PM
#2
Thread Starter
New Member
Re: HELP! Sorting Numbers From Text File!
double post, i came up with some new code but it prints the first number everytime instead of going to the next
cant any of you guys just look at my code for one sec and try to figure something out
VB Code:
Dim nom(1 To 500) As Single
Private Sub cmdDumb_Click()
Dim count As Single, flag As Boolean
Open App.Path & "\7Data.Txt" For Input As #1
For i = 1 To 100
Input #1, nom(i)
Do
count = 1
flag = 0
If nom(count) > nom(count + 1) Then
temp = nom(count)
nom(count) = nom(count + 1)
nom(count + 1) = temp
flag = 1
End If
picOutput.Print temp
count = count + 1
Loop Until flag = 0
Next i
Close #1
End Sub
-
Oct 17th, 2006, 12:04 AM
#3
Re: HELP! Sorting Numbers From Text File!
 Originally Posted by kapaka2
cant any of you guys just look at my code for one sec and try to figure something out
VB Code:
Dim nom(1 To 500) As Single
Private Sub cmdDumb_Click()
Dim count As Single, flag As Boolean
Open App.Path & "\7Data.Txt" For Input As #1
For i = 1 To 100
[COLOR=Red]Input #1, nom(i)[/COLOR]
Do
count = 1
flag = 0
If nom(count) > nom(count + 1) Then
temp = nom(count)
nom(count) = nom(count + 1)
nom(count + 1) = temp
flag = 1
End If
picOutput.Print temp
count = count + 1
Loop Until flag = 0
Next i
Close #1
End Sub
Try using Line Input instead of just Input.
Also, a quicker way, if the file isn't too big, is to load the entire file at once into an array, and then sorting that array.
Here's an example:
VB Code:
Option Explicit
Private Sub Command1_Click()
Dim lonFF As Long, strData() As String
Dim lonLoop As Long
'Get an available file handle.
lonFF = FreeFile
Open "C:\numbers.txt" For Input As #lonFF
'Load the entire contents of the file into strData variable
'while splitting it into an array.
strData() = Split(Input(LOF(lonFF), lonFF), vbNewLine)
Close #lonFF
'Sort the array.
ShellSortNumbers strData()
lonFF = FreeFile
'Open the output file.
Open "C:\numbers (sorted).txt" For Append As #lonFF
'Loop through sorted array and print to file.
For lonLoop = 0 To UBound(strData())
Print #lonFF, strData(lonLoop)
Next lonLoop
Close #lonFF
End Sub
'MSDN's version of sorting numbers (SLOW).
'Replace with your own.
Sub ShellSortNumbers(vArray As Variant)
Dim lLoop1 As Long
Dim lHold As Long
Dim lHValue As Long
Dim lTemp As Long
'startTime = Time()
lHValue = LBound(vArray)
Do
lHValue = 3 * lHValue + 1
Loop Until lHValue > UBound(vArray)
Do
lHValue = lHValue / 3
For lLoop1 = lHValue + LBound(vArray) To UBound(vArray)
lTemp = vArray(lLoop1)
lHold = lLoop1
Do While vArray(lHold - lHValue) > lTemp
vArray(lHold) = vArray(lHold - lHValue)
lHold = lHold - lHValue
If lHold < lHValue Then Exit Do
Loop
vArray(lHold) = lTemp
Next lLoop1
Loop Until lHValue = LBound(vArray)
'endTime = Time()
'timeToSort = endTime - startTime
'timeTaken = timeTaken & "; Shell Sort: " & timeToSort
End Sub
-
Oct 17th, 2006, 03:50 AM
#4
Re: HELP! Sorting Numbers From Text File!
Try using Line Input instead of just Input.
who said the file had lines?
do a search on this forum for quick sort or bubble sort
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|