chapter 9: arrays

51
Chapter 9: Arrays Programming with Microsoft Visual Basic 2005, Third Edition

Upload: diata

Post on 22-Jan-2016

64 views

Category:

Documents


0 download

DESCRIPTION

Chapter 9: Arrays. Programming with Microsoft Visual Basic 2005, Third Edition. Using a One-Dimensional Array Lesson A Objectives. Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 9: Arrays

Chapter 9: Arrays

Programming with Microsoft Visual Basic 2005, Third Edition

Page 2: Chapter 9: Arrays

2Programming with Microsoft Visual Basic 2005, Third Edition

Using a One-Dimensional ArrayLesson A Objectives

• Declare and initialize a one-dimensional array

• Store data in a one-dimensional array

• Display the contents of a one-dimensional array

• Code a loop using the For Each…Next statement

• Access an element in a one-dimensional array

Page 3: Chapter 9: Arrays

3Programming with Microsoft Visual Basic 2005, Third Edition

Using a One-Dimensional ArrayLesson A Objectives (continued)

• Search a one-dimensional array

• Compute the average of a one-dimensional array’s contents

• Find the highest entry in a one-dimensional array

• Update the contents of a one-dimensional array

• Sort a one-dimensional array

Page 4: Chapter 9: Arrays

4Programming with Microsoft Visual Basic 2005, Third Edition

Previewing the Completed Application

• Go to Run command on Windows Start menu

• Browse to the VB2005\Chap09 folder

• Open the Perrytown.exe file

• Perrytown Gift Shop user interface appears

Page 5: Chapter 9: Arrays

5Programming with Microsoft Visual Basic 2005, Third Edition

Previewing the Completed Application (continued)

Figure 9-1: FWT for a married employee with taxable wages of $288.46

Page 6: Chapter 9: Arrays

6Programming with Microsoft Visual Basic 2005, Third Edition

Using Arrays

• Simple (scalar variable)

– One unrelated to any other variable in memory

• Array

– Group of variables

– Group members have same name and data type

• Reasons to use arrays

– Simplifies process of coding an application

– Increases run-time efficiency of a program

Page 7: Chapter 9: Arrays

7Programming with Microsoft Visual Basic 2005, Third Edition

One-Dimensional Arrays

• One-dimensional array

– Sequence of contiguous memory cells

– Visualized as a column of variables

• Subscript: integer identifying an array variable

• Refer to array variable by array name and subscript

– Example: states(0) is the first variable in states array

• There are two ways to declare arrays

Page 8: Chapter 9: Arrays

8Programming with Microsoft Visual Basic 2005, Third Edition

One-Dimensional Arrays (continued)

Figure 9-2: Names of the variables in a one-dimensional array named states

Page 9: Chapter 9: Arrays

9Programming with Microsoft Visual Basic 2005, Third Edition

One-Dimensional Arrays (continued)

Figure 9-3: Syntax versions and examples of declaring a one-dimensional array

Page 10: Chapter 9: Arrays

10Programming with Microsoft Visual Basic 2005, Third Edition

One-Dimensional Arrays (continued)

• Element: refers to an individual array variable

• “Off by one” issue: first element has a subscript of 0

• “Off by one” issue in Syntax-Version 1

– Size of new array exceeds highestSubscript by 1

• Populating an array: assigning initial values

• “Off by one” issue in Syntax-Version 2

– Highest subscript < number of initialValues by 1

Page 11: Chapter 9: Arrays

11Programming with Microsoft Visual Basic 2005, Third Edition

Storing Data in a One-Dimensional Array

• Most common way to enter data into an array

– Use an assignment statement

• Syntax: arrayname(subscript) = value

• Examples

– cities(0) = “Madrid” cities(1) = “Paris” cities(2) = “Rome”

– Assigns three strings to the cities array

Page 12: Chapter 9: Arrays

12Programming with Microsoft Visual Basic 2005, Third Edition

Manipulating One-Dimensional Arrays

• Tasks performed with a one-dimensional array

– Display the contents of an array

– Access an array element using its subscript

– Search the array

– Calculate average of data stored in a numeric array

