```4
Arrays
Programming Right from the Start
with Visual Basic .NET 1/e
Objectives
• Understand the concept of an array
• Be able to declare arrays of various sizes
• Be able to populate and access the elements
in an array
2
Objectives (cont.)
• Understand how to manipulate and process
an array
• Consider multiple problems that benefit
from an array solution
• Understand the Bubble Sort algorithm
3
4-1 Arrays
• An array is a variable that holds a
collection of related data values.
• Each of the values in an array is called
an element.
• Each element in the array is identified by
an integer value called its index, which
indicates the position of the element in
the array.
4
4-1 Arrays (cont.)
• Most modern programming languages
implement zero-based arrays, meaning
that array index values begin with 0.
• The array length is the number of
elements in the array.
• The upper bound of an array is the index
of the last element.
5
4-1 Arrays (cont.)
6
Creating an Array
• In Visual Logic you create, or declare, an
array using the Make Array command
7
Accessing Individual Elements
of an Array
• To access individual array elements, you
specify the array name and follow it with
an index expression enclosed in
parentheses.
• If you attempt to reference an array with an
index value greater than the upper bound of
the array, the result will be an out-ofbounds error.
8
When to Use Arrays
• Arrays are useful…
– when you are storing or processing large
amounts of related data
– when information must be stored and
processed twice
9
4-2 Above Average Problem
(Mutual Funds)
• The Problem
– Jim is a financial analyst with many large
investment clients. Each year Jim identifies ten
different mutual funds that he shares with his
clients for investing. At the end of each year he
keeps the funds that performed better than the
ten-fund average and replaces the others with
new funds.
10
Analysis and
Design
11
Analysis and Design (cont.)
12
4-3 Largest Value Problem
(Highest GPA)
• The Problem
– The Alpha Beta Gamma fraternity is one of
many popular Greek organizations on campus.
Every semester, ABΓ recognizes the graduating
brother who has the highest GPA. The number
of graduates changes from semester to
semester.
13
Analysis and
Design
14
Analysis and
Design (cont.)
15
Analysis and Design (cont.)
16
4-4 Working with Index Values
(Two-Week Totals)
• The Problem
– Anthony owns a small business and plans to
install new inventory hardware and software
during the upcoming year. The installation
process will require two weeks. To minimize
the disruption that will occur during the
migration, Anthony wants to deploy the system
during the two weeks that had the lowest
consecutive two-week total gross sales during
the previous year.
17
Analysis and
Design
18
Analysis and Design (cont.)
19
4-5 Simulation (Die Roll)
• The Problem
– A balanced die is equally likely to roll a 1, 2, 3,
4, 5, or 6. If a balanced die is rolled many
times, the roll values should be evenly
distributed. As the number of rolls increases,
the distributions should become closer to onesixth, or 16.67 percent. What are the
percentages after forty rolls? What are the
percentages after four hundred rolls?
20
Analysis and
Design
21
Analysis and Design (cont.)
22
4-6 Parallel Arrays
• The Problem
– Every spring you look forward to buying a box
of Caramel deLites Girl Scout cookies from
your niece, Belinda. Her troop gives an award
to the girl who sells the most boxes of cookies
each year. They are looking to develop a
software solution to assist in determining the
annual award winner.
23
Analysis and Design
• Parallel arrays are two or more arrays
whose elements are related by their position
in the arrays.
24
Analysis and
Design (cont.)
25
Analysis and Design (cont.)
26
Chapter Summary
• An array is a variable that holds a
collection of related data values.
• Most modern programming languages
implement zero-based arrays.
• To access individual array elements, you
specify the array name and follow it with
an index expression enclosed in
parentheses.
27
Chapter Summary (cont.)
• Arrays contain a finite number of elements,
each of which is referenced by a unique
index.
• Parallel arrays are two or more arrays
whose elements are related by their
positions in the arrays.
• Bubble Sort is a simple sorting technique
involving multiple passes through the array.
28
4
Arrays
Programming Right from the Start
with Visual Basic .NET 1/e
```