Array
Array
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.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
7.3 Answer each of the following questions:
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
i) When declaring an array, a(n) _________ is required inside parentheses in order to indicate that the
array is two-dimensional.
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
o) Two-dimensional arrays are often used to represent _________.
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
7.4 Write statements to accomplish each of the following tasks:
b) Using a For...Next statement, assign the value 8 to each of the five elements of one-dimensional
Integer array values.
d) Copy 11-element array source into the first portion of 34-element array sourceCopy.
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.10 (Enhanced Cafeteria Survey App) A school cafeteria is giving an electronic survey to its students
to improve their lunch menu. Create an app that uses a rectangular array to store votes for the
survey. The app should display four food items in a ComboBox. Provide Like and Dislike RadioButtons
to allow students to indicate whether they like or dislike a particular food. The user selects an item
from the ComboBox, clicks the appropriate RadioButton and presses the Submit Button to submit the
vote. Display a report that’s updated as each new set of responses is entered. Use a rectangular
Integer array named votes, with four rows and two columns to summarize the results. Each row
corresponds to a food item in the ComboBox. The columns store the number of “like” and “dislike”
votes, respectively.
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.
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.13 (Duplicate Elimination) Use a one-dimensional array to solve the following problem: Read in 20
numbers, each of which is between 10 and 100, inclusive. As each number is read, display it in
numbersEnteredListBox and, if it’s not a duplicate of a number already read, display it in
uniqueValuesListBox. Provide for the “worst case” (in which all 20 numbers are different). Use the
smallest possible array to solve this problem.
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.
7.15 (Telephone-Number Word Generator) Standard telephone keypads contain the digits zero
through nine. The numbers two through nine each have three letters associated with them (Fig. 7.29).
Many people find it difficult to memorize phone numbers, so they use the correspondence between
digits and letters to develop seven-letter words that correspond to their phone numbers. For
example, a person whose telephone number is 686-2377 might use the correspondence indicated in
Fig. 7.29 to develop the seven-letter word “NUMBERS.” Every seven-letter word corresponds to
exactly one seven-digit telephone number. A restaurant wishing to increase its takeout business could
surely do so with the number 825-3688 (that is, “TAKEOUT”).
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."
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.