You are on page 1of 8

Solution Manual for Visual Basic 2012 How to

Program, 6/E 6th Edition : 0133406954

To download the complete and accurate content document, go to:


https://testbankbell.com/download/solution-manual-for-visual-basic-2012-how-to-prog
ram-6-e-6th-edition-0133406954/
Solution Manual for Visual Basic 2012 How to Program, 6/E 6th Edition : 0133406954
vbhtp6_07.fm Page 1 Friday, June 28, 2013 10:19 AM

Arrays 7
Now go, write it before them in
a table, and note it in a book.
—Isaiah 30:8

With sobs and tears he sorted


out
Those of the largest size …
—Lewis Carroll

Attempt the end, and never


stand to doubt;
Nothing’s so hard, but search
will find it out.
—Robert Herrick

Begin at the beginning, ... and


go on till you come to the end:
then stop.
—Lewis Carroll

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.

Visit TestBankBell.com to get complete for all chapters


vbhtp6_07.fm Page 2 Friday, June 28, 2013 10:19 AM

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

l) Typically, statements are used to iterate over each element in a two-dimen-


sional array.
1. Do While…Loop 2. nested For…Next
3. Do…Loop Until 4. nested Do…Loop While
ANS: 2.
7.2 State whether each of the following is true or false. If false, explain why.
a) An array can store many different types of values.
ANS: False. An array can store only values of the same type.
b) An array index normally should be of type Double.
ANS: False. An array index must be a nonnegative integer or integer expression.
c) Method GetUpperBound returns the highest numbered index in an array.
ANS: True.
d) To determine the number of elements in an array, use the NumberOfElements property.
ANS: False. To determine the number of elements in an array, we can use the Length prop-
erty.
e) The linear search works well for unsorted arrays.
ANS: True.

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

g) The ComboBox control combines a TextBox control with a control.


1. DateTimePicker 2. ListBox
3. NumericUpDown 4. Label
ANS: 2.
h) Property contains the size of an array.
1. Elements 2. ArraySize
3. Length 4. Size
ANS: 3.
i) When declaring an array, a(n) is required inside parentheses in order to indi-
cate that the array is two-dimensional.
1. comma 2. asterisk
3. period 4. apostrophe
ANS: 1.
j) In a array each row contains the same number of columns.
1. data 2. rectangular
3. tabular 4. All of the above
ANS: 2.
k) In an m-by-n array, the m stands for .
1. the number of columns in the array 2. the total number of array elements
3. the number of rows in the array 4. the number of elements in each row
ANS: 3.
l) Which of the following creates an Integer array of five rows and three columns?
1. Dim values(5, 3) As Integer 2. Dim values(4, 2 ) As Integer
3. Dim values(4, 3) As Integer 4. Dim values(5, 2) As Integer
ANS: 2.
m) Use a to group RadioButtons on a Form.
1. GroupBox control 2. ComboBox control
3. ListBox control 4. None of the above
ANS: 1.
n) The property is set to True when a RadioButton is selected.
1. Selected 2. Chosen
3. On 4. Checked
ANS: 4.
o) Two-dimensional arrays are often used to represent .
1. a pie chart 2. distances
3. lines 4. tables
ANS: 4.
p) Which of the following statements creates Integer array values with three rows and
three columns?
1. Dim array()() As Integer = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
2. Dim array() As Integer = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
3. Dim array(,) As Integer = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
4. All of the above
ANS: 3.
vbhtp6_07.fm Page 5 Friday, June 28, 2013 10:19 AM

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

If data(i) < smallest Then


smallest = data(i)
End If
Next

7.7 (What Does This Code Do?) This function declares numbers as its parameter. What does it
return?

1 Function Mystery(numbers() As Integer) As Integer()


2 Dim length As Integer = numbers.Length - 1
3 Dim tempArray(length) As Integer
4
5 For i As Integer = length To 0 Step -1
6 tempArray(length - i) = numbers(i)
7 Next
8
9 Return tempArray
10 End Function ' Mystery
vbhtp6_07.fm Page 6 Friday, June 28, 2013 10:19 AM

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.

1 Function Mystery() As Integer()


2 Dim prices(1, 31) As Integer
3
4 prices = GetStockPrices()
5
6 Dim result(30) As Integer
7
8 For i As Integer = 0 To 30
9 result(i) = prices(1, i) - prices(0, i)
10 Next
11
12 Return result
13 End Function ' Mystery

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

Visit TestBankBell.com to get complete for all chapters

You might also like