Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

deleting from array

Status
Not open for further replies.

noel9

Programmer
Nov 21, 2002
4
IE
Hey there
I need a little help!!
The following is code I have written for to add names to a list,
display who is on the list and also to search for a name on the list.
I need help with the following :

If all seats(20) are taken then the name is added to a waiting list. if someone wishes to be removed then a search is performed for their name and if found they are deleted from the array and whoever was first in the waiting list takes their place.Anyone any ideas how to do this?

Also how would a implement a parallel array for seat,
ie have two arrays of ten seats?

Apologies for the code taking up so much space but I wasnt sure which bits to leave out.

Hope someone can help!!

Regards,
Noel. VB:
Dim seat(1 To 20) As String
Dim counter As Integer

Private Sub cmdAddName_Click()
If (counter < 20) Then
counter = counter + 1
seat(counter) = UCase(Trim(txtName.Text))
txtName.Text = &quot;&quot;
txtName.SetFocus
Else
MsgBox &quot;Sorry Plane is full&quot;, , &quot;&quot;
txtName.Text = &quot; &quot;
cmdDisplaySeat.SetFocus
End If

End Sub

Private Sub cmdDisplaySeat_Click()
Call SortNames
Call ShowNames
End Sub

Private Sub ShowNames()
Dim i As Integer
picSeats.Cls
picSeats.Print
For i = 1 To counter
picSeats.Print seat(i)
If i Mod 2 = 0 Then
picSeats.Print
End If
Next i
End Sub

Private Sub SortNames()
Dim gap As Integer, doneFlag As Boolean
Dim index As Integer, temp As String
gap = Int(counter / 2)
Do While gap >= 1
Do
doneFlag = True
For index = 1 To counter - gap
If seat(index) > seat(index) Then
temp = seat(index)
seat(index) = seat(index + gap)
seat(index + gap) = temp
doneFlag = False
End If
Next index
Loop Until doneFlag = True
gap = Int(gap / 2)
Loop
End Sub

Private Sub BinarySearch(name As String, result As String)
Dim foundFlag As Boolean
Dim first As Integer, middle As Integer, last As Integer

foundFlag = False
first = 1
last = counter

Do While (forst <= last) And (Not foundFlag)
middle = Int((first + last) / 2)
Select Case UCase(seat(middle))
Case name
foundFlag = True
Case Is > name
last = middle - 1
Case Is < name
first = middle + 1
End Select
Loop
If foundFlag Then
result = &quot;is on plane&quot;
Else
result = &quot;is not on plane&quot;
End If
End Sub

Private Sub cmdSearch_Click()
Dim name As String, result As String
name = UCase(Trim(txtName.Text))
Call BinarySearch(name, result)
picSeats.Cls
picSeats.Print name; &quot; &quot;; result
End Sub
 

The first thing I see in your code is in your add sub where counter < 20. I belive it should be counter <= 20 since your array is from 1 to 20 (if 0 to 19 then its ok).

Also you could do everything you want to do with just one array (or collection or dictionary).

Ok to answer your direct questions...

To be able to have two arrays (1 to 10) you could use a two dimensional array (1 to 2, 1 to 10) or use a row1(1 to 10) and row2(1 to 10).

Now back to the collection object and a single array/object being able to do what you want. With a collection object it would be harder to sort the names but it can be done. With a single collection object when you use the remove index method it automatically renumbers the index. Meaning that person 21 and up are on the waiting list. When you remove person 4 the collection will place person 5 in persons 4 index. Meaning that person 21 is now person 20.

Ok so you have a lot of code using arrays and you don't want to start using collections. So you could imatate this behavior by using a bubble sort (like the one you have to order the array with)...
[tt]
Private Sub PutBlanksAtEnd(MyArray() As String)

Dim I As Integer

For I = LBound(MyArray) To Ubound(MyArray) - 1
If Trim(MyArray(I)) = &quot;&quot; Then MyArray(I) = MyArray(I + 1)
Next I

End Sub
[/tt]

I Hope This Helps, Good Luck

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top