Professional Documents
Culture Documents
Arrays 7
Now go, write it before them in
a table, and note it in a book.
—Isaiah 30:8
To go beyond is as wrong as to
fall short
—Confucius
Objectives
In this chapter you’ll learn:
■ How arrays are used to store,
sort and search lists and
tables of values.
■ To declare, initialize and refer
to elements of arrays.
■ To pass arrays to methods.
■ To use the For Each…Next
statement to iterate through
all the elements of an array.
■ To use the elements of arrays
as counters.
■ To declare and manipulate
rectangular arrays.
2 Chapter 7 Arrays
Self-Review Exercises
7.1 Answer each of the following questions:
a) The number that refers to a particular element of an array is called its .
1. value 2. size
3. indexed array name 4. index
ANS: 4.
b) The indexed array name of one-dimensional array units’s element 2 is .
1. units{2} 2. units(2)
3. units[0,2] 4. units[2]
ANS: 2.
c) An array’s length is .
1. one more than the array’s last index 2. one less than the array’s last index
3. the same as the array’s last index 4. returned by method GetUpperBound
ANS: 1.
d) Property specifies the source of the data displayed in a ComboBox.
1. ComboData 2. Source
3. DataList 4. DataSource
ANS: 4.
e) ComboBox property is 0 when the first ComboBox item is selected.
1. SelectedIndex 2. SelectedValue
3. Index 4. SelectedNumber
ANS: 1.
f) The process of ordering the elements of an array is called the array.
1. allocating 2. sorting
3. declaring 4. initializing
ANS: 2.
g) Which of the following sorts array averageRainfall?
1. Array(averageRainfall).Sort() 2. Sort.Array(averageRainfall)
3. Sort(averageRainfall) 4. Array.Sort(averageRainfall)
ANS: 4.
h) Arrays that use two indices are referred to as arrays.
1. single-dimensional 2. two-dimensional
3. double 4. one-dimensional
ANS: 2.
i) The expression creates an Integer array of two rows and five columns.
1. Dim a(2, 5) As Integer 2. Dim a(1, 5) As Integer
3. Dim a(1, 4) As Integer 4. Dim a(2, 4) As Integer
ANS: 3.
j) The event is raised when a RadioButton is either selected or deselected.
1. CheckedChanged 2. Changed
3. SelectedChanged 4. None of the above
ANS: 1.
k) When one RadioButton in a container is selected, .
1. others can be selected at the same time 2. a logic error will occur
3. all others will be deselected 4. Both 1 and 2
ANS: 3.
vbhtp6_07.fm Page 3 Friday, June 28, 2013 10:19 AM
Quick Quiz 3
Quick Quiz
7.3 Answer each of the following questions:
a) Arrays can be declared to hold values of .
1. type Double 2. type Integer
3. type String 4. any data type
ANS: 4.
b) An array’s elements are related by the fact that they have the same name and .
1. constant value 2. index
3. type 4. value
ANS: 3.
c) Method returns an array’s highest index.
1. GetUpperBound 2. GetUpperLimit
3. GetHighestIndex 4. GetUpperIndex
ANS: 1.
d) The first element in every array is the .
1. index 2. zeroth element
3. length of the array 4. smallest value in the array
ANS: 2.
e) The initializer list can .
1. be used to determine the size of the array
2. contain a comma-separated list of initial values for the array elements
3. be empty
4. All of the above
ANS: 4.
f) Which method call sorts array words in ascending order?
1. Array.Sort(words) 2. words.SortArray()
3. Array.Sort(words, 1) 4. Sort(words)
ANS: 1.
vbhtp6_07.fm Page 4 Friday, June 28, 2013 10:19 AM
4 Chapter 7 Arrays
Exercises 5
Exercises
NOTE: Solutions to the programming exercises are located in the sol_ch07 folder.
7.4 Write statements to accomplish each of the following tasks:
a) Display the value of element 6 of array numbers.
ANS: Console.WriteLine("Element 6 of numbers is {0}.", numbers(6))
b) Using a For…Next statement, assign the value 8 to each of the five elements of one-di-
mensional Integer array values.
ANS: Dim values As Integer()
values = New Integer(4) {}
For i = 0 To values.GetUpperBound(0)
values(i) = 8
Next
c) Total the 100 elements of floating-point array results.
ANS: Dim total As Double
For i = 0 To results.GetUpperBound(0)
total += results(i)
Next
d) Copy 11-element array source into the first portion of 34-element array sourceCopy.
ANS: For i = 0 To source.GetUpperBound(0)
sourceCopy(i) = source(i)
Next
e) Determine the smallest and largest values in 99-element floating-point array data.
ANS: Dim largest As Double = data(0)
Dim smallest As Double = data(0)
For i = 0 To data.GetUpperBound(0)
If data(i) > largest Then
largest = data(i)
End If
7.7 (What Does This Code Do?) This function declares numbers as its parameter. What does it
return?
6 Chapter 7 Arrays
ANS: This method takes the contents of parameter numbers and reverses the order of its
contents, returning the reversed array to the caller.
7.8 (What’s Wrong with This Code?) The code that follows uses a For…Next loop to sum the
elements in an array. Find the error(s) in the following code:
1 Sub SumArray()
2 Dim sum As Integer
3 Dim numbers() As Integer = {1, 2, 3, 4, 5, 6, 7, 8}
4
5 For counter As Integer = 0 To numbers.Length
6 sum += numbers(counter)
7 Next
8 End Sub ' SumArray
ANS: Array numbers does have numbers.Length number of elements, but the indices are
zero through numbers.Length – 1. The For…Next loop increments counter beyond
the highest index in the array, which results in a runtime error. One solution would
be to use numbers.Length - 1 as the loop-termination condition. Or, you can use the
code that follows:
1 Sub SumArray()
2 Dim sum As Integer
3 Dim numbers As Integer() = {1, 2, 3, 4, 5, 6, 7, 8}
4
5 For counter As Integer = 0 To numbers.GetUpperBound(0)
6 sum += numbers(counter)
7 Next
8 End Sub ' SumArray
7.11 (What Does This Code Do?) What is returned by the following code? Assume that Get-
StockPrices is a Function that returns a 2-by-31 array, with the first row containing the stock price
at the beginning of the day and the last row containing the stock price at the end of the day, for each
day of the month.
ANS: The method returns a one-dimensional array containing the daily stock-price change
for each day of the month.
Solution Manual for Visual Basic 2012 How to Program, 6/E 6th Edition : 0133406954
vbhtp6_07.fm Page 7 Friday, June 28, 2013 10:19 AM
Exercises 7
7.12 (What’s Wrong with This Code?) Find the error(s) in the following code. The TwoDArrays
procedure should create a two-dimensional array and initialize all its values to one.
1 Sub TwoDArrays()
2 Dim array(3, 3) As Integer
3
4 ' assign 1 to all cell values
5 For i As Integer = 0 To 3
6 array(i, i) = 1
7 Next
8 End Sub ' TwoDArrays
ANS: Two nested For…Next loops should be used to assign each element in a two-dimen-
sional array. The corrected code is as follows:
1 Sub TwoDArrays()
2 Dim array As Integer(,)
3
4 array = New Integer(0 To 3, 0 To 3) {}
5
6 ' assign 1 to all cell values
7 For i As Integer = 0 To 3
8 For j As Integer = 0 To 3
9 array(i, j) = 1
10 Next j
11 Next i
12 End Sub ' TwoDArrays