Programming with Microsoft Visual Basic 2005, Third Edition

Slides:



Advertisements
Similar presentations
1.
Advertisements

Chapter 6: The Repetition Structure
Programming with Microsoft Visual Basic 2008 Fourth Edition
Programming with Microsoft Visual Basic th Edition
Chapter 3: Using Variables and Constants Programming with Microsoft Visual Basic 2005, Third Edition.
Microsoft Visual Basic: Reloaded Chapter Seven More on the Repetition Structure.
Chapter 11: Classes and Objects
Chapter 7: Sub and Function Procedures
Microsoft Visual Basic 2010: Reloaded Fourth Edition
Chapter 12: Using ADO.NET 2.0 Programming with Microsoft Visual Basic 2005, Third Edition.
An Introduction to Programming with C++ Fifth Edition
Arrays-Part 1. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 8 Arrays.
Arrays.
Chapter 7: Sub and Function Procedures
Introduction to Programming with C++ Fourth Edition
Chapter 9 Introduction to Arrays
Chapter Three Using Variables and Constants Programming with Microsoft Visual Basic th Edition.
Chapter 8: String Manipulation
Programming with Microsoft Visual Basic th Edition
Programming with Microsoft Visual Basic th Edition CHAPTER SEVEN SUB AND FUNCTION PROCEDURES.
Programming with Microsoft Visual Basic 2012 Chapter 7: Sub and Function Procedures.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.
Programming with Microsoft Visual Basic th Edition CHAPTER THREE USING VARIABLES AND CONSTANTS.
Programming with Microsoft Visual Basic 2012 Chapter 13: Working with Access Databases and LINQ.
Chapter 4: The Selection Structure
1 Microsoft Visual Basic 2010 Arrays. 2 Using a One-Dimensional Array Lesson A Objectives After completing this lesson, you will be able to:  Declare.
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Six Repeating Program Instructions.
Using Arrays and File Handling
Array Processing Simple Program Design Third Edition A Step-by-Step Approach 7.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.
Programming with Microsoft Visual Basic 2008 Fourth Edition
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Seven More on the Repetition Structure.
Chapter 10: Structures and Sequential Access Files
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
Array Processing.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Working with option button, check box, and list box controls Visual Basic for Applications 13.
Chapter 6: Arrays: Lists and Tables
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
Chapter 6: The Repetition Structure
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Six The Do Loop and List Boxes.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
Chapter 8: Manipulating Strings
Chapter Thirteen Working with Access Databases and LINQ Programming with Microsoft Visual Basic th Edition.
Programming with Microsoft Visual Basic th Edition
An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays.
Computer Programming TCP1224 Chapter 11 Arrays. Objectives Using Arrays Declare and initialize a one-dimensional array Manipulate a one-dimensional array.
1.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 18 – Student Grades Application Introducing.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
Lab 6 (2) Arrays ► Lab 5 (1) Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Three Using Variables and Constants.
Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.
Programming with Microsoft Visual Basic th Edition
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Ten Structures and Sequential Access Files.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
An Introduction to Programming with C++ Sixth Edition Chapter 12 Two-Dimensional Arrays.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 19 A Ray of Sunshine.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 6 Looping and Multiple Forms.
Microsoft Visual Basic 2010: Reloaded Fourth Edition
An Introduction to Programming with C++ Sixth Edition
Microsoft Visual Basic 2005: Reloaded Second Edition
Tutorial 11 Arrays Tutorial 11: Arrays.
CIS16 Application Development and Programming using Visual Basic.net
Arrays Part 2.
Presentation transcript:

Programming with Microsoft Visual Basic 2005, Third Edition 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 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 Array Lesson 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) Array Reasons to use arrays 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 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

Figure 9-10: Pseudocode for the xDisplayButton’s Click event procedure 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

Figure 9-20: Code for the xHighButton’s Click event procedure 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 Arrays Lesson 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 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 Arrays Lesson 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 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

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 Programming with Microsoft Visual Basic 2005, Third Edition