Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jul 2007
Posts: 7
Reputation: Pastafarian is an unknown quantity at this point 
Solved Threads: 0
Pastafarian Pastafarian is offline Offline
Newbie Poster

VB6 help

 
0
  #1
Jul 12th, 2007
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:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub Command1_Click()
  2. For x = 1 To UBound(myArr) - 1
  3. For y = (x + 1) To UBound(myArr)
  4. If myArr(x) >= myArr(y) Then
  5. doSwap x, y
  6. End If
  7. Next y
  8. Next x
  9. 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)
  1. Private Sub cmdSort_Click()
  2. For x = 1 To UBound(Sarray) - 1
  3. For y = (x + 1) To UBound(Sarray)
  4. If Sarray(x) >= Sarray(y) Then
  5. doSwap x, y
  6. Else
  7. increment y
  8. increment x
  9. End If
  10. 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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 848
Reputation: QVeen72 is on a distinguished road 
Solved Threads: 120
QVeen72's Avatar
QVeen72 QVeen72 is offline Offline
Practically a Posting Shark

Re: VB6 help

 
0
  #2
Jul 12th, 2007
Hi,

Use This Code:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Dim i As Integer
  2. Dim J As Integer
  3. Dim k
  4. '
  5. For i = 1 To UBound(SArray)
  6. For J = i To 1 Step -1
  7. If Val(SArray(J)) < Val(SArray(J - 1)) Then
  8. k = SArray(J - 1)
  9. SArray(J - 1) = TArr(J)
  10. SArray(J) = k
  11. Else
  12. Exit For
  13. End If
  14. Next
  15. Next


Regards
Veena
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 7
Reputation: Pastafarian is an unknown quantity at this point 
Solved Threads: 0
Pastafarian Pastafarian is offline Offline
Newbie Poster

Re: VB6 help

 
0
  #3
Jul 15th, 2007
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. k = SArray(J - 1)
  2. SArray(J - 1) = TArr(J)
  3. SArray(J) = k
This just swaps the values right? because i've already got a function 'doSwap' which does it for me.

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Else
  2. Exit For
  3. End If
  4. Next
  5. Next
After the 'else' why does it exit the 'for'? because i want it to increment the two variables if the "IF' statement isn't satisfied.

Why can't I put this in?
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. For x = 1 To UBound(Sarray) - 1
  2. For y = (x + 1) To UBound(Sarray)
  3. If Sarray(x) >= Sarray(y) Then
  4. doSwap x, y
  5. Else
  6. Exit For
  7. Next y
  8. Next x
  9. End If
  10. Next y
  11. 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.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 21
Reputation: Cruize_Invades is an unknown quantity at this point 
Solved Threads: 1
Cruize_Invades's Avatar
Cruize_Invades Cruize_Invades is offline Offline
Newbie Poster

Re: VB6 help

 
0
  #4
Jul 15th, 2007
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1.  
  2. For x = 1 To UBound(Sarray) - 1
  3. For y = (x + 1) To UBound(Sarray)
  4. If Sarray(x) >= Sarray(y) Then
  5. doSwap x, y
  6. Else
  7. Exit For
  8. Next y
  9. Next x
  10. End If
  11. Next y
  12. 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)
  1. For x = 1 To UBound(Sarray) - 1
  2. For y = (x + 1) To UBound(Sarray)
  3. If Sarray(x) >= Sarray(y) Then
  4. doSwap x, y
  5. End If
  6. Next
  7. 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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 7
Reputation: Pastafarian is an unknown quantity at this point 
Solved Threads: 0
Pastafarian Pastafarian is offline Offline
Newbie Poster

Re: VB6 help

 
0
  #5
Jul 15th, 2007
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
Last edited by Pastafarian; Jul 15th, 2007 at 7:05 am.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 21
Reputation: Cruize_Invades is an unknown quantity at this point 
Solved Threads: 1
Cruize_Invades's Avatar
Cruize_Invades Cruize_Invades is offline Offline
Newbie Poster

Re: VB6 help

 
0
  #6
Jul 18th, 2007
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...

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. dim check as integer
  2. check = -1
  3.  
  4. while check=-1
  5. For x = 1 To UBound(Sarray) - 1
  6. y = x + 1;
  7. If Sarray(x) >= Sarray(y) Then
  8. doSwap x, y
  9. End If
  10. Next
  11. 'end of the pass
  12. 'this would check the array each passes
  13. for x = 1 to UBound(Sarray) -1
  14. if sarray(x) > sarray(x+1)
  15. check=-1
  16. exit for
  17. else
  18. check=0
  19. end if
  20. next
  21. 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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 7
Reputation: Pastafarian is an unknown quantity at this point 
Solved Threads: 0
Pastafarian Pastafarian is offline Offline
Newbie Poster

Re: VB6 help

 
0
  #7
Jul 18th, 2007
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.
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Dim check As Integer
  2. check = -1
  3.  
  4. While check = -1
  5. For x = 1 To UBound(Sarray) - 1
  6. y = x + 1; 'THIS line wasnt recognised change to y=(x+1) and nothing would work when i click my sort command button
  7. If Sarray(x) >= Sarray(y) Then
  8. doSwap x, y
  9. End If
  10. Next
  11. 'end of the pass
  12. 'this would check the array each passes
  13. For x = 1 To UBound(Sarray) - 1
  14. If Sarray(x) > Sarray(y) Then 'ADDED THEN and still wouldnt dort so changed x+1 to y and it still didn't work:(
  15. check = -1
  16. Exit For
  17. Else
  18. check = 0
  19. End If
  20. Next
  21. Wend
Last edited by Pastafarian; Jul 18th, 2007 at 7:02 am.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 21
Reputation: Cruize_Invades is an unknown quantity at this point 
Solved Threads: 1
Cruize_Invades's Avatar
Cruize_Invades Cruize_Invades is offline Offline
Newbie Poster

Re: VB6 help

 
0
  #8
Jul 18th, 2007
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 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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 7
Reputation: Pastafarian is an unknown quantity at this point 
Solved Threads: 0
Pastafarian Pastafarian is offline Offline
Newbie Poster

Re: VB6 help

 
0
  #9
Jul 18th, 2007
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.
Last edited by Pastafarian; Jul 18th, 2007 at 9:00 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 848
Reputation: QVeen72 is on a distinguished road 
Solved Threads: 120
QVeen72's Avatar
QVeen72 QVeen72 is offline Offline
Practically a Posting Shark

Re: VB6 help

 
0
  #10
Jul 18th, 2007
Hi Pasta,

Why not try my code..?

REgards
Veena
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Visual Basic 4 / 5 / 6 Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC