PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
-RESOLVED- Need Subroutine Help-VBForums
Page 1 of 2 12 LastLast
Results 1 to 40 of 63

Thread: -RESOLVED- Need Subroutine Help

  1. #1

    Thread Starter
    Lively Member InsanePyro's Avatar
    Join Date
    Nov 2007
    Posts
    76

    -RESOLVED- Need Subroutine Help



    I am super lost and super confused on how to do this...PLEASE HELP!
    Last edited by InsanePyro; Jan 22nd, 2008 at 08:33 PM.

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,135

    Re: Need Subroutine Help

    Can you guys stop posting that thing please - once is more than enough...

    Anyway, subroutine is just another way to name function, procedure, etc...
    The following is a sample subroutine:
    Code:
    Private Function Test(a As Long, b As Long) As Long
        Test = a + b
    End Function
    
    Private Sub Command1_Click()
    
        MsgBox Test(10, 15)
    
    End Sub
    btw, Command1_Click is also a subroutine.

  3. #3

    Thread Starter
    Lively Member InsanePyro's Avatar
    Join Date
    Nov 2007
    Posts
    76

    Re: Need Subroutine Help

    I know that I just don't know/understand how to do each thing that I need to do...

  4. #4

  5. #5

  6. #6
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    Washington DC
    Posts
    314

    Re: Need Subroutine Help

    Quote Originally Posted by InsanePyro
    I am super lost and super confused on how to do this...PLEASE HELP!
    Well, here is a QBasic answer for a hint.

    Mac

    Code:
    CLS
    DIM n(19) AS INTEGER
    DATA 334, 384, 105, 222, 61, 2, 175, 228, 114
    DATA 235, 241, 213, 206, 3, 321, 152, 214, 137, 224
    FOR i = 1 TO 19: READ n(i): NEXT i
    GOSUB Dump
    GOSUB Sort
    GOSUB Dump
    FOR i = 1 TO 19: GOSUB SwapEnds: NEXT i
    GOSUB Dump
    GOSUB Sort
    GOSUB Dump
    FOR i = 1 TO 19
      IF n(i) < 450 THEN n(i) = n(i) + 100 ELSE n(i) = n(i) + 150
    NEXT i
    GOSUB Dump
    FOR i = 1 TO 19: GOSUB SwapEnds: NEXT i
    GOSUB Dump
    GOSUB Sort
    GOSUB Dump
    FOR i = 1 TO 19: n(i) = n(i) - 113: NEXT i
    GOSUB Dump
    FOR i = 1 TO 19
      DO UNTIL n(i) < 28: n(i) = n(i) - 28: LOOP
    NEXT i
    GOSUB Dump
    FOR i = 1 TO 19
      IF n(i) > 25 THEN n(i) = n(i) + 6 ELSE n(i) = n(i) + 65
    NEXT i
    FOR i = 1 TO 19: PRINT CHR$(n(i)); : NEXT i: PRINT
    SYSTEM
    
    SwapEnds:
    IF n(i) < 100 THEN RETURN
    w$ = LTRIM$(RTRIM$(STR$(n(i))))
    w$ = RIGHT$(w$, 1) + MID$(w$, 2, 1) + LEFT$(w$, 1)
    n(i) = VAL(w$)
    RETURN
    
    Sort:
    FOR i = 1 TO 18
      FOR j = i + 1 TO 19
        IF n(i) > n(j) THEN SWAP n(i), n(j)
      NEXT j
    NEXT i
    RETURN
    
    Dump:
    PRINT
    FOR i = 1 TO 19: PRINT n(i); : NEXT i: PRINT
    RETURN

  7. #7

  8. #8

  9. #9
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    Washington DC
    Posts
    314

    Re: Need Subroutine Help

    Sorry you think so.

    I intended it as a hint for someone who is confused on the problem, not on VB. It shows three good hints for SUB's. Naturally, I would not provide VB code to do that. (Homework, I presume).

    But opine as you will.

    Mac

  10. #10
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,121

    Re: Need Subroutine Help

    Well the point is that while that may be a good example of QBasic coding (I have no idea), someone who is trying to learn VB might think that GoSubs and jamming multiple lines of code into one line are good ideas to be emulated. Also if someone wants examples of Subs and Functions there are of course examples by the thousands in this forum.

  11. #11
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Need Subroutine Help

    Quote Originally Posted by InsanePyro
    I know that I just don't know/understand how to do each thing that I need to do...
    It's also meant as an exercise in thinking (analysis) not just coding. If you went through the list of activities you'd note that an array is the common data structure (all activities relate to manipulating an array), and printing the array data is a repetitive process... hence printing array data is a candidate for a called procedure. There are other repetitive activities, exert some effort on your part since its for your own good.

  12. #12
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,135

    Re: Need Subroutine Help

    Quote Originally Posted by Mr.Mac
    ... It shows three good hints for SUB's...
    What QBasic calls "sub" in Visual Basic is called "label" so it's not exactly subroutine anymore.
    I wish they had elliminated the GoSub earlier in the game (they could've done it as of VB4 imho).

  13. #13

    Thread Starter
    Lively Member InsanePyro's Avatar
    Join Date
    Nov 2007
    Posts
    76

    Re: Need Subroutine Help

    Yeah...uh GoSubs don't really help me...still need help...

    I guess what would be a nice start is how to revese numbers....

    I.E make 123 into 321...

  14. #14

  15. #15
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    Washington DC
    Posts
    314

    Re: Need Subroutine Help

    Quote Originally Posted by RhinoBull
    What QBasic calls "sub" in Visual Basic is called "label" so it's not exactly subroutine anymore.
    Gosh, it's hard to communicate.

    I DID NOT INTEND TO IMPLY THAT THE OP SHOULD CODE GOSUB-TYPE SUBROUTINES. I PRESUMED HE WOULD USE THE SUB.

    I wanted to show how subroutines (no matter how written) could be used to shorten this assignment.

    Please don't waste any more replies explaining that QBasic code is not good VB code. I have been in the programming profession since 1958 and was a pioneer in modular programming. I have used VB since it came out. I know QBasic is not VB.

    I now see I should have posted pseudo-code since using QBasic as a form of pseudo-code is well beyond the live-in-box mentally here.

    Mac

  16. #16
    Frenzied Member
    Join Date
    Oct 2003
    Posts
    1,301

    Re: Need Subroutine Help

    To stick with the need to use subroutines:
    vb Code:
    1. Function ReverseNumber(Number As Long) As Long
    2.     'your code here
    3.     ReverseNumber = the_outcome_of_the_code
    4. End Function
    You can then call it like:
    vb Code:
    1. Dim NumA As Long
    2. Dim NumB As Long
    3. Dim NumC As Long
    4.     NumA = 123
    5.     NumB = ReverseNumber(NumA)
    6.     NumC = ReverseNumber(NumB)
    7.     Print NumA
    8.     Print NumB
    9.     Print NumC
    Can you see how subroutines are used and how their use will make coding easier?

  17. #17
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,566

    Re: Need Subroutine Help

    Quote Originally Posted by Mr.Mac
    Gosh, it's hard to communicate.

    I DID NOT INTEND TO IMPLY THAT THE OP SHOULD CODE GOSUB-TYPE SUBROUTINES. I PRESUMED HE WOULD USE THE SUB.

    I wanted to show how subroutines (no matter how written) could be used to shorten this assignment.

    Please don't waste any more replies explaining that QBasic code is not good VB code. I have been in the programming profession since 1958 and was a pioneer in modular programming. I have used VB since it came out. I know QBasic is not VB.

    I now see I should have posted pseudo-code since using QBasic as a form of pseudo-code is well beyond the live-in-box mentally here.

    Mac
    QBasic would have been fine, IF you had at least provided some explanation of WHY you were posting it. But you didn't. You simply said "Here"... and posted code, without any further explanation.... When posting code like that, unless the intended recipient also knows that language.... it's generally as cryptic as what they are trying to learn. It's like posting on a foreign language forum "I don't understand this German phrase"... and some one posts "Here's some french phrases, hope it helps".....That's all. It's not like the effort wasn't appreciated, it's just that w/o the explanation, it might as well have been greek.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  18. #18
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,135

    Re: Need Subroutine Help

    Quote Originally Posted by Mr.Mac
    Gosh, it's hard to communicate...
    It isn't Mac. You're still missing the main point - teaching someone to program in modern language using very old mentality (or technic/approach if you will) may (and usually does) develop serious problems later in someone's carreer.
    It could be extremly difficult to make necessary adjustments to your programming style and ultimately way of thinking.
    Therefore QBasic code samples shall not be posted in Visual Basic section despite they both based on BASIC language.

    ps, I've met people who just kept writing VB2/3 code in VB6 and I'm sure they will continue doing the same (when syntax is supported) in VB.Net world.
    And you know why? I think because someone's gave them wrong concept initially - as simple as that.

  19. #19

  20. #20

    Thread Starter
    Lively Member InsanePyro's Avatar
    Join Date
    Nov 2007
    Posts
    76

    Re: Need Subroutine Help

    The strReverse thing doesnt work Rhino....
    Last edited by InsanePyro; Jan 21st, 2008 at 04:05 PM.

  21. #21

    Thread Starter
    Lively Member InsanePyro's Avatar
    Join Date
    Nov 2007
    Posts
    76

    Re: Need Subroutine Help

    Here is my code so far

    Code:
    Private Sub cmdexecute_Click()
    Dim FilePath As String
    Dim strTemp As String
    Dim FF As Long
    Dim strReverse As String
    Dim intCode(30) As Integer
    Dim intX As Integer
    Dim intSize As Integer
    
    FF = FreeFile()
    FilePath = App.Path & "\numbers.txt"
    
    
    Open FilePath For Input As #FF
    Do While Not EOF(FF)
    intX = intX + 1
    Input #FF, intCode(intX)
    
    
    lstOne.AddItem intCode(intX) 'inital numbers
    
    Loop
    
    intSize = intX
    Close #FF
    End Sub

  22. #22
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,121

    Re: Need Subroutine Help

    That's a decent start. You have a small problem though. I assume you are not doing Option Base 1 so by default the first entry in intCode is 0 and not 1, so doing this

    intX = intX + 1
    Input #FF, intCode(intX)

    puts the first number in intCode(1) rather than intCode(0). Reversing the two lines will solve that.

    Now what specifically are you confused about?

  23. #23

    Thread Starter
    Lively Member InsanePyro's Avatar
    Join Date
    Nov 2007
    Posts
    76

    Re: Need Subroutine Help

    Well I dunno how to call the subroutines as well as how to call them in the correct order...

  24. #24
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,121

    Re: Need Subroutine Help

    Quote Originally Posted by InsanePyro
    Well I dunno how to call the subroutines as well as how to call them in the correct order...
    A subroutine can be called by simply using it's name, so let's say you have a Sub called Calculate, you would call it this way.

    Code:
    'some code
    Calculate
    'more code
    If Calculate was a function (function unlike subs, return something) that looked like this

    Code:
    Private Function Calculate(intVal As Integer) As Integer
       Calculate = intVal + 10
    End Function
    You would call it with something like this

    MyNewVal = Calculate(5)

    and after you did that MyNewValue would equal 15.

  25. #25

    Thread Starter
    Lively Member InsanePyro's Avatar
    Join Date
    Nov 2007
    Posts
    76

    Re: Need Subroutine Help

    Here is my subroutine so far
    Code:
    Sub Order(intCode() As Integer, intSize As Integer)
    intN = intSize
    Do While intN > 1
    For intI = 1 To intN - 1
    intJ = intI + 1
    Call Order(intCode(intI), intCode(ingJ))
    If intOne > intTwo Then
    intemp = intOne
    intOne = intTwo
    intTwo = intTemp
    End If
    Next intI
    intN = intN - 1
    End Loop
    End Sub
    Whats wrong with it?
    Last edited by InsanePyro; Jan 21st, 2008 at 08:16 PM.

  26. #26
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,121

    Re: Need Subroutine Help

    To start with, a small point - you should indent. It's much easier that way to see what's going on.

    vb Code:
    1. Sub Order(intOne As Integer, intTwo As Integer)
    2. intN = intSize
    3. Do While intN > 1
    4.     For intI = 1 To intN - 1
    5.         intJ = intI + 1
    6.         Call Order(intCode(intI), intCode(ingJ))
    7.         If intOne > intTwo Then
    8.             intemp = intOne
    9.             intOne = intTwo
    10.             intTwo = intTemp
    11.        End If
    12.     Next intI
    13.     intN = intN - 1
    14. End Loop
    15. End Sub
    Other questions/problems:
    • Where is intSize defined?
    • What is the purpose of the sub?
    • Line 14 is incorrect - you don't need the "End"

  27. #27

    Thread Starter
    Lively Member InsanePyro's Avatar
    Join Date
    Nov 2007
    Posts
    76

    Re: Need Subroutine Help

    Alright it is a bubble sort
    intSize is defined....in the wrong spot...i'll fix that...
    Line 14 is fixed...

  28. #28

  29. #29

    Thread Starter
    Lively Member InsanePyro's Avatar
    Join Date
    Nov 2007
    Posts
    76

    Re: Need Subroutine Help

    Yes I am trying to rearrange numbers in a list box and have it print the sorted numbers in a second list box....and my teacher said to use sub routines and have them all call in order when a command button is pressed

  30. #30

  31. #31

    Thread Starter
    Lively Member InsanePyro's Avatar
    Join Date
    Nov 2007
    Posts
    76

    Re: Need Subroutine Help

    how do i get the bubble sort to read my array of numbers and then print the result in the list box?

  32. #32

    Thread Starter
    Lively Member InsanePyro's Avatar
    Join Date
    Nov 2007
    Posts
    76

    Re: Need Subroutine Help

    my program as it stands right now

    Code:
    Option Explicit
    Dim intSize As Integer
    Private Sub cmdExit_Click()
    End
    End Sub
    
    Private Sub cmdexecute_Click()
    Dim FilePath As String
    Dim strTemp As String
    Dim FF As Long
    Dim strReverse As String
    Dim intCode(30) As Integer
    Dim intX As Integer
    Dim intSize As Integer
    Dim intI As Integer
    Dim intJ As Integer
    Dim intN As Integer
    Dim intTemp As Integer
    
    FF = FreeFile()
    FilePath = App.Path & "\numbers.txt"
    
    
    Open FilePath For Input As #FF
    Do While Not EOF(FF)
    intX = intX + 1
    Input #FF, intCode(intX)
    
    
    lstOne.AddItem intCode(intX) 'inital numbers
    
    Loop
    
    Close #FF
    End Sub
    
    Sub Order(intCode() As Integer, intSize As Integer)
    intN = intSize
    Do While intN > 1
    For intI = 1 To intN - 1
    intJ = intI + 1
    Call Order(intCode(intI), intCode(ingJ))
    If intOne > intTwo Then
    intemp = intOne
    intOne = intTwo
    intTwo = intTemp
    End If
    Next intI
    intN = intN - 1
    Loop
    End Sub

  33. #33
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Need Subroutine Help

    Indent your code properly.
    Use dynamic arrays.
    Use proper scope for variables (use procedure level variables, don't place everything as module level or public).
    Use parameter passing (related to proper scoping).

    Bubble sort code:
    Code:
    Public Sub Sort(ByRef arrRef() As Integer, Optional Ascending As Boolean=True)
    Dim i As Long
    Dim j As Long
    Dim intTmp As Integer
    
       If Ascending Then
          For i = 0 To Ubound(arrRef) - 1
             For j = 0 To Ubuond(arrRef) - 1
                If arrRef(j) > arrRef(j + 1) Then
                   'swap array elements using intTmp
                End If  
             Next
          Next
       Else
          For i = 0 To Ubound(arrRef) - 1
             For j = 0 To Ubound(arrRef) - 1
                If arrRef(j) < arrRef(j+1) Then
                   'swap array elements using intTmp
                End If
             Next
          Next
       End If
    End Sub
    To sort ascending Call Sort(source_array)
    To sort descending Call Sort(source_array, False)

  34. #34

    Thread Starter
    Lively Member InsanePyro's Avatar
    Join Date
    Nov 2007
    Posts
    76

    Re: Need Subroutine Help

    Alright but how do I get it to affect the numbers? It doesn't affect my array...

  35. #35
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Need Subroutine Help

    Probably because you just copied and pasted instead of reading through the code and understanding what it does. If you had done so then you would have realized you needed to update comment 'swap array elements using intTmp' accordingly with 3 lines of code.

    Code:
    Option Explicit
    Private arrSrc() As Integer
    
    Private Sub Form_Load()
    Dim i As Long
    
    'Create test data instead of loading from text file
       Randomize
       ReDim arrSrc(Int(Rnd * 10) + 5)
       
       For i = 0 To UBound(arrSrc)
          arrSrc(i) = Int(Rnd * 900) + 100
       Next
    'Finished creating test data
       
       Debug.Print "Test data"
       Call DebugPrint(arrSrc)
       
       Debug.Print "Sort ascending"
       Call Sort(arrSrc)
       Call DebugPrint(arrSrc)
       
       Debug.Print "Sort descending"
       Call Sort(arrSrc, False)
       Call DebugPrint(arrSrc)
       
       Erase arrSrc
       Unload Me
    End Sub
    
    Public Sub DebugPrint(ByRef arrRef() As Integer)
    Dim i As Long
    
       For i = 0 To UBound(arrRef)
          Debug.Print arrRef(i)
       Next
    End Sub
    
    Public Sub Sort(ByRef arrRef() As Integer, Optional Ascending As Boolean = True)
    Dim i As Long
    Dim j As Long
    Dim intTmp As Integer
    
       If Ascending Then
          For i = 0 To UBound(arrRef) - 1
             For j = 0 To UBound(arrRef) - 1
                If arrRef(j) > arrRef(j + 1) Then
                   intTmp = arrRef(j)
                   arrRef(j) = arrRef(j + 1)
                   arrRef(j + 1) = intTmp
                End If
             Next
          Next
       Else
          For i = 0 To UBound(arrRef) - 1
             For j = 0 To UBound(arrRef) - 1
                If arrRef(j) < arrRef(j + 1) Then
                   intTmp = arrRef(j)
                   arrRef(j) = arrRef(j + 1)
                   arrRef(j + 1) = intTmp
                End If
             Next
          Next
       End If
      
    End Sub

  36. #36

    Thread Starter
    Lively Member InsanePyro's Avatar
    Join Date
    Nov 2007
    Posts
    76

    Re: Need Subroutine Help

    Ok maybe I'm just stupid or something but I don't get this.... need stuff that applies directly to my program using my variables....this is due tomorrow and is my semester final....i really need help on this....badly....please...

  37. #37
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    Washington DC
    Posts
    314

    Re: Need Subroutine Help

    Quote Originally Posted by leinad31
    Bubble sort code:
    Isn't that the "selection sort" you presented?

    I remember the bubble sort as being something like this

    1) Record that no item was swapped
    2) Make a pass at array swapping any two adjacent elements if required and record that fact
    3) Loop as long as an item was swapped.

    The chief benefit of the bubble sort is for sorting files that are already sorted or nearly so. Somehow, the school teachers have used the term "bubble sort" to mean the selection sort or similar sorts. Hardly anyone today actually codes a bubble sort.

    Mac

  38. #38

    Thread Starter
    Lively Member InsanePyro's Avatar
    Join Date
    Nov 2007
    Posts
    76

    Re: Need Subroutine Help

    I know it's outdated as hell...the damn worksheet is from 1987

  39. #39
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Need Subroutine Help

    Quote Originally Posted by InsanePyro
    Ok maybe I'm just stupid or something but I don't get this.... need stuff that applies directly to my program using my variables....this is due tomorrow and is my semester final....i really need help on this....badly....please...
    I already provided the template. You only have to change algorithm for loading values from text file into an array (incorporate what you have), change algorithm for printing out array values (output to wherever you need it to rather than immediate window), and add all other procedures for manipulating the array (such as reversal of digits and addition/subtraction). You have to try... the whole experience (analysis, design, debugging, coding, etc) is more important in the long run.

  40. #40
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Need Subroutine Help

    Quote Originally Posted by Mr.Mac
    Isn't that the "selection sort" you presented?

    I remember the bubble sort as being something like this

    1) Record that no item was swapped
    2) Make a pass at array swapping any two adjacent elements if required and record that fact
    3) Loop as long as an item was swapped.

    The chief benefit of the bubble sort is for sorting files that are already sorted or nearly so. Somehow, the school teachers have used the term "bubble sort" to mean the selection sort or similar sorts. Hardly anyone today actually codes a bubble sort.

    Mac
    Plenty of material on the internet, please research on what bubble sort and selection sort is. What you described is a modification/optimization of basic bubble sort to reduce iterations (if already sorted, skip rest of passes/comparisons).

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width