WAP to find maximum and minimum element in an array.InVBA

Dim a(5), n As Integer
Console.WriteLine("Enter 5 numbers")
For n = 1 To 5
a(n) = Console.ReadLine()
Next
For n = 1 To 5
If a(1) < a(n) Then a(1) = a(n)
Next
Console.WriteLine("Greatest=" & a(1))
Console.ReadLine()

For n = 1 To 5
If a(1) > a(n) Then a(1) = a(n)
Next
Console.WriteLine("Smallest=" & a(1))
Console.ReadLine()

I have entered 1,2 ,3,4,5.I should get maximum 5 and minimum 1 but I am getting minimum 2 Why?

1 answer

Your first loop changes a(1), so the contents of the array have been changed. a(1) is no longer the minimum value.
Rather than changing a(1), create a separate variable, say "maxval" and use it to store the maximum value.

Plus, if you also create a "minval" name, you can scan for both max ind min in a single loop.
Similar Questions
  1. WAP to check if number is in array using VBA.Dim a(4), n, no As Integer Console.WriteLine("Enter a no.") For n = 0 To 4 a(n) =
    1. answers icon 4 answers
  2. WAP to reverse an array of integers in VBA.Dim a(5), n As Integer Dim b = 0, e = n - 1, temp As Integer Console.WriteLine("Enter
    1. answers icon 0 answers
    1. answers icon 2 answers
    1. answers icon 2 answers
more similar questions