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.

The range of an array need not just begin with 1. A statement of the form<br />

Dim arrayName(m To n) As varType<br />

where m is less than or equal to n, creates an array with elements arrayName(m), arrayName(m<br />

+ 1), arrayName(m + 2), . . . , arrayName(n). The same holds for ReDim.<br />

EXAMPLE 6<br />

The following program segment stores the names of the 13th, 14th, <strong>and</strong> 15th Chief Justices of the U.S.<br />

Supreme Court in the array pictured in Figure 6-4.<br />

‘Place names of last three Chief Justices in an array<br />

Dim chiefJustice(13 To 15) As String<br />

Private Sub Form_Load()<br />

chiefJustice(13) = “Earl Warren”<br />

chiefJustice(14) = “Warren Burger”<br />

chiefJustice(15) = “William Rehnquist”<br />

End Sub<br />

FIGURE 6-4 The Array Created by Example 6<br />

An array can be used as either a checklist or frequency table, as in the next example. The<br />

function Asc associates each character with its position in the ANSI table.<br />

EXAMPLE 7<br />

The following program requests a sentence as input <strong>and</strong> records the number of occurrences of each letter<br />

of the alphabet. The array charCount( ) has range Asc(“A”) To Asc(“Z”), that is, 65 To 90. The number<br />

of occurrences of each letter is stored in the element whose subscript is the ANSI value of the uppercase<br />

letter.<br />

Private Sub cmdAnalyze_Click()<br />

Dim index As Integer, letterNum As Integer, sentence As String<br />

Dim letter As String, column As Integer<br />

‘Count occurrences of different letters in a sentence<br />

ReDim charCount(Asc(“A”) To Asc(“Z”)) As Integer<br />

For index = Asc(“A”) To Asc(“Z”)<br />

charCount(index) = 0<br />

Next index<br />

‘Consider <strong>and</strong> tally each letter of sentence<br />

sentence = UCase(txtSentence.Text)<br />

For letterNum = 1 To Len(sentence)<br />

letter = Mid(sentence, letterNum, 1)<br />

If (letter >= “A”) And (letter

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

Saved successfully!

Ooh no, something went wrong!