'Note 'This Program Shows How To Sort The Array by Using Bubble Sorting
'Note 'This Program Shows How To Sort The Array by Using Bubble Sorting
Module modBubbleSort Sub Main() Dim output As String Dim rnd As New Random Dim arrNum As Integer() = New Integer(9) {} Console.WriteLine("Random generated array values") For i As Integer = 0 To arrNum.GetUpperBound(0) arrNum(i) = rnd.Next(1, 100) output &= "arrNum(" & i & ")=" & arrNum(i) & vbCrLf Next Console.WriteLine(output) Console.WriteLine("Sorted Array") BubbleSort(arrNum) output = "" For i As Integer = 0 To arrNum.GetUpperBound(0) output &= "arrNum(" & i & ")=" & arrNum(i) & vbCrLf Next Console.WriteLine(output) End Sub Sub BubbleSort(ByVal sortArray As Integer()) Dim pass, i As Integer For pass = 1 To sortArray.GetUpperBound(0) For i = 0 To sortArray.GetUpperBound(0) - 1 If sortArray(i) > sortArray(i + 1) Then Swap(sortArray, i) End If Next Next End Sub Sub Swap(ByVal swapArray As Integer(), ByVal first As Integer) Dim hold As Integer hold = swapArray(first) swapArray(first) = swapArray(first + 1) swapArray(first + 1) = hold End Sub End Module
'NOTE 'This program performs linear searching on arrays. Module modLinearSearch Sub main() Dim Dim Dim Dim Dim output key As ind As rnd As arrNum As String Integer Integer New Random As Integer() = New Integer(9) {}
Console.WriteLine("Random generated array values") For i As Integer = 0 To arrNum.GetUpperBound(0) arrNum(i) = rnd.Next(1, 100) output &= "arrNum(" & i & ")=" & arrNum(i) & vbCrLf Next Console.WriteLine(output) Console.Write("Enter the key value to search:") key = Console.ReadLine ind = LinearSearch(key, arrNum) If ind <> -1 Then Console.WriteLine("The index of the key value is {0}", ind) Else Console.WriteLine("Key not found.") End If End Sub Function LinearSearch(ByVal key As Integer, ByVal number As Integer()) As Integer Dim n As Integer For n = 0 To number.GetUpperBound(0) If number(n) = key Then Return n End If Next Return -1 End Function End Module
'NOTE 'This program shows how to declare arrays and how vb.net initialize the value implicity 'by default value of the data type used. Imports System.Windows.Forms Module modCreateArray Sub main() Dim output As String Dim i As Integer Dim array As Integer() array = New Integer(9) {} output &= "Subscript" & vbTab & "Value" & vbCrLf For i = 0 To array.GetUpperBound(0) output &= i & vbTab & array(i) & vbCrLf Next output &= vbCrLf & "The array contains " & array.Length & " elements." MessageBox.Show(output, "Array of Integer Values", MessageBoxButtons.OK, MessageBoxIcon.Information) End Sub End Module
Module modForEach Sub main() Dim gradeArray As Integer(,) = New Integer(,) {{77, 68, 86, 73}, {98, 87, 89, 81}, {70, 90, 86, 81}} Dim grade As Integer Dim lowGrade As Integer = 100 For Each grade In gradeArray If grade < lowGrade Then lowGrade = grade End If Next Console.WriteLine("The minimun grade is {0}", lowGrade) End Sub End Module
Imports System.Windows.Forms Module modHistogram Sub main() Dim output As String Dim i, j As Integer Dim array As Integer() = New Integer() {19, 3, 15, 7, 11, 9, 13, 5, 17, 1} output &= "Element" & vbTab & "Value" & vbTab & "Histogram" For i = 0 To array.GetUpperBound(0) output &= vbCrLf & i & vbTab & array(i) & vbTab For j = 1 To array(i) output &= "*" Next Next MessageBox.show(output, "Histogram Printing Program", MessageBoxButtons.OK, MessageBoxIcon.Information) End Sub End Module
'NOTE 'This program show how to declare more than one array in one statement. 'How to specify the size of the array by using expression. Imports System.Windows.Forms Module modInitArray Sub main() Dim output As String Dim i As Integer Dim array1, array2 As Integer() array1 = New Integer() {32, 27, 64, 18, 95, 14, 90, 70, 60, 37} array2 = New Integer(array1.GetUpperBound(0)) {} For i = 0 To array2.GetUpperBound(0) array2(i) = 2 + 2 * i Next output &= "Subscript" & vbTab & " Array1" & vbTab & "Array2" & vbCrLf For i = 0 To array1.GetUpperBound(0) output &= i & vbTab & array1(i) & vbTab & array2(i) & vbCrLf Next MessageBox.show(output, "Array of Integer Values", MessageBoxButtons.OK, MessageBoxIcon.Information) End Sub End Module
Imports System.Windows.Forms Module modJaggedArray Dim lastStudent, lastExam As Integer Dim output As String Sub main() Dim i As Integer 'jagged array with 3 rows of exam scores Dim gradeArray As Integer()() = New Integer(2)() {} 'allocate each row with 4 students grades gradeArray(0) = New Integer() {77, 68, 86, 73} gradeArray(1) = New Integer() {98, 87, 89, 81} gradeArray(2) = New Integer() {70, 90, 86, 81} 'upperbounds for array manipulation lastStudent = gradeArray.GetUpperBound(0) lastExam = gradeArray(0).GetUpperBound(0) output = "Students \ Exams" & vbCrLf
'build output string BuildString(gradeArray) output &= vbCrLf & vbCrLf & "Lowest grade: " & Minimum(gradeArray) & vbCrLf & "Highest Grade: " & Maximum(gradeArray) & vbCrLf 'calculate each student's average For i = 0 To lastStudent output &= vbCrLf & "Average for student " & i & " is " & Average(gradeArray(i)) Next MessageBox.show(output, "Jagged two-dimensional array", MessageBoxButtons.OK,MessageBoxIcon.Information) End Sub Function Minimum(ByVal grades As Integer()()) As Integer Dim lowGrade As Integer = 100 Dim i, j As Integer For i = 0 To lastStudent For j = 0 To lastExam If grades(i)(j) < lowGrade Then lowGrade = grades(i)(j) End If Next Next Return lowGrade End Function Function Maximum(ByVal grades As Integer()()) As Integer Dim highGrade As Integer = 0 Dim i, j As Integer
For i = 0 To lastStudent For j = 0 To lastExam If grades(i)(j) > highGrade Then highGrade = grades(i)(j) End If Next Next Return highGrade End Function Function Average(ByVal setOfGrades As Integer()) As Double Dim i As Integer, total As Integer = 0 For i = 0 To lastExam total += setOfGrades(i) Next Return total / setOfGrades.Length End Function Sub BuildString(ByVal grades As Integer()()) Dim i, j As Integer output &= " " For i = 0 To lastExam output &= "(" & i & ") " Next For i = 0 To lastStudent output &= vbCrLf & " (" & i & ")" For j = 0 To lastExam output &= grades(i)(j) & " " Next Next End Sub End Module
'NOTE 'This program shows the difference between rectangular arrays and jagged arrays 'Initialization 'Traversal Imports System.Windows.Forms Module modMultidimensionalArrays Sub main() Dim output As String Dim i, j As Integer 'creating a two dimensional array Dim array1 As Integer(,) array1 = New Integer(,) {{1, 2, 3}, {4, 5, 6}}
'creating a jagged array Dim array2 As Integer()() array2(0) = New Integer() array2(1) = New Integer() array2(2) = New Integer()
output = "Values in array1 by row are" & vbCrLf 'the zero in the GetUpperBound refers to the row 'NOTE:it is a zero based For i = 0 To array1.GetUpperBound(0) For j = 0 To array1.GetUpperBound(1) output &= array1(i, j) & " " Next output &= vbCrLf Next MessageBox.Show(output, "Initializing Multidimensional Arrays", MessageBoxButtons.OK, MessageBoxIcon.Information) Dim total, row, column As Integer For row = 0 To array2.GetUpperBound(0) For column = 0 To array2(row).GetUpperBound(0) total += array2(row)(column) Next Next MessageBox.Show("The sum of array 2 is " & total, "Sum of Jagged Array", MessageBoxButtons.OK, MessageBoxIcon.Information) End Sub End Module
Module modParamArrayTest Sub main() AnyNumberArguments() AnyNumberArguments(2, 3) AnyNumberArguments(7, 8, 9, 10, 11, 12) End Sub Sub AnyNumberArguments(ByVal ParamArray array1() As Integer) Dim i, total As Integer total = 0 If array1.Length = 0 Then Console.WriteLine("Procedure AnyNumberArguments received 0 arguments")
Else Console.WriteLine("The total of ") For i = 0 To array1.GetUpperBound(0) Console.Write(array1(i) & " ") total += array1(i) Next Console.WriteLine("is {0}", total) End If End Sub End Module