| | |
VB6 help
Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Jul 2007
Posts: 7
Reputation:
Solved Threads: 0
Hi, I'm just starting out in programming and I'm currently making a small program which shows how Bubble, selection and Insertion sorts work. I've found a small program which bubble sorts a user selected number of integers and shows each step at which these are sorted (each pass). However this program doesn't sort the way a bubble sort should work (eg the largest element should be at the last index on the first pass).
I'm in the middle of altering the source code, this is what it was originally:
you can see that if the first element isn't bigger then the second it will increment only y so if the first elemnt is bigger then the third the first and third will switch positions (like a selection sort).
I want the code to look something like this:
so that if the first element is smaller then the second, the first element stays in current position and then the second elemnt is compared to the third. Bust since I'm only new to programming i have no idea what to type.
Help is appreciated. Thank you.
I'm in the middle of altering the source code, this is what it was originally:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
Private Sub Command1_Click() For x = 1 To UBound(myArr) - 1 For y = (x + 1) To UBound(myArr) If myArr(x) >= myArr(y) Then doSwap x, y End If Next y Next x End Sub
you can see that if the first element isn't bigger then the second it will increment only y so if the first elemnt is bigger then the third the first and third will switch positions (like a selection sort).
I want the code to look something like this:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
Private Sub cmdSort_Click() For x = 1 To UBound(Sarray) - 1 For y = (x + 1) To UBound(Sarray) If Sarray(x) >= Sarray(y) Then doSwap x, y Else increment y increment x End If End Sub
so that if the first element is smaller then the second, the first element stays in current position and then the second elemnt is compared to the third. Bust since I'm only new to programming i have no idea what to type.
Help is appreciated. Thank you.
Last edited by Pastafarian; Jul 12th, 2007 at 4:29 am.
Hi,
Use This Code:
Regards
Veena
Use This Code:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
Dim i As Integer Dim J As Integer Dim k ' For i = 1 To UBound(SArray) For J = i To 1 Step -1 If Val(SArray(J)) < Val(SArray(J - 1)) Then k = SArray(J - 1) SArray(J - 1) = TArr(J) SArray(J) = k Else Exit For End If Next Next
Regards
Veena
•
•
Join Date: Jul 2007
Posts: 7
Reputation:
Solved Threads: 0
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
k = SArray(J - 1) SArray(J - 1) = TArr(J) SArray(J) = k
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
Else Exit For End If Next Next
Why can't I put this in?
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
For x = 1 To UBound(Sarray) - 1 For y = (x + 1) To UBound(Sarray) If Sarray(x) >= Sarray(y) Then doSwap x, y Else Exit For Next y Next x End If Next y Next x
I get a compile error stating 'Next without For' on the first Next.
Last edited by Pastafarian; Jul 15th, 2007 at 3:51 am.
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
For x = 1 To UBound(Sarray) - 1 For y = (x + 1) To UBound(Sarray) If Sarray(x) >= Sarray(y) Then doSwap x, y Else Exit For Next y Next x End If Next y Next x
I think that there is really a compiler error here.. you see your ebd if,, it is in a wrong place...
try replacing your code above with this one...
hope it helps
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
For x = 1 To UBound(Sarray) - 1 For y = (x + 1) To UBound(Sarray) If Sarray(x) >= Sarray(y) Then doSwap x, y End If Next Next
note that I don't use any else statement since if you want to increment just the second ( variable y) as variable x remains in its position then all you have to do is to increment 'y' and after the last element in the array is being compared then that's the only time the variable y increment..
Regards
Cruize²
Last edited by Cruize_Invades; Jul 15th, 2007 at 4:33 am.
•
•
Join Date: Jul 2007
Posts: 7
Reputation:
Solved Threads: 0
Hi Cruize,
I've used your code in the program and here is the output (4 array values used).
11 2 5 9
2 11 5 9 ------pass 1
2 5 11 9 ------pass 2
2 5 9 11 ------pass 3
This isn't the expected output, as a bubblesort should bring the largest value to the right of pass 1 (which is 11). It's only sorting by one position at a time, what it should look like is this:
11 2 5 9
2 5 9 11 ----pass 1
2 5 9 11 ----pass 2
the 11 is compared to the 2, since it is greater it is swapped (temp), then it is compared to 5 swapped (temp), then 9 swapped and stored in the last index. After the first pass the x value is incremented as well as y value, so 5 is compared to 9, since it is not greater it doesn't swap and should increment again to x=9 and y=11.
So what it's doing should be shown as one pass, ie the final result of your code should be displayed as the first pass. It's a little difficult with those numbers. Heres another example with 3 values (i've added x and y to show you what i mean):
6 2 0
2 6 0
0 6 2
0 2 6
It should look like this
6 2 0
x y
2 6 0
x y
2 0 6 ---- pass 1
x y
0 2 6
x y
0 2 6 -----pass 2
I've used your code in the program and here is the output (4 array values used).
11 2 5 9
2 11 5 9 ------pass 1
2 5 11 9 ------pass 2
2 5 9 11 ------pass 3
This isn't the expected output, as a bubblesort should bring the largest value to the right of pass 1 (which is 11). It's only sorting by one position at a time, what it should look like is this:
11 2 5 9
2 5 9 11 ----pass 1
2 5 9 11 ----pass 2
the 11 is compared to the 2, since it is greater it is swapped (temp), then it is compared to 5 swapped (temp), then 9 swapped and stored in the last index. After the first pass the x value is incremented as well as y value, so 5 is compared to 9, since it is not greater it doesn't swap and should increment again to x=9 and y=11.
So what it's doing should be shown as one pass, ie the final result of your code should be displayed as the first pass. It's a little difficult with those numbers. Heres another example with 3 values (i've added x and y to show you what i mean):
6 2 0
2 6 0
0 6 2
0 2 6
It should look like this
6 2 0
x y
2 6 0
x y
2 0 6 ---- pass 1
x y
0 2 6
x y
0 2 6 -----pass 2
Last edited by Pastafarian; Jul 15th, 2007 at 7:05 am.
I got confused, well this is my understanding, correct me if I am wrong,
there should be an indicator to tell if the array is sorted in ascending.. and if not the sorting should continue after each passes...
I'd like to explain this how, but try it and you'll understand...
Hope this answers your question... sorry for the late post,, I got busy this past few days.. and by the way, I think you can still minimize my code.
there should be an indicator to tell if the array is sorted in ascending.. and if not the sorting should continue after each passes...
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
dim check as integer check = -1 while check=-1 For x = 1 To UBound(Sarray) - 1 y = x + 1; If Sarray(x) >= Sarray(y) Then doSwap x, y End If Next 'end of the pass 'this would check the array each passes for x = 1 to UBound(Sarray) -1 if sarray(x) > sarray(x+1) check=-1 exit for else check=0 end if next wend
I'd like to explain this how, but try it and you'll understand...
Hope this answers your question... sorry for the late post,, I got busy this past few days.. and by the way, I think you can still minimize my code.
Last edited by Cruize_Invades; Jul 18th, 2007 at 6:00 am.
•
•
Join Date: Jul 2007
Posts: 7
Reputation:
Solved Threads: 0
Hi,
I've run through the code and done a deskcheck on 4 values and it is correct.
I've made the following chnages to the code as there was 2 lines that wernt accepted.
I've run through the code and done a deskcheck on 4 values and it is correct.
I've made the following chnages to the code as there was 2 lines that wernt accepted.
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
Dim check As Integer check = -1 While check = -1 For x = 1 To UBound(Sarray) - 1 y = x + 1; 'THIS line wasnt recognised change to y=(x+1) and nothing would work when i click my sort command button If Sarray(x) >= Sarray(y) Then doSwap x, y End If Next 'end of the pass 'this would check the array each passes For x = 1 To UBound(Sarray) - 1 If Sarray(x) > Sarray(y) Then 'ADDED THEN and still wouldnt dort so changed x+1 to y and it still didn't work:( check = -1 Exit For Else check = 0 End If Next Wend
Last edited by Pastafarian; Jul 18th, 2007 at 7:02 am.
I'm really sorry for the trouble.. I misused the syntax for I have a project on C++ in creating a game. Sorry for the trouble.. and thanks for reminding me. I GOT LOST.... THIS IS VB..
int the first error, there is simple a semicolon in the end of the code... you see.. that shoudn't be there...
about the second mistake I have... I am wondering, wouldn't there should be
int the first error, there is simple a semicolon in the end of the code... you see.. that shoudn't be there...
about the second mistake I have... I am wondering, wouldn't there should be
y=x+1 before the if statement... since that if you're going to compare the first element and the next... Last edited by Cruize_Invades; Jul 18th, 2007 at 7:12 am.
•
•
Join Date: Jul 2007
Posts: 7
Reputation:
Solved Threads: 0
I've deskchecked and its correct, except it just doesn't seem to work when i click the sort button, perhaps something to do with my doSwap or DoShow functions?
ie i get no errors when i click the sort button, just nothing is displayed.
EDIT: ok i got it to sort, but it still doesn't do it correctly, could it be where my doShow function is? because it occurrs within DoSwap.
ie i get no errors when i click the sort button, just nothing is displayed.
EDIT: ok i got it to sort, but it still doesn't do it correctly, could it be where my doShow function is? because it occurrs within DoSwap.
Last edited by Pastafarian; Jul 18th, 2007 at 9:00 am.
![]() |
Similar Threads
- VB6 Database Program (Visual Basic 4 / 5 / 6)
- VB6 and MS Access 2002 (Visual Basic 4 / 5 / 6)
- "Error in linking List box with the VB6.0 database" (Visual Basic 4 / 5 / 6)
- New key word not recoginized in VB6 connecting to Access (Visual Basic 4 / 5 / 6)
- Deploying VB6 onto different platform (Visual Basic 4 / 5 / 6)
- XP and VB6 (Windows NT / 2000 / XP)
- Vb6 and XP (Visual Basic 4 / 5 / 6)
Other Threads in the Visual Basic 4 / 5 / 6 Forum
- Previous Thread: VB 6.0 - Accepting value (email address)from Inputbox
- Next Thread: Visual & Excel without referencing Excel???
| Thread Tools | Search this Thread |
* 6 429 2007 access activex add age append application basic beginner birth bmp calculator cd cells.find click client code college column component connection connectionproblemusingvb6usingoledb copy creat ctrl+f data database datareport date delete dissertations dissertationthesis dissertationtopic edit error excel excelmacro file filename form hardware header iamthwee image inboxinvb internetfiledownload keypress label listbox listview liveperson login looping machine microsoft movingranges number objectinsert open oracle password prime program prompt range-objects readfile reading record refresh remotesqlserverdatabase report retrieve save search sendbyte sites sort sql sql2008 sqlserver subroutine table tags textbox time urldownloadtofile vb vb6 vb6.0 vba visual visualbasic visualbasic6 web window windows





