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
Re: Largest Number in an Array
Do you have a question in there somewhere?
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)
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.
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().
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
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)
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.