Click here to Skip to main content
15,743,179 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to perform a linear search which loops through an array to find an element (which is entered by the user and is referred to as 'SearchItem' in my algorithm) in Visual Basic. The user can enter a name and corresponding number into two parralel separate arrays. The user then enters a number and the number array is searched to see if that number lies there. I have created a basic algorithm which goes as follows:
	Set Index to 0
	Prompt User for ‘SearchItem’
	Get ‘Num’
	Found = False
	While Index < 7 and Found = False
			IF ‘SearchItem’ = ‘Num’ THEN
				Found = True
				Found = False
			Increment Index
	IF Found = True THEN
			Display "Match Found – Num’" 
			Display "Match not Found"

I have never performed a linear search before and I am really unsure of how to compare the search item with the values in the array. Any help would be appreciated as I have have no experience with performing these searches. So far the code I have goes:
Public Class Form1
    Dim Name(7) As String
    Dim Number(7) As Integer
    Dim Index1 As Integer
    Dim LastIndex1 As Double
    Dim SearchItem As Integer
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub btnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnter.Click

        If txtName.Text = "" Then
            MsgBox("Enter a name")
        ElseIf txtNumber.Text = "" Then
            MsgBox("Enter a Number")
        ElseIf txtNumber.Text = "" And txtName.Text = "" Then
            MsgBox("Enter a name and corresponding number")
        ElseIf Index1 = 7 Then
            MsgBox("Maximum data entered, reset for additional input")

            Index1 = Index1 + 1

            Name(Index1) = txtName.Text
            Double.TryParse(txtNumber.Text, Number(Index1))

            For i = LastIndex1 + 1 To Index1
            Next i
            LastIndex1 = Index1
        End If

    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        SearchItem = txtSearchItem.Text
    End Sub
End Class


I really need help with the search button that checks the number array for the search item. If the item is found a msgbox will pop up saying "Match Found - Number" and if it isnt, then the msgbox will say "Not Found".
Updated 31-Oct-12 8:05am

1 solution

No flaw spotted in your logics.

I use to avoid handling a Found flag, like this:
WHILE Index < 7 and  SearchItem <> Num
  Increment Index

IF Index < 7 THEN
  Display "Match Found – Num" 
  Display "Match not Found"
Share this answer
b2906 25-Oct-12 8:32am    
I understand the algorithm and it seems logical. I just cant comprehend how to then take that algorithm and implement it into Visual Basic, that's what I need help with unfortunatley
YvesDaoust 25-Oct-12 8:41am    
You should have stated your question differently then.

Do you have a Visual Basic development environment ?
b2906 25-Oct-12 16:56pm    
Yeah, I just need help doing the linear search in code.
I have Visual Basic 2010 Express.
YvesDaoust 26-Oct-12 2:33am    
So you've got all ingredients now, just code the search routine...
b2906 26-Oct-12 2:38am    
That's the problem, I don't know how too because I've never done a linear search before

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900