0% found this document useful (0 votes)
41 views1 page

Binary Search

This program uses a binary search algorithm to search for a number entered by the user in an array of numbers entered by the user. It prompts the user to enter 5 numbers to populate an array, then a number to search for. It calculates the midpoint index between the upper and lower array bounds, compares the search value to the number at that index, and updates the bounds accordingly, repeating until the number is found or the bounds cross.

Uploaded by

Chingez Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views1 page

Binary Search

This program uses a binary search algorithm to search for a number entered by the user in an array of numbers entered by the user. It prompts the user to enter 5 numbers to populate an array, then a number to search for. It calculates the midpoint index between the upper and lower array bounds, compares the search value to the number at that index, and updates the bounds accordingly, repeating until the number is found or the bounds cross.

Uploaded by

Chingez Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

Module Module1

Sub Main()
Dim num(4) As Integer
Dim number, upperbound, lowerbound, index As Integer
Dim found As Boolean
upperbound = 4
lowerbound = 0
For count = 0 To 4
Console.WriteLine("please enter numbers")
num(count) = Console.ReadLine
Next
Console.WriteLine("please enter a number to search")
number = Console.ReadLine
Do
index = Int((upperbound + lowerbound) / 2)
If number = num(index) Then
found = True
ElseIf number > num(index) Then
lowerbound = index + 1
ElseIf number < num(index) Then
upperbound = index - 1
End If
Loop Until found Or lowerbound = upperbound
If found Then
Console.WriteLine("the index is" & index)
Else
Console.WriteLine("not found")
End If
Console.ReadKey()
End Sub

End Module

You might also like