– Find the highest value stored in an array

– Update the array elements

– Sort the array elements

Page 13: Chapter 9: Arrays

13Programming with Microsoft Visual Basic 2005, Third Edition

Displaying the Contents of a One-Dimensional Array

• Refer to the Months application

• MainForm’s Load event procedure uses arrays

– Array named months declared with initial values

– Array values transferred to list box in For…Next loop

– First item in list box selected as default display value

Page 14: Chapter 9: Arrays

14Programming with Microsoft Visual Basic 2005, Third Edition

Displaying the Contents of a One-Dimensional Array (continued)

Figure 9-7: Code for the MainForm’s Load event procedure

Page 15: Chapter 9: Arrays

15Programming with Microsoft Visual Basic 2005, Third Edition

The For Each…Next Statement

• Used to traverse an entire array

• Convenience factor relative to the For…Next loop– Handles coding of starting and ending subscripts– Programmer relieved of using extra array notation

• Example– For Each monthName As String In months

Me.xMonthListBox.Items.Add(monthName) Next monthName

Page 16: Chapter 9: Arrays

16Programming with Microsoft Visual Basic 2005, Third Edition

Using the Subscript to Access an Element in a One-Dimensional Array

• Salary code application used by XYZ Corporation

– Managers are classified by salary codes 1 through 6

– Salary code is entered by the user

– Salary linked to code is displayed with button click

• xDisplayButton’s Click event procedure

– Initializes an array to store salary values

– Uses input salary code to access array elements

Page 17: Chapter 9: Arrays

17Programming with Microsoft Visual Basic 2005, Third Edition

Using the Subscript to Access an Element in a One-Dimensional Array

(continued)

Figure 9-10: Pseudocode for the xDisplayButton’s Click event procedure

Page 18: Chapter 9: Arrays

18Programming with Microsoft Visual Basic 2005, Third Edition

Searching a One-Dimensional Array

• Application for sales manager at Jacobsen Motors

– Sales manager enters a sales amount

– Sales manager presses Search button

– Number of salespeople selling > amount are displayed

• xSearchButton’s Click event procedure

– Searches an array for values > input sales amount

– Displays number of sales amounts > input amount

Page 19: Chapter 9: Arrays

19Programming with Microsoft Visual Basic 2005, Third Edition

Searching a One-Dimensional Array (continued)

Figure 9-14: Code for the xSearchButton’s Click event procedure

Page 20: Chapter 9: Arrays

20Programming with Microsoft Visual Basic 2005, Third Edition

Calculating the Average Amount Stored in a One-Dimensional

Numeric Array• Application ordered by Professor Jeremiah

– Calculates and displays average test scores

• xCalcAvgButton’s Click event procedure

– Adds the test scores stored in an array

– Divides the total from step 1 by length of array

– Displays the average test score

• Syntax of Length property: arrayname.Length

Page 21: Chapter 9: Arrays

21Programming with Microsoft Visual Basic 2005, Third Edition

Calculating the Average Amount Stored in a One-Dimensional

Numeric Array (continued)

Figure 9-17: Code for the xCalcButton’s Click event procedure

Page 22: Chapter 9: Arrays

22Programming with Microsoft Visual Basic 2005, Third Edition

Determining the Highest Value Stored in a One-Dimensional Array

• Application ordered by Sharon Johnson

– Displays highest amount she has earned in a week

• xHighestButton’s Click event procedure

– Searches the array, looking for the highest amount

– Displays the highest amount

Page 23: Chapter 9: Arrays

23Programming with Microsoft Visual Basic 2005, Third Edition

Determining the Highest Value Stored in a One-Dimensional Array

(continued)

Figure 9-20: Code for the xHighButton’s Click event procedure

Page 24: Chapter 9: Arrays

24Programming with Microsoft Visual Basic 2005, Third Edition

Updating the Values Stored in a One-Dimensional Array

• Application for sales manager at Jillian Company

– Used to raise price of each item the company sells

– Displays each item’s new price in a label control

• xUpdateButton’s Click event procedure

– Stores original prices in an array

– Retrieves amount of increase input to text box

– Adds amount of increase to each array element

