Click here to Skip to main content
11,920,772 members (59,959 online)
Rate this:
Please Sign up or sign in to vote.
See more: VB VB.NET
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".
Posted 25-Oct-12 3:17am
Edited 31-Oct-12 9:05am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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”
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
YvesDaoust 26-Oct-12 3:38am
This is a really unexpected statement, as 1) you understand the concepts, 2) you are quite able to use the VB IDE, 3) you wrote the code for a linear search, and yet you claim that you cannot do it !?

We always do things we never did before. What are you missing ?
b2906 26-Oct-12 3:48am
No, that code is die the rest of the program, I'm just unsure of how to do the search button which performs the linear search.
YvesDaoust 26-Oct-12 3:59am
" that code is die " ???
" how to do " ???

Please rephrase.
b2906 26-Oct-12 4:02am
For* sorry about that, was auto correct. The enter button is the majority of the program I'm just unsure of how to perform the search of the array.
YvesDaoust 26-Oct-12 5:59am
I am doing my best to figure out what you can't do. You give me little clue. Please explain.
b2906 26-Oct-12 6:03am
Okay. I have started a program where the user enters a name and number into an array (which I have already coded in the enter button). Then I need to be able to enter a number into a textbox and if this number is found in the array of numbers there is a message box displaying "Match found" and if not then "no match". I need help comparing the value being searched for with the items in the number array (in code).
YvesDaoust 26-Oct-12 6:18am
Yes, this is clear from the beginning.

You already know how to display a message box and input numbers, don't you ?
You know how to respond to GUI events like a button click, don't you ?
You also know how to perform a linear search in an array, don't you ?

So what is missing ? Just how to compare two numbers ?
b2906 26-Oct-12 6:20am
I'm just not sure how to perform a linear search based on the search item inputted by the user
YvesDaoust 26-Oct-12 6:26am
This is becoming quite surrealistic.

Do you realize that you gave the solution at the beginning of your post?
b2906 26-Oct-12 6:29am
No, that was an algorithm explaining what I want to do. I don't know how to do this in code though, that is what I'm asking for help with (how to write a linear search on code).
YvesDaoust 26-Oct-12 6:56am
What you wrote is 95% valid Visual Basic. Just enter it and fix the syntax errors. (I am not going to do that for you.)
b2906 26-Oct-12 6:58am
That was for the rest of the program. I just don't know how to do the linear Search in a button

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

  Print Answers RSS
0 OriginalGriff 6,778
1 KrunalRohit 4,530
2 Sergey Alexandrovich Kryukov 3,203
3 George Jonsson 2,815
4 Suvendu Shekhar Giri 2,181

Advertise | Privacy | Mobile
Web01 | 2.8.151120.1 | Last Updated 31 Oct 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100