by Johny Holmes (4 Submissions)
Category: Math/Dates
Compatability: Visual Basic 4.0 (32-bit)
Difficulty: Unknown Difficulty
Originally Published: Sat 6th March 1999
Date Added: Mon 8th February 2021
Rating: (1 Votes)
Insertion, Deletion, and Searching In a Sorted Array
API Declarations
Private Type arraySStudentRecord
fname As String
lname As String
End Type
Dim ASStudent(Arrsize) As arraySStudentRecord
Dim currloc As Integer
'searching code for a sorted array
'*************** INSERTING CODE **********************
'Declaring the variables
Dim Inputlname As String
Dim Inputfname As String
Dim I
Dim J
Dim K
I = 0
'Check if array is full
If currloc < Arrsize Then
Inputlname = InputBox("Enter last name...")
Inputfname = InputBox("Enter first name...")
Else
MsgBox "Array Is Full", 0, "Big Sexy"
Exit Sub
End If
'Find location
Do While (ASStudent(I).lname < Inputlname) And (I < currloc)
I = I + 1
Loop
'Move all elements to make room for location
For J = currloc To I + 1 Step -1
ASStudent(J).lname = ASStudent(J - 1).lname
ASStudent(J).fname = ASStudent(J - 1).fname
Next J
'Place at location
ASStudent(I).lname = Inputlname
ASStudent(I).fname = Inputfname
currloc = currloc + 1
'Clearing the list box
lstdSort.Clear
'Displaying all entries in the list box
For K = K To currloc - 1
lstdSort.AddItem ASStudent(K).lname & ", " & ASStudent(K).fname
Next K
'**********************************************************************
'****************************** DELETING CODE *************************
'Display all the entries, until the last before the first blank
Dim Inputfname
Dim Inputlname
Dim I As Integer
Dim L
Dim M
Dim locfound As Integer
'Default Value
locfound = -1
If currloc = 0 Then
MsgBox "Records have not been found, array is empty!", 0, "Big Sexy"
Exit Sub
End If
'Information on name to search for
Inputlname = InputBox("Input The last name of the student...", "Big Sexy")
Inputfname = InputBox("Input The first name of the student...", "Big Sexy")
'Search for the specified student(all none blanks)
'If found, program will delete it
For I = I To currloc - 1 Step 1
If ((Inputfname = ASStudent(I).fname) And (Inputlname = ASStudent(I).lname)) Then
locfound = I
MsgBox "The record of " & ASStudent(I).lname & ", " & ASStudent(I).fname & " has been found at " & locfound & " and has been deleted", , "Big Sexy"
lstdSort.Selected(I) = True
'Moving all records back one space
For L = I To currloc - 1 Step 1
ASStudent(L).lname = ASStudent(L + 1).lname
ASStudent(L).fname = ASStudent(L + 1).fname
Next L
'Decreasing by 1
currloc = currloc - 1
'clearing the listbox
lstdSort.Clear
'Displaying all the enteries in the listbox
For M = M To currloc - 1
lstdSort.AddItem ASStudent(M).lname & ", " & ASStudent(M).fname
Next M
End If
Next I
'*******************************************************************
'***************************** SEARCHING CODE **********************
'Declaring the variables
Dim Inputfname As String
Dim Inputlname As String
Dim locfound As Integer
Dim I As Integer
'default value
locfound = -1
'Check if there exists at least one element
If currloc = 0 Then
MsgBox "Record Has Not Been Found", 0, "Big Sexy"
End If
'get info on name to search for
Inputlname = InputBox("Enter Last Name..", "Big Sexy")
Inputfname = InputBox("Enter First Name..", "Big Sexy")
'Search through all the students to find the specified one
For I = I To currloc - 1
If ((Inputlname = ASStudent(I).lname) And (Inputfname = ASStudent(I).fname)) Then
locfound = I
MsgBox "Record " & ASStudent(I).lname & ", " & ASStudent(I).fname & " has been found at location #" & locfound, 0, "Big Sexy"
lstdSort.Selected(I) = True
End If
Next I
'If invalid entery has been put in
If locfound = -1 Then
MsgBox "Record has not been found", 0, "Big Sexy"
End If
'*************************************************************************