510 likes | 692 Views
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
E N D
Chapter 9: Arrays Programming 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 Programming 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 Programming 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 Programming 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 Programming 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 Programming 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 Programming 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 Programming with Microsoft Visual Basic 2005, Third Edition
One-Dimensional Arrays (continued) Figure 9-3: Syntax versions and examples of declaring a one-dimensional array Programming 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 Programming 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 Programming 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 Programming 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 Programming 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 Programming 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 Programming 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 Programming 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 Programming 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 Programming with Microsoft Visual Basic 2005, Third Edition
Searching a One-Dimensional Array (continued) Figure 9-14: Code for the xSearchButton’s Click event procedure Programming 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 Programming 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 Programming 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 Programming 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 Programming 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 Programming 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 Programming 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 Programming 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 Programming 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 Programming 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 Programming 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 Programming 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 Programming 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 Programming with Microsoft Visual Basic 2005, Third Edition
Parallel One-Dimensional Arrays (continued) Figure 9-28: Sample run of the Price List application Programming with Microsoft Visual Basic 2005, Third Edition
Parallel One-Dimensional Arrays (continued) Figure 9-29: Pseudocode for the xDisplayButton’s Click event procedure Programming 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 Programming 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 Programming 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 Programming 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 Programming 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 Programming 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 Programming 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 Programming 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 Programming 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 Programming 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 Programming with Microsoft Visual Basic 2005, Third Edition
The Perrytown Gift Shop (continued) Figure 9-40: Interface for Perrytown Gift Shop application Programming with Microsoft Visual Basic 2005, Third Edition
The Perrytown Gift Shop (continued) Figure 9-42: Code to declare and initialize the two-dimensional arrays Programming 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 Programming 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 Programming 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 Programming 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 Programming with Microsoft Visual Basic 2005, Third Edition