Linear Search Program in Visual Basic 2008

3,989 views
3,639 views

Published on

A Program on Linear Search using Visual Basic 2008

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,989
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
125
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Linear Search Program in Visual Basic 2008

  1. 1. A Program in Linear Search Using Visual Basic 2008 (version 9.0) By Ariel T. Sumagaysay, MSCSA. Form DesignDesign the form shown below using the Visual Basic 2008:B. Components The following are the components of the program: 1 Form, 1 GroupBox, 4 Labels, 1 ListBox, 5 TextBoxes and 5 Buttons.
  2. 2. 2C. Code ListingsLINEAR SEARCHPROGRAM BY ARIEL T. SUMAGAYSAYCOLLEGE OF INFORMATION & COMMUNICATION TECHNOLOGYWEST NEGROS UNIVERSITYDECEMBER 15, 2008Public Class Form1 Variables Dim nNumberOfElements As Integer Dim sNumbers As String Dim nFind As Integer Dim n As Integer Dim x As Integer Dim s As String One Dimensional Array Dim aInputSet() As Integer Private Sub Form1_Load(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles MyBase.Load Setting Buttons - controls Button2.Enabled = False Button3.Enabled = False Button4.Enabled = False TextBox1.Enabled = True TextBox6.Enabled = False TextBox7.Enabled = False TextBox1.Text = " " TextBox2.Text = " " TextBox3.Text = " " TextBox6.Text = " " TextBox7.Text = " " ListBox1.Items.Clear() Initialize variables nNumberOfElements = 0 n = 0 x = 0 s = " " sNumbers = " " Set string of numbers to NULL nFind = 0 Set the Number of Elements ReDim aInputSet(0) End Sub
  3. 3. 3 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button2.Click Re - setting Buttons Button2.Enabled = False Button3.Enabled = False Button4.Enabled = False Button5.Enabled = True TextBox1.Enabled = True TextBox6.Enabled = False TextBox7.Enabled = False TextBox1.Text = " " TextBox2.Text = " " TextBox3.Text = " " TextBox6.Text = " " TextBox7.Text = " " ListBox1.Items.Clear() Initialize variables nNumberOfElements = 0 n = 0 x = 0 s = " " sNumbers = " " Set string of numbers to NULL nFind = 0 Set the Number of Elements ReDim aInputSet(0) End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button1.Click Terminate Program End End Sub Private Sub bubblesort() Sort the elements using bubblesort Static a As Integer Static b As Integer Static temp As Integer Static sHereNumbers As String initialize variables a = 0 b = 0 temp = 0 sHereNumbers = " " For a = 1 To n For b = 1 To n - 1
  4. 4. 4 If (aInputSet(b) > aInputSet(b + 1)) Then temp = aInputSet(b) aInputSet(b) = aInputSet(b + 1) aInputSet(b + 1) = temp End If Next Next display list For a = 1 To n sHereNumbers = sHereNumbers + Str(aInputSet(a)) & " " TextBox3.Text = sHereNumbers Next Enable Search button Button3.Enabled = True TextBox7.Enabled = True End Sub Private Sub Button5_Click_1(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button5.Click Setting Buttons Button4.Enabled = True Button5.Enabled = False TextBox6.Enabled = True TextBox1.Enabled = False Get number of elements nNumberOfElements = CInt(TextBox1.Text) n = nNumberOfElements ReDim aInputSet(n) set command button label s = Format(1, "0#") Button4.Text = "&ENTER ELEMENT " + s End Sub Private Sub Button4_Click_1(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button4.Click Static y As Integer y = 0 Enter numbers If TextBox6.Text <> " " Then x = x + 1 y = x s = Format(y + 1, "0#") Button4.Text = "&ENTER ELEMENT " + s aInputSet(x) = CInt(TextBox6.Text) If x = n Then Button4.Enabled = False TextBox6.Enabled = False Button4.Text = "&ENTER ELEMENT"
  5. 5. 5 Call bubblesort() End If sNumbers = sNumbers & TextBox6.Text & " " TextBox2.Text = sNumbers TextBox6.Text = " " End If End Sub Private Sub Button3_Click_1(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button3.Click Static z As Integer Static bFind As Boolean Setting Buttons & Initialization Button2.Enabled = True bFind = False ListBox1.Items.Clear() Searching nFind = CInt(TextBox7.Text) For z = 1 To n If nFind = aInputSet(z) Then ListBox1.Items.Add("Number is found on location " & Str(z)) bFind = True End If Next If bFind = False Then ListBox1.Items.Add(" Number is NOT FOUND on the LIST! ") End If End SubEnd Class

×