Top Banner
Arrays Programming Right from the Start with Visual Basic .NET 1/e 4
29

Arrays Programming Right from the Start with Visual Basic.NET 1/e 4.

Dec 31, 2015

Download

Documents

Marsha Preston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Arrays Programming Right from the Start with Visual Basic.NET 1/e 4.

Arrays

Programming Right from the Start with Visual Basic .NET 1/e

4

Page 2: Arrays Programming Right from the Start with Visual Basic.NET 1/e 4.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 2

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

Page 3: Arrays Programming Right from the Start with Visual Basic.NET 1/e 4.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 3

Objectives (cont.)

• Understand how to manipulate and process an array

• Consider multiple problems that benefit from an array solution

• Understand the Bubble Sort algorithm

Page 4: Arrays Programming Right from the Start with Visual Basic.NET 1/e 4.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 4

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.

Page 5: Arrays Programming Right from the Start with Visual Basic.NET 1/e 4.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 5

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.

Page 6: Arrays Programming Right from the Start with Visual Basic.NET 1/e 4.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 6

4-1 Arrays (cont.)

Page 7: Arrays Programming Right from the Start with Visual Basic.NET 1/e 4.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 7

Creating an Array

• In Visual Logic you create, or declare, an array using the Make Array command

Page 8: Arrays Programming Right from the Start with Visual Basic.NET 1/e 4.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 8

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-of-bounds error.

Page 9: Arrays Programming Right from the Start with Visual Basic.NET 1/e 4.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 9

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

Page 10: Arrays Programming Right from the Start with Visual Basic.NET 1/e 4.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 10

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.

Page 11: Arrays Programming Right from the Start with Visual Basic.NET 1/e 4.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 11

Analysis and Design

Page 12: Arrays Programming Right from the Start with Visual Basic.NET 1/e 4.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 12

Analysis and Design (cont.)

Page 13: Arrays Programming Right from the Start with Visual Basic.NET 1/e 4.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 13

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.

Page 14: Arrays Programming Right from the Start with Visual Basic.NET 1/e 4.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 14

Analysis and Design

Page 15: Arrays Programming Right from the Start with Visual Basic.NET 1/e 4.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 15

Analysis and Design (cont.)

Page 16: Arrays Programming Right from the Start with Visual Basic.NET 1/e 4.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 16

Analysis and Design (cont.)

Page 17: Arrays Programming Right from the Start with Visual Basic.NET 1/e 4.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 17

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.

Page 18: Arrays Programming Right from the Start with Visual Basic.NET 1/e 4.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 18

Analysis and Design

Page 19: Arrays Programming Right from the Start with Visual Basic.NET 1/e 4.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 19

Analysis and Design (cont.)

Page 20: Arrays Programming Right from the Start with Visual Basic.NET 1/e 4.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 20

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 one-sixth, or 16.67 percent. What are the percentages after forty rolls? What are the percentages after four hundred rolls?

Page 21: Arrays Programming Right from the Start with Visual Basic.NET 1/e 4.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 21

Analysis and Design

Page 22: Arrays Programming Right from the Start with Visual Basic.NET 1/e 4.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 22

Analysis and Design (cont.)

Page 23: Arrays Programming Right from the Start with Visual Basic.NET 1/e 4.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 23

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.

Page 24: Arrays Programming Right from the Start with Visual Basic.NET 1/e 4.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 24

Analysis and Design

• Parallel arrays are two or more arrays whose elements are related by their position in the arrays.

Page 25: Arrays Programming Right from the Start with Visual Basic.NET 1/e 4.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 25

Analysis and Design (cont.)

Page 26: Arrays Programming Right from the Start with Visual Basic.NET 1/e 4.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 26

Analysis and Design (cont.)

Page 27: Arrays Programming Right from the Start with Visual Basic.NET 1/e 4.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 27

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.

Page 28: Arrays Programming Right from the Start with Visual Basic.NET 1/e 4.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 28

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.

Page 29: Arrays Programming Right from the Start with Visual Basic.NET 1/e 4.

Arrays

Programming Right from the Start with Visual Basic .NET 1/e

4