Monday, November 15, 2010

Monday 11/15/2010


Module Module1

Sub Main()
Dim I As Integer
Dim X() As Integer = {5, 2, 7, 4, 6, 1, 3, 9, 8}

Console.WriteLine("Unsorted array:")

For I = 0 To X.GetUpperBound(0)
Console.Write("{0} ", X(I))
Next I

insertion_sort(X, X.GetUpperBound(0))

Console.WriteLine("{0} Sprrted Array: ", Chr(10))

For I = 0 To X.GetUpperBound(0)
Console.Write("{0} ", X(I))
Next I
Console.ReadLine()


End Sub

Sub insertion_sort(ByRef x() As Integer, ByVal length As Integer)
Dim key As Integer, i As Integer, j As Integer

For j = 1 To length
key = x(j)
i = j - 1

Do While i >= 0
If x(i) > key Then
x(i + 1) = x(i)
i -= 1
Else
Exit Do
End If
Loop
x(i + 1) = key
Next j
End Sub


Here is a variation on a While loop for figuring out the powers of a number

Sub main()
Dim product As Integer

product = Console.ReadLine()
Dim multiplier As Integer = product
Dim original As Integer = product



While product < 1000
Console.Write("{0} ", product)
product = product * multiplier

End While
Console.WriteLine("The smallest power of " & original & " greater than 1000 is {0}", product)
Console.ReadLine()
End Sub

Here is a while loop that draws a square, kind of


Sub main()
Dim side As Integer
Dim row As Integer
Dim column As Integer

side = 12

If side <= 20 Then
While row <= side
column = 1
While column <= side
Console.Write("X")
column += 1

End While
Console.WriteLine()

row += 1
End While
End If
Console.ReadLine()
End Sub


I wrote another quick program of my own to make a square.

Sub main()
Dim iRow As Integer
Dim iColumn As Integer
Dim iWidth As Integer


iRow = Console.ReadLine()

For i = 1 To iRow
iColumn = 1
While iColumn <= iRow
Console.Write("X")
iColumn += 1
End While
Console.WriteLine()
Next

Console.ReadLine()
End Sub


Here we have another program for matching with a position in an array.


Dim iCounter As Integer = 0
Dim arrList(11) As String
Dim iMatch As Integer = -1
Dim sMatch As String

sMatch = Console.ReadLine()
sMatch = sMatch.ToUpper()


arrList(0) = "A"
arrList(1) = "B"
arrList(2) = "C"
arrList(3) = "D"
arrList(4) = "E"
arrList(5) = "F"
arrList(6) = "G"
arrList(7) = "H"
arrList(8) = "I"
arrList(9) = "J"
arrList(10) = "K"
arrList(11) = "L"


While iCounter <= 11 And iMatch = -1
If arrList(iCounter) Like sMatch Then
iMatch = iCounter
Else
iCounter = iCounter + 1
End If
End While

If iMatch <> -1 Then
System.Console.WriteLine("Matched with array" & iMatch)

End If
Console.ReadLine()

No comments: