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
Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall
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
Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall
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.
Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall
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.
Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall
5
4-1 Arrays (cont.)
Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall
6
Creating an Array
• In Visual Logic you create, or declare, an
array using the Make Array command
Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall
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.
Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall
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
Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall
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.
Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall
10
Analysis and
Design
Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall
11
Analysis and Design (cont.)
Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall
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.
Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall
13
Analysis and
Design
Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall
14
Analysis and
Design (cont.)
Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall
15
Analysis and Design (cont.)
Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall
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.
Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall
17
Analysis and
Design
Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall
18
Analysis and Design (cont.)
Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall
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?
Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall
20
Analysis and
Design
Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall
21
Analysis and Design (cont.)
Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall
22
4-6 Parallel Arrays
(Girl Scout Cookies)
• 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.
Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall
23
Analysis and Design
• Parallel arrays are two or more arrays
whose elements are related by their position
in the arrays.
Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall
24
Analysis and
Design (cont.)
Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall
25
Analysis and Design (cont.)
Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall
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.
Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall
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.
Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall
28
4
Arrays
Programming Right from the Start
with Visual Basic .NET 1/e
Descargar

Click to Add Title - Tarleton State University