'///////////////////// BAM - Arrays #4b /////////////// Public Class Form1 '//////// GLOBAL DATA Const MAXSIZE As Integer = 50 ' Maximum array size Dim many As Integer = 0 ' Actual size used. (Number of employees.) '//////// Employee data. Dim id(MAXSIZE) As Integer ' Array of ID numbers. '++++ Just ID for now; add more, later. ++++ '... '//////// METHODS FOR FILE INPUT //////// Private Sub bAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bAdd.Click '//////// Get next employee input from form; store into array. '++++ Just ID for now; add more, later. ++++ Dim nextid As Integer Dim nextln As String = "" Dim nextfn As String = "" Dim nextdep, nextcode As Integer Dim nextrate As Double Call getInputs(nextid, nextln, nextfn, nextdep, nextcode, nextrate) '@@@@ showit("bAdd: nextid= " & Format(nextid)) '//// Store the values into array(s). Call storeValues(nextid) End Sub Sub getInputs(ByRef nextid As Integer, _ ByVal nextln As String, ByVal nextfn As String, _ ByVal nextdep As Integer, ByVal nextcode As Integer, ByVal nextrate As Double) '//// Get input value(s) from the form. '++++ Just ID for now; add more, later. ++++ '... '//// Get input value(s) nextid = Val(Me.tID.Text) '//// Also, clear the input(s). Me.tID.Text = "" End Sub Sub storeValues(ByVal nextid As Integer) '//////// Store the value(s) into the array(s). '++++ Just ID for now; add more, later. ++++ '... '//// Check for room in array, bad values, etc. '@@@@ showit("storeValues: nextid= " & Format(nextid)) If (many >= MAXSIZE) Then MsgBox("No more room in arrays: ", many) Return ElseIf (nextid <= 0) Then ' Error check(s) MsgBox("Bad ID ignored: ", nextid) Return Else '//// Advance the index, for next array element. many += 1 ' Next employee Dim n = many '//// Store the input value(s) into the array(s). id(n) = nextid '++++ Just ID for now; add more, later. ++++ '... End If End Sub '///////// METHODS FOR FILE INPUT //////// Private Sub bRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bRead.Click '//// Input data from a file. Dim sr As IO.StreamReader sr = IO.File.OpenText(Me.tFilename.Text) ' Get filename from textbox '@@@@ showit(Me.tFilename.Text & " was openned") '@@@ '@@@@ showit("PEEK: " & sr.Peek) While (sr.Peek <> -1) '//// read the next record into string "s". Dim s As String s = sr.ReadLine ' Read record into s '@@@@ showit(s) '@@@ '//// Extract values from s. Dim nextID As Integer Call getFields(s, nextID) '//// Store values into the array(s). Call storeValues(nextID) End While End Sub Sub getFields(ByVal s As String, ByRef nextid As Integer) '//// Decode the values from fields of the input string '++++ Just ID for now; add more, later. ++++ '... '//// Get input value(s) nextid = Val(s) End Sub '//////// OUTPUT METHODS //////// Sub clearout() '//// Clear output area Me.oListbox.Items.Clear() End Sub Sub showit(ByVal s As String) '//// Display a string Me.oListbox.Items.Add(s) End Sub Sub showit(ByVal s As String, ByVal x As Double) '//// Display a string and a value Me.oListbox.Items.Add(s & Format(x)) End Sub Sub showit(ByVal s As String, ByVal x As Integer) '//// Display a string and a value Me.oListbox.Items.Add(s & Format(x)) End Sub Sub showit(ByVal s As String, ByVal x As String) '//// Display a string and a value Me.oListbox.Items.Add(s & Format(x)) End Sub '//////// OUTPUT THE ARRAY VALUES //////// Private Sub bReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bReport.Click '//////// Show all elements of array. Call clearout() Call showit("Number of elements in array: ", many) ' OUTPUT: Show each element Dim j As Integer = 0 For j = 1 To many Call showemp(j) Next End Sub Sub showemp(ByVal j As Integer) '//// Display data for one employee. Dim s As String s = Format(id(j)) Call showit(s) '++++ STUB. Add more employee data. End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click '//////// Clear the array(s). many = 0 End Sub Private Sub bBig_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bBig.Click '//////// Find biggest value in the array. Dim k As Integer k = wherebig(id, many) showit(" The biggest value is at id(" & Format(k) & ")= ", id(k)) End Sub Function wherebig(ByVal a() As Integer, ByVal m As Integer) As Integer '//// Return index of biggest in array. Dim w As Integer = 1 ' Start with w=1 Dim j As Integer For j = 2 To m ' Search array for anything bigger; save index If (a(j) > a(w)) Then w = j End If Next Return w End Function Private Sub bTiny_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bTiny.Click '//////// Find smallest value in the array. Dim k As Integer k = wheresmall(id, many) showit(" The smallest value is at id(" & Format(k) & ")= ", id(k)) End Sub Function wheresmall(ByVal a() As Integer, ByVal m As Integer) As Integer '//// Return index of smallest in array. Dim w As Integer = 1 ' Start with w=1 Dim j As Integer For j = 2 To m ' Search array for anything smaller; save index If (a(j) < a(w)) Then w = j End If Next Return w End Function Private Sub bMatch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bMatch.Click '//// Find matching value in array Dim nextid As String = Me.tMatch.Text Dim k As Integer k = lookup(nextid, id, many) If (k > 0) Then showit(" The matching value is at id(" & Format(k) & ")= ", id(k)) Else showit("No match for ", nextid) End If End Sub Function lookup(ByVal it As Integer, ByVal a() As Integer, ByVal m As Integer) As Integer '//// Return index of matching value. Dim w As Integer = -1 ' Start with w=1 Dim j As Integer For j = 1 To m ' Search array for match If (a(j) = it) Then w = j End If Next Return w End Function Private Sub bBinarySearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bBinarySearch.Click '////Binary search to find match Dim nextid As String = Me.tMatch.Text Dim k As Integer k = lookup(nextid, id, many) If (k > 0) Then showit(" The matching value is at id(" & Format(k) & ")= ", id(k)) Else showit("No match for ", nextid) End If End Sub Function binsearch(ByVal it As Integer, ByVal a() As Integer, ByVal m As Integer) As Integer '//// Return index of matching value. Dim w As Integer = -1 ' Start with w=1 Dim j As Integer '+++++++++++ CHANGE THIS TO BINARY SEARCH +++++ Return w End Function Private Sub bSort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bSort.Click '//// Sort the "id" array (ascending order) Call sortarray(id, many) End Sub Sub sortarray(ByRef a() As Integer, ByVal m As Integer) '//// Sort an array. Dim j, k As Integer For j = m To 2 Step -1 ' Go backwards, making array smaller & smaller k = wherebig(a, j) ' Find the biggest. Call swap(a, j, k) ' Move biggest to end Next End Sub Sub swap(ByVal a() As Integer, ByVal j As Integer, ByVal k As Integer) '//// Swap two elements of array. Dim tmp As Integer tmp = a(j) a(j) = a(k) a(k) = tmp End Sub End Class