19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

174 <strong>Computer</strong> <strong>Programming</strong> <strong>Concepts</strong> <strong>and</strong> <strong>Visual</strong> <strong>Basic</strong><br />

Private Sub cmdSummarize_Click()<br />

Dim i As Integer<br />

‘List stock companies that have been recorded<br />

picStocks.Cls<br />

picStocks.Print “You own the following”; counter; “stocks.”<br />

For i = 1 To counter<br />

picStocks.Print stock(i) & “ ”;<br />

‘Move to new line after every 5 stocks<br />

If Int(i / 5) = i / 5 Then<br />

picStocks.Print<br />

End If<br />

Next i<br />

End Sub<br />

Private Sub Form_Load()<br />

‘Initialize count of companies<br />

counter = 0<br />

End Sub<br />

[Run, type in eleven companies (press Record Name after each company) <strong>and</strong> press Summarize.]<br />

Suppose you have two ordered lists of customers (possibly with some customers on both<br />

lists) <strong>and</strong> you want to consolidate them into a single ordered list. The technique for creating<br />

the third list, called the merge algorithm, is as follows.<br />

1. Compare the two names at the top of the first <strong>and</strong> second lists.<br />

(a) If one name alphabetically precedes the other, copy it onto the third list <strong>and</strong><br />

cross it off its original list.<br />

(b) If the names are the same, copy the name onto the third list <strong>and</strong> cross out<br />

the name from the first <strong>and</strong> second lists.<br />

2. Repeat Step 1 with the current top names until you reach the end of either list.<br />

3. Copy the names from the remaining list onto the third list.<br />

EXAMPLE 3<br />

The following program stores two lists of names in arrays <strong>and</strong> merges them into a third list. Although at<br />

most 10 names will be placed into the third array, duplications will reduce this number. Because the variable<br />

r identifies the next position to insert a name in the third array, r – 1 is the number of names in the<br />

array.<br />

‘Create arrays to hold list of names<br />

Dim list1(1 To 5) As String, list2(1 To 5) As String<br />

Dim newList(1 To 10) As String

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!