0% found this document useful (0 votes)
15 views

Array

Uploaded by

Maaz qureshi 1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Array

Uploaded by

Maaz qureshi 1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

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

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]

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

d) Property _________ specifies the source of the data displayed in a ComboBox.

1. ComboData

2. Source

3. DataList

4. DataSource

e) ComboBox property _________ is 0 when the first ComboBox item is selected.

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

g) Which of the following sorts array averageRainfall?

1. Array(averageRainfall).Sort()

2. Sort.Array(averageRainfall)

3. Sort(averageRainfall)

4. Array.Sort(averageRainfall)

h) Arrays that use two indices are referred to as _________ arrays.

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. 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

j) The _________ event is raised when a RadioButton is either selected or deselected.

1. CheckedChanged

2. Changed

3. SelectedChanged

4. None of the above

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

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

4. nested Do...Loop While

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.

b) An array index normally should be of type Double.

c) Method GetUpperBound returns the highest numbered index in an array.

d) To determine the number of elements in an array, use the NumberOfElements property.

e) The linear search works well for unsorted arrays.

Answers to Self-Review Exercises


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
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

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

c) Method _________ returns an array’s highest index.

1. GetUpperBound

2. GetUpperLimit

3. GetHighestIndex
4. GetUpperIndex

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

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

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)

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

1. DateTimePicker

2. ListBox

3. NumericUpDown

4. Label

h) Property _________ contains the size of an array.

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

j) In a _________ array each row contains the same number of columns.

1. data

2. rectangular

3. tabular

4. All of the above

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

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

m) Use a _________ to group RadioButtons on a Form.

1. GroupBox control

2. ComboBox control

3. ListBox control

4. None of the above

n) The _________ property is set to True when a RadioButton is selected.

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?

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

Exercises
7.4 Write statements to accomplish each of the following tasks:

a) Display the value of element 6 of array numbers.

b) Using a For...Next statement, assign the value 8 to each of the five elements of one-dimensional
Integer array values.

c) Total the 100 elements of floating-point array results.

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?

Click here to view code image

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

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:

Click here to view code image

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.

Click here to view code image

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
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.

Click here to view code image

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”).

Fig. 7.29. Telephone keypad digits and letters.

Every seven-letter phone number corresponds to many different seven-letter combinations.


Unfortunately, most of these represent unrecognizable juxtapositions of letters. It’s possible,
however, that the owner of a barbershop would be pleased to know that the shop’s telephone
number, 424-7288, corresponds to “HAIRCUT.” A veterinarian with the phone number 738-2273
would be pleased to know that the number corresponds to the letters “PETCARE.” An automotive
dealership would be pleased to know that the dealership number, 639-2277, corresponds to “NEW-
CARS.”

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."

Making a Difference Exercise


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.

You might also like