Practice Exercise 9
More Array Exercises

Public Class Form1

     ' Test of function that returns the difference between the largest and smallest
     ' elements in an array of integers.
     Private Sub cmdTest_Click(ByVal sender As System.Object, _
                                             ByVal e As System.EventArgs) _
                                             Handles cmdExercise1.Click
          Dim newArray() As Integer = New Integer() {11, 2, 32, 66, 22, 19, 34, 49}
          MsgBox("The difference between the largest and smallest elements in the array is " &  _
          maxElement(newArray) - minElement(newArray))
     End Sub

     ' This function returns the difference between the largest and smallest elements
     ' in an array of integers.
     Private Function arrayValueRange(ByVal testArray() As Integer) As Integer
          Return maxElement(testArray) - minElement(testArray)
     End Function

     ' This function returns the maximum value in an array of integers.
     Private Function maxElement(ByVal testArray() As Integer) As Integer
          Dim tempMax As Integer = testArray(0)
          Dim lcv As Integer

          ' start loop at 1 since we already accounted for element 0
          ' in the initialization of the temp variable
          For lcv = 1 To testArray.GetUpperBound(0)
               If testArray(lcv) > tempMax Then
                    tempMax = testArray(lcv)
               End If
          Next
          Return tempMax
     End Function

     ' This function returns the minimum value in an array of integers.
     Private Function minElement(ByVal testArray() As Integer) As Integer
          Dim tempMin As Integer = testArray(0)
          Dim lcv As Integer

          ' start loop at 1 since we already accounted for element 0
          ' in the initialization of the temp variable
          For lcv = 1 To testArray.GetUpperBound(0)
               If testArray(lcv) < tempMin Then
                    tempMin = testArray(lcv)
               End If
          Next

          Return tempMin
     End Function


     ' Test of a sub that sets each element in array to the sum of the corresponding
     ' elements in two other arrays
     Private Sub cmdExercise2_Click(ByVal sender As System.Object, _
                                                     ByVal e As System.EventArgs) _
                                                     Handles cmdExercise2.Click
          Dim array1() As Integer = New Integer() {2, 4, 5, 8}
          Dim array2() As Integer = New Integer() {1, 0, 4, 6}
          Dim newArray() As Integer ' note: declared but not given a size

          Call sumArrays(array1, array2, newArray)

          Dim lcv As Integer
          Dim output As String = "Subscript" & vbTab & "Value" & vbCrLf
          For lcv = 0 To newArray.GetUpperBound(0)
               output &= lcv & vbTab & newArray(lcv) & vbCrLf
          Next

          MessageBox.Show(output, "Summed Array", MessageBoxButtons.OK, _
                         MessageBoxIcon.Information)

     End Sub

     ' This sub sets each element in array to the sum of the corresponding elements in two other arrays.
     '
     ' Notice that sumArray is passed by reference. This is because it was declared in the calling routine
     ' but not given a size. It is given a size in this procedure, but unless it is passed by reference
     ' nothing will be returned.
     ' Similarly, if the array had been given a size in the calling routine but redimmed in the sub, it
     ' would also have to be passed by reference.
     ' Conversely, if it had been given a size in the calling routine and that size is unchanged in the
     ' sub, it would be passed ByVal like usual.
     Private Sub sumArrays(ByVal array1() As Integer, ByVal array2() As Integer, _
                                       ByRef sumArray() As Integer)
          ' Set arraySize to the size of the smaller array
          Dim arraySize As Integer = array1.GetUpperBound(0)
          If arraySize > array2.GetUpperBound(0) Then
               arraySize = array2.GetUpperBound(0)
          End If

          sumArray = New Integer(arraySize) {}

          Dim lcv As Integer
          For lcv = 0 To arraySize
               sumArray(lcv) = array1(lcv) + array2(lcv)
          Next
     End Sub

End Class