Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 2010 > Visual Basic 2010 General Discussion
|
Visual Basic 2010 General Discussion For any discussions about Visual Basic 2010 topics which aren't related to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2010 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 28th, 2010, 03:31 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Show the code you wrote. Surely just a simple error if the same basic code had worked for numbers.
The Following User Says Thank You to Old Pedant For This Useful Post:
SamC (October 28th, 2010)
 
Old October 28th, 2010, 07:11 PM
Authorized User
 
Join Date: Oct 2010
Posts: 29
Thanks: 23
Thanked 0 Times in 0 Posts
Send a message via MSN to SamC
Smile

I thought it wasn't working because you couldn't compare letters, because it always threw an error at this line which compared them. Below is the code for the program I wrote, I've deleted my attempt at a bubble sort as it was an embarrassing mess.

Code:
Module Module1

    Sub Main()

        '// Declare Variables 
        Dim Name(1) As String
        Dim Input As String = " "
        Dim Temp As String = " "
        Dim n As Integer = 0

        '// Populate Array with User Input
        Do Until Input = ""
            Console.Write("Enter Name: ")
            Input = Console.ReadLine()
            If Input <> "" Then
                ReDim Preserve Name(n)
                Name(n) = Input
                n += 1
            End If
            Console.Clear()
        Loop

        '// Sort Names

        '// Display Names
        For Each Item In Name
            Console.WriteLine(Item)
        Next

        '// Pause the program to keep the console open
        Console.ReadLine()

    End Sub

End Module
If you could write the sorting algorithm for me so I can see how you're supposed to do it, then I would be eternally grateful!
__________________
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction."
- Albert Einstein
 
Old October 28th, 2010, 07:18 PM
Authorized User
 
Join Date: Oct 2010
Posts: 29
Thanks: 23
Thanked 0 Times in 0 Posts
Send a message via MSN to SamC
Default

I would also like to know how to sort a local array in a separate sub routine, would that be be by reference or value? Or do you have to use something else entirely?
__________________
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction."
- Albert Einstein
 
Old October 28th, 2010, 07:38 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

If you want to sort it in place, pass it ByRef.
The Following User Says Thank You to Old Pedant For This Useful Post:
SamC (October 30th, 2010)
 
Old October 28th, 2010, 07:50 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Quote:
I thought it wasn't working because you couldn't compare letters, because it always threw an error at this line which compared them. Below is the code for the program I wrote, I've deleted my attempt at a bubble sort as it was an embarrassing mess.
Compare "letters"?? As opposed to comparing entire strings? Why?

Okay, so don't show me the string bubble sort you wrote. Show the numbers bubble sort. If it worked, it can't be *THAT* bad.

Not so minor point:
Code:
Dim Name(1) As String
That declares an array of *TWO* strings! Arrays always start at zero, and in VB the number in the DIM statement specifies the UPPER BOUND of the array, not the number of elements.

Now, that's not a huge deal in this code, as you later do
Code:
                ReDim Preserve Name(n)
and so with n=0 you indeed redim it back to one element.

For an interactive input like this, what you did is okay. But Redim Preserve is an expensive operation that you'd want to eliminate or at least only use as little as possible.

This code would actually be much better (though again it doesn't matter at all when you are having to wait on a super slow human to input something...that's aeons of time to a computer).
Code:
        '// Declare Variables
        Dim Name(10000) As String
        Dim Input As String
        Dim Temp As String
        Dim n As Integer = -1

        ' Populate Array with User Input
        Do While True
            Console.Write("Enter Name: ")
            Input = Console.ReadLine()
            If Input = "" Then Exit Do
            n += 1 
            Name(n) = Input
            Console.Clear()
        Loop
        ReDim Preserve Name(n)
The Following User Says Thank You to Old Pedant For This Useful Post:
SamC (October 30th, 2010)
 
Old October 30th, 2010, 10:35 AM
Authorized User
 
Join Date: Oct 2010
Posts: 29
Thanks: 23
Thanked 0 Times in 0 Posts
Send a message via MSN to SamC
Default

Thanks, I was aware of this, the reason I set it at 1 was because I intended to force the user to enter at least two names at the start, but I have made your suggested amendments to the source code. Secondly by 'letters' I just meant strings comprised entirely of letters.

Unfortunately I made the integer bubble sort after failing to make the string sort in the same file, then I tried to modify it back into sorting integers, it became a bloated mess so I deleted it in frustration. I'm going to work on it again, needless to say it probably won't work.
__________________
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction."
- Albert Einstein
 
