Results 1 to 8 of 8

Thread: Largest Number in an Array

  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

  2. #2
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,757

    Re: Largest Number in an Array

    Do you have a question in there somewhere?
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  3. #3
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: Largest Number in an Array

    Start out with using Min, Max, Sum
    Code:
    Dim A() As Integer = {60, 40, 80, 100, 30, 20, 90, 50, 10, 70}
    Console.WriteLine("Min: {0} Max: {1} Sum: {2}", A.Min, A.Max, A.Sum)

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: Largest Number in an Array

    Hi TKDlover14,

    I've added code tags to your code. They help the code remain properly formatted and they're simple to use. You can either manually type it out:

    [CODE][/CODE]

    Or you can hit the pound(hash) button in the quote or advanced reply.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Sep 2014
    Posts
    25

    Re: Largest Number in an Array

    I just need help doing this:

    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().

  6. #6
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: Largest Number in an Array

    Hello,

    I gave you one already and don't expect me or anyone else to do all the work especially where this appears like a school assignment. Another example for max and min


    Code:
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim A() = {60, 40, 80, 100, 30, 20, 90, 50, 10, 70}
    
            Dim maxVal? As Integer = Nothing
            Dim index As Integer = -1
            Dim thisNum As Integer = 0
    
            For i As Integer = 0 To A.Length - 1
                thisNum = A(i)
                If (Not maxVal.HasValue) OrElse thisNum > maxVal.Value Then
                    maxVal = thisNum
                    index = i
                End If
            Next i
    
            Console.WriteLine(maxVal)
            Console.WriteLine(Min(A))
    
        End Sub
        Private Function Min(ByVal numbers() As Integer) As Integer
            Dim m As Integer = numbers(0)
            For i As Integer = 0 To numbers.Length - 1
                If m > numbers(i) Then
                    m = numbers(i)
                End If
            Next i
            Return m
        End Function

  7. #7
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: Largest Number in an Array

    Here is another way:
    Code:
    Private Function ReturnMaximum(ByVal numbers() As Integer) As Integer
        Array.Reverse(numbers)
    
        Return numbers(0)
    End Function
    
    Private Function ReturnMinimum(ByVal numbers() As Integer) As Integer
        Array.Sort(numbers)
    
        Return numbers(0)
    End Function
    To call the function, it's as simple as this:
    Code:
    Dim myArray() As Integer = {1, 5, 10, 7, 2}
    
    Dim minimum As Integer = ReturnMinimum(myArray)
    Dim maximum As Integer = ReturnMaximum(myArray)
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  8. #8
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,757

    Re: Largest Number in an Array

    What about the code YOU wrote (or at least the code your posted)? Does it run? If not you should be asking questions about it and what might be wrong with it.
    Last edited by kebo; Nov 5th, 2014 at 04:15 PM.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

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