– Displays each new value stored in the array

Page 25: Chapter 9: Arrays

25Programming with Microsoft Visual Basic 2005, Third Edition

Updating the Values Stored in a One-Dimensional Array (continued)

Figure 9-23: Code for the xUpdateButton’s Click event procedure

Page 26: Chapter 9: Arrays

26Programming with Microsoft Visual Basic 2005, Third Edition

Sorting the Data Stored in a One-Dimensional Array

• Sorting: arranging data in a specific order

• Array.Sort method

– Sort elements of 1-D array in ascending order

– Syntax: Array.Sort(arrayname)

• Sorting an array in descending order

– First use Array.sort to sort in ascending order

– Then use Array.reverse to reverse array elements

Page 27: Chapter 9: Arrays

27Programming with Microsoft Visual Basic 2005, Third Edition

Sorting the Data Stored in a One-Dimensional Array (continued)

Figure 9-24: Sample run of the State application

Page 28: Chapter 9: Arrays

28Programming with Microsoft Visual Basic 2005, Third Edition

Summary – Lesson A

• An array groups variables with the same data type under one name

• An individual array variable is also called an element

• To refer to an array element, use the array’s name followed by the element’s subscript

• Arrays may be declared with or without a list of initial values

• Values can be assigned to an array after declaration

Page 29: Chapter 9: Arrays

29Programming with Microsoft Visual Basic 2005, Third Edition

Summary – Lesson A (continued)

• The Length property of an array returns array size

• Traverse an array using a For…Next or For Each…Next statement

• Array.Sort: sorts elements in ascending order

• Array.Reverse: reverses the order of array elements

Page 30: Chapter 9: Arrays

30Programming with Microsoft Visual Basic 2005, Third Edition

Parallel One-Dimensional ArraysLesson B Objectives

• Create parallel one-dimensional arrays

• Locate information in two parallel one-dimensional arrays

Page 31: Chapter 9: Arrays

31Programming with Microsoft Visual Basic 2005, Third Edition

Using Parallel One-Dimensional Arrays

• Parallel one-dimensional arrays

– Two or more arrays whose elements are correlated

• Scenario involving two parallel arrays

– Parallel arrays are named ids and prices

– Each ids element corresponds to a prices element

– To view an item price, search ids array for item ID

– If ID exists, access and display price in prices array

Page 32: Chapter 9: Arrays

32Programming with Microsoft Visual Basic 2005, Third Edition

Parallel One-Dimensional Arrays (continued)

Figure 9-27: Illustration of a price list stored in two one-dimensional arrays

Page 33: Chapter 9: Arrays

33Programming with Microsoft Visual Basic 2005, Third Edition

Parallel One-Dimensional Arrays (continued)

Figure 9-28: Sample run of the Price List application

Page 34: Chapter 9: Arrays

34Programming with Microsoft Visual Basic 2005, Third Edition

Parallel One-Dimensional Arrays (continued)

Figure 9-29: Pseudocode for the xDisplayButton’s Click event procedure

Page 35: Chapter 9: Arrays

35Programming with Microsoft Visual Basic 2005, Third Edition

Summary – Lesson B

• The elements in parallel one-dimensional arrays are correlated

• To create parallel one-dimensional arrays, create two one-dimensional arrays with same size

Page 36: Chapter 9: Arrays

36Programming with Microsoft Visual Basic 2005, Third Edition

Two-Dimensional ArraysLesson C Objectives

• Create and initialize a two-dimensional array

• Store data in a two-dimensional array

• Search a two-dimensional array

• Determine the highest and lowest subscript in a two-dimensional array

Page 37: Chapter 9: Arrays

37Programming with Microsoft Visual Basic 2005, Third Edition

Using Two-Dimensional Arrays

• Two-dimensional array

– Stores variables (elements) in rows and columns

– Resembles a table

• How to identify an two-dimensional array element

– Use a unique combination of two subscripts

– Subscripts specify element’s row and column position

– Example: products(1,2) refers to row two, column three

Page 38: Chapter 9: Arrays

38Programming with Microsoft Visual Basic 2005, Third Edition