Old October 30th, 2010, 11:32 AM
Authorized User
 
Join Date: Oct 2010
Posts: 29
Thanks: 23
Thanked 0 Times in 0 Posts
Send a message via MSN to SamC
Default

OK I got it to work this time, although I changed my code back to my own, because my teacher told me that the user had to be able to enter an infinite number of names, and my understanding of the code you proposed is that the program would crash if the user tried to enter a 1002 names.

Here's my code:

Code:
Module Module1

    Sub Main()

        '// Declare Variables 
        Dim Name(0) As String
        Dim Input As String = " "
        Dim Temp As String = " "
        Dim n As Integer = 0

        '// Change Console Text Colour
        Console.ForegroundColor = ConsoleColor.Green

        '// Populate Array with User Input
        Do Until Input = ""
            Console.Write("Enter Name: ")
            Input = Console.ReadLine()
            If Input <> "" Then
                ReDim Preserve Name(n)
                Name(n) = Input
                n += 1
            End If
            Console.Clear()
        Loop

        '// Sort Names
        For Outer = n - 1 To 0 Step -1
            For Inner = 0 To Outer - 1
                If Name(Inner) > Name(Inner + 1) Then
                    Temp = Name(Inner)
                    Name(Inner) = Name(Inner + 1)
                    Name(Inner + 1) = Temp
                End If
            Next
        Next

        '// Display Names
        For Each Item In Name
            Console.WriteLine(Item)
        Next

        '// Pause the program to keep the console open
        Console.ReadLine()

    End Sub

End Module
I used an example of the bubble sort quoted on another website in pseudo-code, now looking at it I'm sure it would be more efficient if I made it end upon realising that the list is already ordered, although I'm not really sure how to implement this.

Also how would I turn it into a function that can sort arrays which are local to another sub, by referring to it in that sub? If that makes sense!

PS: thank you very much for your help, it's been invaluable to me - I'll go back and click the 'thank' button now on your posts.
__________________
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction."
- Albert Einstein
 
Old October 31st, 2010, 12:35 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Turn it into a function:
Code:
Function MySort( ByRef ary AS Array ) ' or AS Array Of String when you get into generics
    Dim Outer, Inner As Integer ' always declare function local variables
    Dim Temp As String
    For Outer = UBound(ary) - 1 To 0 Step -1
        For Inner = 0 To Outer - 1
             If ary(Inner) > ary(Inner + 1) Then
                    Temp = ary(Inner)
                    ary(Inner) = ary(Inner + 1)
                    ary(Inner + 1) = Temp
             End If
        Next
    Next 
End Function
The Following User Says Thank You to Old Pedant For This Useful Post:
SamC (October 31st, 2010)
 
Old October 31st, 2010, 12:41 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Allow any number of array entries, more efficiently:
Code:
        Dim Name(1000) As String
        Dim n As Integer = -1

        ' Populate Array with User Input
        Do While True
            Console.Write("Enter Name: ")
            Input = Console.ReadLine()
            If Input = "" Then Exit Do
            n += 1
            If n > UBound(Name) Then ReDim Preserve Name(n+500)
            Name(n) = Input
            Console.Clear()
        Loop
        Redim Preserve Name(n)
The Following User Says Thank You to Old Pedant For This Useful Post:
SamC (October 31st, 2010)
 
Old November 1st, 2010, 06:50 PM
Authorized User
 
Join Date: Oct 2010
Posts: 29
Thanks: 23
Thanked 0 Times in 0 Posts
Send a message via MSN to SamC
Default

I believe I may have found the solution to my Pi Problems!

http://en.wikipedia.org/wiki/Bailey%...thm_for_.CF.80

I don't really understand how you interpret the algorithm though, do you think you could explain it to me? Also how easy do you think it would be to implement in Visual Basic.NET?
__________________
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction."
- Albert Einstein





Similar Threads
Thread Thread Starter Forum Replies Last Post
Number with 2 decimal places only pallone SQL Server 2000 4 July 3rd, 2007 07:29 AM
Display Decimal Places rsm42 ASP.NET 1.0 and 1.1 Basics 3 January 7th, 2007 10:46 AM
Round to 2 decimal places adman JSP Basics 0 June 30th, 2005 07:24 AM
Two decimal places only pallone Javascript How-To 5 February 2nd, 2005 12:35 AM
output only 2 decimal places Toka1 Javascript How-To 2 February 20th, 2004 11:44 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.