Results 1 to 8 of 8

Thread: Largest Number in an Array

Threaded View

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2014
    Posts
    25

    Largest Number in an Array

    Call a function, named Maximum(), which finds and returns the largest value in
    the array A(). Print the largest value from inside Main().

    Call Maximum() which finds and returns the largest value in the array B().
    Print the largest value from inside Main().



    Code:
    ' Program: P07.vb
    ' Author: Anna DeAngelis
    
    Module P07
    
       '------------------Subroutine: Main()----------------------------------
       Sub Main()
          Console.Clear()
          Console.WriteLine("Anna DeAngelis     Assignment 7" & vbCrLf)
          Dim aSum, bSum As Integer
          Dim min As Integer
          
      '-----------------------------------------------------------------------
          Dim A() As Integer = {60, 40, 80, 100, 30, 20, 90, 50, 10, 70}
         
         Console.Write("Array A[]:")
         PrintArray(A)
         aSum = Sum(A)
         Console.WriteLine(Space(10) & "Sum of the elements of A[] = " & aSum)
    
         min = Minimum(A)
         Console.WriteLine("Smallest Element of A() = " & min)
     '------------------------------------------------------------------------
         Dim B() As Integer = {15, 95, 35, -10, 45, -5, 85, 75, 65, 55, -20, 25, 5, 25, -15}
    
         Console.Write("Array B[]:")
         PrintArray(B)
         bSum = Sum(B)
         Console.WriteLine(Space(4) & "Sum of the elements of B[] = " & bSum & vbCrLf & vbCrLf)
     
         min = Minimum(B)
         Console.WriteLine("Smallest Element of B[] = " & min & vbCrLf & vbCrLf)
    End Sub 'Main()
    
    '---------------------Subroutine: Print()-----------------------------
    Sub PrintArray(ByVal P As Integer())
       Dim k As Integer
       For k = 0 To P.GetUpperBound(0)
           Console.Write(Format(P(k), "0").PadLeft(6))
       Next
    End Sub 'Print()
    
    '--------------------Function: Sum()----------------------------------------
    Function Sum(ByVal P As Integer()) As Integer
       Dim arraySum As Integer = 0
       For k = 0 To P.GetUpperBound(0)
           arraySum = arraySum + P(k)
       Next
       Return arraySum
    
    End Function 'Sum()
    
    '-----------------------Function: Minimum()----------------------------
     Function Minimum(ByVal P As Integer()) As Integer
        Dim min = P(0), k As Integer
        For k = 1 To P.GetUpperBound(0)
           If (P(k) < min) Then : min = P(k) : End If
        Next
        Return min
     End Function 'Minimum()
    
    '-----------------------Function: Minimum()----------------------------
     Function Minimum(ByVal P As Integer()) As Integer
        Dim min = P(0), k As Integer
        For k = 1 To P.GetUpperBound(0)
           If (P(k) < min) Then : min = P(k) : End If
        Next
        Return min
     End Function 'Minimum()
    
    
    End Module 'P07
    Last edited by dday9; Nov 4th, 2014 at 12:58 PM. Reason: Added code tags

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width