Using Two-Dimensional Arrays (continued)

Figure 9-34: Names of some of the variables contained in the products array

Page 39: Chapter 9: Arrays

39Programming with Microsoft Visual Basic 2005, Third Edition

Using Two-Dimensional Arrays (continued)

Figure 9-35: Syntax versions and examples of declaring a two-dimensional array

Page 40: Chapter 9: Arrays

40Programming with Microsoft Visual Basic 2005, Third Edition

Storing Data in a Two-Dimensional Array

• Assign values using an assignment statement

• Assignment statement syntax– arrayname(rowSubscript, columnSubscript) = value– rowSubscript: horizontal subscript – columnSubscript: vertical subscript

Page 41: Chapter 9: Arrays

41Programming with Microsoft Visual Basic 2005, Third Edition

Storing Data in a Two-Dimensional Array (continued)

Figure 9-36: Syntax and examples of assignment statements used to enter data into a two-dimensional array

Page 42: Chapter 9: Arrays

42Programming with Microsoft Visual Basic 2005, Third Edition

Searching a Two-Dimensional Array

• Two-dimensional arrays versus parallel arrays– Both can represent data in tabular format – Two-dimensional arrays are easier to code, use, read

• New version of application for Takoda Tapahe– One two-dimensional array stores price list – 2-D array replaces two parallel arrays in first version– xDisplayButton Click event procedure reflects change

Page 43: Chapter 9: Arrays

43Programming with Microsoft Visual Basic 2005, Third Edition

Searching a Two-Dimensional Array (continued)

Figure 9-38: Modified pseudocode for the xDisplayButton’s Click event procedure

Page 44: Chapter 9: Arrays

44Programming with Microsoft Visual Basic 2005, Third Edition

The Perrytown Gift Shop Application

• Application for Perrytown Gift Shop

– Ordered by the owner, John Blackfeather

– Used to calculate weekly federal withholding tax

• Requirements of application

– Allow user to enter taxable wages in text box

– Enable user to specify marital status

– Given complete input, calculate and display tax

Page 45: Chapter 9: Arrays

45Programming with Microsoft Visual Basic 2005, Third Edition

The Perrytown Gift Shop (continued)

Figure 9-40: Interface for Perrytown Gift Shop application

Page 46: Chapter 9: Arrays

46Programming with Microsoft Visual Basic 2005, Third Edition

The Perrytown Gift Shop (continued)

Figure 9-42: Code to declare and initialize the two-dimensional arrays

Page 47: Chapter 9: Arrays

47Programming with Microsoft Visual Basic 2005, Third Edition

Coding the xCalcButton Click Event Procedure

• Main tasks for xCalcButton Click event procedure– Calculate the federal withholding tax (FWT) – Display the calculated amount in the xFwtLabel

Page 48: Chapter 9: Arrays

48Programming with Microsoft Visual Basic 2005, Third Edition

Coding the xCalcButton Click Event Procedure (continued)

Figure 9-44: Pseudocode for the xCalcButton’s Click event procedure

Page 49: Chapter 9: Arrays

49Programming with Microsoft Visual Basic 2005, Third Edition

The GetUpperBound And GetLowerBound Methods

• GetUpperBound method – Returns integer indicating the highest subscript– Syntax: arrayname.GetUpperBound(dimension)

• GetLowerBound method– Returns integer indicating the lowest subscript– Syntax:arrayname.GetLowerBound(dimension)

• dimension argument in a two-dimensional array– 0 represents the row dimension– 1 represents the column dimension

Page 50: Chapter 9: Arrays

50Programming with Microsoft Visual Basic 2005, Third Edition

The GetUpperBound And GetLowerBound Methods (continued)

Figure 9-46: Syntax and examples of the GetUpperBound and GetLowerBound methods

Page 51: Chapter 9: Arrays

51Programming with Microsoft Visual Basic 2005, Third Edition

Summary – Lesson C

• Two-dimensional arrays store data in rows and columns

• Visualize a two-dimensional array as a table

• Refer to an element in a two-dimensional array using row and column subscripts

• GetUpperBound method returns highest subscript

• GetLowerBound method returns lowest subscript