Problem Solving 2
Problem Solving 2
a) The number that refers to a particular element of an array is called its _________.
1. value
2. size
4. index
1. units{2}
2. units(2)
3. units[0,2]
4. units[2]
1. ComboData
2. Source
3. DataList
4. DataSource
1. SelectedIndex
2. SelectedValue
3. Index
4. SelectedNumber
f) The process of ordering the elements of an array is called _________ the array.
1. allocating
2. sorting
3. declaring
4. initializing
1. Array(averageRainfall).Sort()
2. Sort.Array(averageRainfall)
3. Sort(averageRainfall)
4. Array.Sort(averageRainfall)
1. single-dimensional
2. two-dimensional
3. double
4. one-dimensional
i) The expression _________ creates an Integer array of two rows and five columns.
1. CheckedChanged
2. Changed
3. SelectedChanged
4. Both 1 and 2
l) Typically, _________ statements are used to iterate over each element in a two-
dimensional array.
1. Do While...Loop
2. nested For...Next
3. Do...Loop Until
7.2 State whether each of the following is true or false. If false, explain why.
7.1 a) 4. b) 2. c) 1. d) 4. e) 1. f) 2. g) 4. h) 2. i) 3. j) 1. k) 3. l) 2.
7.2 a) False. An array can store only values of the same type. b) False. An array index
must be a nonnegative integer or integer expression. c) True. d) False. To determine
the number of elements in an array, we can use the Length property. e) True.
Quick Quiz
1. type Double
2. type Integer
3. type String
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
1. GetUpperBound
2. GetUpperLimit
3. GetHighestIndex
4. GetUpperIndex
1. index
2. zeroth element
3. be empty
1. Array.Sort(words)
2. words.SortArray()
3. Array.Sort(words, 1)
4. Sort(words)
1. DateTimePicker
2. ListBox
3. NumericUpDown
4. Label
1. Elements
2. ArraySize
3. Length
4. Size
1. comma
2. asterisk
3. period
4. apostrophe
1. data
2. rectangular
3. tabular
l) Which of the following creates an Integer array of five rows and three columns?
1. GroupBox control
2. ComboBox control
3. ListBox control
1. Selected
2. Chosen
3. On
4. Checked
1. a pie chart
2. distances
3. lines
4. tables
p) Which of the following statements creates Integer array values with three rows
and three columns?
Exercises
b) Using a For...Next statement, assign the value 8 to each of the five elements of
one-dimensional Integer array values.
e) Determine the smallest and largest values in 99-element floating-point array data.
7.5 (Salary Survey App) Use a one-dimensional array to solve the following
problem: A company pays its salespeople on a commission basis. The salespeople
receive $200 per week, plus 9% of their gross sales for that week. For example, a
salesperson who grosses $5,000 in sales in a week receives $200 plus 9% of $5,000, a
total of $650. Write an app (using an array of counters) that determines how many of
the salespeople earned salaries in each of the following ranges (assuming that each
salesperson’s salary is truncated to an integer amount): $200–299, $300–399, $400–
499, $500–599, $600–699, $700–799, $800–899, $900–999 and over $999.
Allow the user to enter the sales for each employee in a TextBox. The user clicks
the Calculate Button to calculate the salesperson’s salary. When the user is done
entering this information, clicking the Show Totals Button displays how many of the
salespeople earned salaries in each of the above ranges.
7.6 (Cafeteria Survey App) Twenty students were asked to rate, on a scale from 1
to 10, the quality of the food in the student cafeteria, with 1 being “awful” and 10
being “excellent.” Allow the user input to be entered using a ComboBox. Use
an Integer array to store the frequency of each rating. Display the frequencies as a
bar chart in a ListBox.
7.7 (What Does This Code Do?) This function declares numbers as its parameter.
What does it return?
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
7.9 (Road Sign Test App) Write an app that tests the user’s knowledge of road
signs. Your app should display a random sign image and ask the user to select the
sign name from a ComboBox (similar to the Flag Quiz app). You can find the images
in the Images folder with this chapter’s examples.
7.11 (What Does This Code Do?) What is returned by the following code? Assume
that GetStockPrices 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.
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
7.14 (Sorting in Ascending and Descending Order) Modify the sorting example
of Fig. 7.12 so that it displays the “Original values,” “Values sorted in ascending
order” and “Values sorted in descending order”; your app should display in the last
two columns the values sorted in ascending and descending order, respectively.
Write an app that allows the user to enter a seven-digit number in a TextBox, and
displays every possible seven-letter word combination corresponding to that number
in a multiple line scrollable TextBox when the user clicks the Generate
Words button. There are 2,187 (37) such combinations. Avoid phone numbers with
the digits 0 and 1.
7.16 (Airline Reservations System) A small airline has just purchased a computer
for its new automated reservations system. You’ve been asked to develop the new
system. You are to write an app to assign seats on each flight of the airline’s only
plane (capacity: 10 seats).
Your app should display RadioButtons that allow the user to choose First
Class or Economy. If the user selects First Class, your app should assign a seat in
the first-class section (seats 1–5). If the user types Economy, your app should assign
a seat in the economy section (seats 6–10). Your app should then display a boarding
pass indicating the person’s seat number and whether it’s in the first-class or
economy section of the plane.
Use a one-dimensional array of type Boolean to represent the plane’s seating chart.
All the elements of the array are initially False to indicate that the seats are empty. As
each seat is assigned, set the corresponding elements of the array to True to indicate
that the seat is no longer available.
Your app should never assign a seat that has already been assigned. When the
economy section is full, your app should ask the person if it’s acceptable to be placed
in the first-class section (and vice versa). If yes, make the appropriate seat
assignment. If no, display the message "Next flight leaves in 3 hours."
7.17 (Polling) The Internet and the web are enabling more people to
network, join a cause, voice opinions, and so on. The presidential candidates
in 2008 used the Internet intensively to get out their messages and raise
money for their campaigns. In this exercise, you’ll write a simple polling app
that allows users to rate five social-consciousness issues from 1 (least
important) to 10 (most important). Pick five causes that are important to you
(for example, political issues, global environmental issues). Use a one-
dimensional array topics (of type String) to store the five causes. To
summarize the survey responses, use a 5-row, 10-column two-dimensional
array responses (of type Integer), each row corresponding to an element in
the topics array. When the app runs, it should ask the user to rate each issue.
Have your friends and family respond to the survey. Then have the app
display a summary of the results, including:
a) A tabular report with the five topics down the left side and the 10 ratings across the
top, listing in each column the number of ratings received for each topic.
b) To the right of each row, show the average of the ratings for that issue.
c) Which issue received the highest point total? Display both the issue and the point
total.
d) Which issue received the lowest point total? Display both the issue and the point
total.