# Chapter 9 Critical Thinking

Document Sample

```					                                  Chapter 9 Critical Thinking
1.   Write declarations for an array storing 15 test grades (integer values), an array storing
100 prices (decimal values, and an array storing 50 true/false test answers (Boolean
values).
2.   a) What is the purpose of using a ReDim statement?
b) What happens if the keyword Preserve does not follow ReDim?
3.   What are the similarities and differences between Structure declarations and
Enumerated type declarations?
4.   a) Why is it not possible to create a two-dimensional array that stores movie titles in its
first dimension and gross money earned in its second dimension?
b) Write a declaration for a structure named MovieInfo that stores a movie title and a
gross money earned amount.
c) Where must the structure in part (b) be declared in a program?
d) Using the structure declared in part (b), write a declaration for an array named
stcTopActionMovies that stores information for 50 movies.
e) Write an assignment statement for the first element of the stcTopActionMovies array
declared in part (d) so that Star Wars, which grossed \$460,395,655, is stored.
5.   Write an array declaration for:
a) decPrices() which initially stores 1.5, 2.25, 3.15, 4.30.
b) strNames() which initially contains zero elements.
c) intScores() which has 30 rows and 5 columns.
6.   The array intValues can be visualized as:
4       6       2       0
10      9       1       12
a) What will intValues.Length return?
b) What will intValues.Rank return?
c) What will intValues.GetLength(0) return?
d) What will intValues.GetLength(1) return?
e) What is stored at intValues(2,2)?
f) What is stored at intValues(1,2)?
g) What will lstOutput display after the following code segment executes:
For intI = 0 To 3
For intJ = 0 To 1
Next intJ
Next intI
7.   For each of the following, determine the error(s):
a) ‘procedure header which accepts an array declared as (2,3) for an argument
Sub GetName(ByRef strNames(2,3) As String)
b) Dim decCost(7) As Decimal            ‘an array with 7 items’
c) ‘The winer is the last element in an array that holds 20 names
Me.lblOutput.Text = “The winner is “ & strName(20)
d) ‘nested For-Next loop to display the elements of a two-dimensional array
For intRow = 0 To intArray.GetLength(1)
For intCol = 0 To intArray.GetLength(2)
Me.lblArray.Text = Me.lblArray.Text & intArray(intRow, intCol) & “ “
Next intCol
Me.lblArray.Text = Me.lblArray.Text & vbCrLf
Next intRow
e) Private Sub btnPlay_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnPlay.Click
Enum Card
Ace = 1
Deuce = 2
King = “Highest Value”
End Enum
Card.Ace = 14
Dim MyDeck As Card
MyDeck = Card.Ace
…
End Sub
f) ‘uses a month array and a day array
‘ FindNumDays() uses a linear search to return number of days
Dim strMonth() As Strin = {“January”, “February”, “March”, “April”, “May”, “June”,
“July”, “Auguts”, “September”, “October”, “November”, “December”}
Dim strInputMonth As String = InputBox(“Enter name of month to see how many days it
has.”)
If strInputMonth <> Nothing Then
Dim intDaysResult As Ineger = FindNumDays(strInputMonth, strMonth, intDays)
If intDays Result = 0 Then
MessageBox.Show(“Input was not a proper month.”)
Else
MessageBox.Show(strInputMonth & “ has “ & intDaysResult & “ days.”)
End If
Else
MessageBox.Show(“Need to enter name of month.”)
End If

Function FidnNumDays(ByVal strInput As String, ByRef strString As String, ByRef
intNum As Integer)
Dim intI As Integer
For intI = 0 To strString.Length – 1
If strInput.ToUpper = strString(intI) Then
Return intNum
End If
Next intI
End Function

8) a) Show the contents of lstOutput after the following statements execute:
Dim intValues() As Integer = {2, 4, 6, 8}
ReDim Preserve intValues(6)
Dim intI As Integer
For intI =5 To 0 Step – 1
Next intI
b) Is intValues() in part (a) an example of a dynamic array or a nondynamic array?

9) Show the contents of the intGrid array after the following statements execute:
Dim intGrid(3, 2) As Integer
Dim intValue, intI, intJ As Integer
For intI = 0 To 3
For intJ = 0 To 2
intValue = intI + intJ
intGrid(intI, intJ) = intValue
Next intJ
Next intI

10) Write an event handler procedure for the following interface:

The Tag properties of the buttons are assigned the same values as the Text properties of
the buttons. The procedure responds to the clicking of the buttons and uses array
intTotalTimesSelected() to keep track of the number of times each button has been
clicked. Use meaningful array indexes. Fo example, if the 3 button has been clicked 5
times then the value of intTotalTimesselected(3) will be 5.

11) a) Write a function RowSum() that returns the sum of the values in the elements of a
specific row for a two-dimensional array the header is:
Function ColSum(ByRef intArray(,) As Integer, ByVal intRow As Integer) As Integer

b) Write a function ColSum() that returns the sum of the values in the elements of a
specific column for a two-dimensional array. The header is:
Function ColSum(ByRef intArray(,) As Integer, ByVal intCol As Integer) As Integer

c) Write a decision structure that calls RowSum() in part (a) only if the value of the row
number is valid. If the row number is not valid, a message box with an appropriate
message is displayed.
12) Use the following code to answer the questions below:
Enum AnimalType
Bird
Cat
Dog
Fish
End Enum

Structure PetType
Dim Name As String
Dim Animal As AnimalType
End Structure
Dim Pets(9) As PetType
a) What is the result of: Pets(2).Animal = AnimalType.Dog
b) What integer value does Fish correlate to?
c) What will the following code display:
Dim MyPet As AnimalType
For MyPet = AnimalType.Bird To AnimalType.Fish
MessageBox.Show(MyPet)
Next MyPet

13) Assume an interface has seven Label objects. Write statements that create an array of the
Label objects and then sets each to a random number between 1 and 7.

14. Determine if each of the following is true or false. If false, explain why.
a) The length of a one-dimensional array is one more than the index of the last element.
b) An array declaration must have a number in the parentheses.
c) “Index was outside the bounds of the array” is syntax error message that is displayed
when an invalid index is used.
d) An array being passed in a Call statement must have its size indicated in parentheses.
f) Arrays must be passed into procedure by reference.
g) A dynamic array is an array that has been assigned values in its declaration statement.
h) ReDim reinitializes all the elements in an array to Nothing.
i) In a Dim statement, the size of an array must be a positive number.
j) All columns in a two-dimensional array must be of the same type.
k) Structures can have members of different types.
l) To pass a structure requires that each individual member be listed.
m) A linear search looks at one element after another until the desired element is found or
the entire array has been searched.
n) A structure can be declared in the procedure that uses it.
o) The Preserve keyword is used when the programmer wants to make sure the array size
is not changed.
p) An enumerated type cannot be defined in the procedure that uses it.
q) The first member declared in an enumerated type has a default value of 0(zero).
r)An array can contain elements from the application interface that are all the same
control class type.

```
DOCUMENT INFO
Categories:
Tags:
Stats:
 views: 27 posted: 7/30/2012 language: English pages: 4