-
Jan 20th, 2008, 05:29 PM
#1
Thread Starter
Lively Member
-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.
-
Jan 20th, 2008, 05:46 PM
#2
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.
-
Jan 20th, 2008, 05:49 PM
#3
Thread Starter
Lively Member
Re: Need Subroutine Help
I know that I just don't know/understand how to do each thing that I need to do...
-
Jan 20th, 2008, 06:52 PM
#4
Re: Need Subroutine Help
So, maybe it's best to ask one thing at the time?
-
Jan 20th, 2008, 06:55 PM
#5
Re: Need Subroutine Help
Originally Posted by RhinoBull
So, maybe it's best to ask one thing at the time?
But when you do, show us some of your code.
-
Jan 20th, 2008, 08:07 PM
#6
Hyperactive Member
Re: Need Subroutine Help
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
-
Jan 20th, 2008, 08:10 PM
#7
Re: Need Subroutine Help
IMO that's a very poor example for someone trying to learn VB.
-
Jan 20th, 2008, 08:30 PM
#8
Re: Need Subroutine Help
Originally Posted by MartinLiss
IMO that's a very poor example for someone trying to learn VB.
I indeed agree - example is horrible. Sorry Mr. Mac.
-
Jan 20th, 2008, 11:08 PM
#9
Hyperactive Member
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
-
Jan 20th, 2008, 11:17 PM
#10
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.
-
Jan 21st, 2008, 01:19 AM
#11
Re: Need Subroutine Help
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.
-
Jan 21st, 2008, 07:55 AM
#12
Re: Need Subroutine Help
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).
-
Jan 21st, 2008, 09:23 AM
#13
Thread Starter
Lively Member
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...
-
Jan 21st, 2008, 09:43 AM
#14
Re: Need Subroutine Help
Originally Posted by InsanePyro
I guess what would be a nice start is how to revese numbers....
I.E make 123 into 321...
But I did show you and your friend how to do that using StrReverse, didn't I?
-
Jan 21st, 2008, 09:48 AM
#15
Hyperactive Member
Re: Need Subroutine Help
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
-
Jan 21st, 2008, 10:20 AM
#16
Frenzied Member
Re: Need Subroutine Help
To stick with the need to use subroutines:
vb Code:
Function ReverseNumber(Number As Long) As Long
'your code here
ReverseNumber = the_outcome_of_the_code
End Function
You can then call it like:
vb Code:
Dim NumA As Long
Dim NumB As Long
Dim NumC As Long
NumA = 123
NumB = ReverseNumber(NumA)
NumC = ReverseNumber(NumB)
Print NumA
Print NumB
Print NumC
Can you see how subroutines are used and how their use will make coding easier?
-
Jan 21st, 2008, 10:52 AM
#17
Re: Need Subroutine Help
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
-
Jan 21st, 2008, 11:08 AM
#18
Re: Need Subroutine Help
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.
Last edited by RhinoBull; Jan 21st, 2008 at 11:16 AM.
-
Jan 21st, 2008, 11:12 AM
#19
Re: Need Subroutine Help
Okay I think we've all made our points, so unless the original poster cares to show some of his code and asks specific questions I don't think there's any more to be gained by continuing this discussion.
-
Jan 21st, 2008, 04:02 PM
#20
Thread Starter
Lively Member
Re: Need Subroutine Help
The strReverse thing doesnt work Rhino....
Last edited by InsanePyro; Jan 21st, 2008 at 04:05 PM.
-
Jan 21st, 2008, 04:18 PM
#21
Thread Starter
Lively Member
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
-
Jan 21st, 2008, 04:39 PM
#22
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?
-
Jan 21st, 2008, 07:49 PM
#23
Thread Starter
Lively Member
Re: Need Subroutine Help
Well I dunno how to call the subroutines as well as how to call them in the correct order...
-
Jan 21st, 2008, 07:56 PM
#24
Re: Need Subroutine Help
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.
-
Jan 21st, 2008, 08:01 PM
#25
Thread Starter
Lively Member
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.
-
Jan 21st, 2008, 08:20 PM
#26
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:
Sub Order(intOne As Integer, intTwo 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
Other questions/problems:
- Where is intSize defined?
- What is the purpose of the sub?
- Line 14 is incorrect - you don't need the "End"
-
Jan 21st, 2008, 08:22 PM
#27
Thread Starter
Lively Member
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...
-
Jan 21st, 2008, 08:55 PM
#28
Re: Need Subroutine Help
Do you want something outside that sub to change? If so the you need a function and not a sub.
-
Jan 21st, 2008, 08:57 PM
#29
Thread Starter
Lively Member
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
-
Jan 21st, 2008, 09:02 PM
#30
Re: Need Subroutine Help
Well everything doesn't have to be in subroutines. And as for bubble sorts, do a search in this forum and you'll find lots of examples.
-
Jan 21st, 2008, 09:03 PM
#31
Thread Starter
Lively Member
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?
-
Jan 21st, 2008, 09:05 PM
#32
Thread Starter
Lively Member
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
-
Jan 21st, 2008, 09:26 PM
#33
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)
-
Jan 21st, 2008, 09:28 PM
#34
Thread Starter
Lively Member
Re: Need Subroutine Help
Alright but how do I get it to affect the numbers? It doesn't affect my array...
-
Jan 21st, 2008, 09:46 PM
#35
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
-
Jan 21st, 2008, 10:26 PM
#36
Thread Starter
Lively Member
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...
-
Jan 21st, 2008, 10:26 PM
#37
Hyperactive Member
Re: Need Subroutine Help
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
-
Jan 21st, 2008, 10:32 PM
#38
Thread Starter
Lively Member
Re: Need Subroutine Help
I know it's outdated as hell...the damn worksheet is from 1987
-
Jan 21st, 2008, 10:34 PM
#39
Re: Need Subroutine Help
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.
-
Jan 21st, 2008, 10:37 PM
#40
Re: Need Subroutine Help
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).
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
|