Asked by Raj
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?
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?
Answers
Answered by
oobleck
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.
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.
There are no AI answers yet. The ability to request AI answers is coming soon!
Submit Your Answer
We prioritize human answers over AI answers.
If you are human, and you can answer this question, please submit your answer.