Daniel Gmail
Daniel Gmail

Reputation: 11

Im having problems figuring out how to print in an array

Not sure how to output the numbers, once they are in ascending order.

This is the task in the pseudocode that I am trying to move into VB.

 Dim a() = {2,3,1,4}
 Dim swapped = False
 Output the values of a()
 Do Swapped 🡨 False

For I = 1 to end of the array Compare a(i-1) with a(i) if they are not in ascending order pass them to swapped (from task 1) Swapped(a(i-1),a(i)) assign the returned value to the variable swapped. While swapped = True Output the values of a()

Dim num = New Integer() {2, 3, 1, 4}
Dim swapped As Boolean = False

While swapped = False
    For i = 1 To 4
        If num(i - 1) > num(i) Then
            temp = num(i)
            num(i) = num(i - 1)
            num(i - 1) = temp
            swapped = True
        Else
            swapped = False
        End If
    Next
End While
While swapped = True
    Console.WriteLine(num)
End While
Console.ReadLine()

Upvotes: 0

Views: 46

Answers (3)

user11982798
user11982798

Reputation: 1908

If you don't want to use vb net generic array order method, and you prefer use the swap concept, here you can try:

Private Sub TestOrderSwap()
    Dim num = New Integer() {9, 7, 0, 11, 12, 10, 6, 2, 3, 1, 4}
    If OrderSwap(num, "ASC") Then
        For a = 0 To num.Length - 1
            Debug.Print(num(a))
        Next
    End If
    If OrderSwap(num, "DESC") Then
        For a = 0 To num.Length - 1
            Debug.Print(num(a))
        Next
    End If
End Sub

Private Function OrderSwap(ByRef myArray As Integer(), OrderType As String) As Boolean
    Dim swp As Integer = 0
    Dim swpFlg As Boolean = False
    For a = 1 To myArray.Length - 1
        swp = myArray(a)
        For b = 0 To a - 1
            If (myArray(b) > swp And OrderType = "ASC") Or (myArray(b) < swp And OrderType = "DESC") Then
                For c = a - 1 To b Step -1
                    myArray(c + 1) = myArray(c)
                Next
                myArray(b) = swp
                swpFlg = True
                Exit For
            End If
        Next
    Next
    Return swpFlg
End Function

Upvotes: 0

Mary
Mary

Reputation: 15091

Although, @laancelot showed you how to output the array, I thought I would show you a simple way to sort an array.

Private Sub OrderArray()
    Dim num As Integer() = {2, 3, 1, 4}
    Array.Sort(num)
    Console.WriteLine("Ascending")
    For Each i In num
        Console.WriteLine(i.ToString)
    Next
    'If you want it the other way around
    Array.Reverse(num)
    Console.WriteLine("Descending")
    For Each i In num
        Console.WriteLine(i.ToString)
    Next
    Console.ReadLine()
End Sub

Upvotes: 1

laancelot
laancelot

Reputation: 3207

Here you go:

For Each n As Integer In num
    Console.WriteLine(n.ToString)
Next

Btw there's a problem with your swapping algorithm: if they are already in the right you will enter an infinite loop, and if there's a swap on the last number you'll exit the loop even if they are not in the right order. If it puzzles you let me a comment and we'll sort this out.

Upvotes: 1

Related Questions