Microsoft Visual Basic 2010: Reloaded Fourth Edition

Slides:



Advertisements
Similar presentations
1.
Advertisements

Programming with Microsoft Visual Basic 2008 Fourth Edition
Programming with Microsoft Visual Basic th Edition
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Eight Sub and Function Procedures.
Programming with Microsoft Visual Basic 2005, Third Edition
Case, Arrays, and Structures. Summary Slide  Case Structure –Select Case - Numeric Value Example 1 –Select Case - String Value Example  Arrays –Declaring.
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.
Introduction to Programming with C++ Fourth Edition
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Programming Logic and Design Fourth Edition, Comprehensive
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.
Chapter 8: String Manipulation
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Flag Quiz Application Introducing One-Dimensional Arrays and ComboBox es.
Microsoft Visual Basic 2008: Reloaded Fourth Edition
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.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
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.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
Chapter 9: Advanced Array Concepts
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.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Seven More on the Repetition Structure.
Chapter 8 Arrays and Strings
6.3 List Boxes and Loops Some Properties, Methods, and Events of List Boxes List Boxes Populated with Strings List Boxes Populated with Numbers Searching.
Array Processing.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Chapter 8: Arrays.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 17 – Flag Quiz Application Introducing One-Dimensional.
Chapter 6: The Repetition Structure
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Six The Do Loop and List Boxes.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
Computer Programming TCP1224 Chapter 11 Arrays. Objectives Using Arrays Declare and initialize a one-dimensional array Manipulate a one-dimensional array.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 18 – Student Grades Application Introducing.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations.
Chapter 9 Processing Lists with Arrays. Class 9: Arrays Understand the concept of random numbers and how to generate random numbers Describe the similarities.
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 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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files.
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 19 A Ray of Sunshine.
Microsoft Visual Basic 2010: Reloaded Fourth Edition
Microsoft Visual Basic 2008: Reloaded Third Edition
Chapter 6: Using Arrays.
An Introduction to Programming with C++ Sixth Edition
Microsoft Visual Basic 2005: Reloaded Second Edition
CIS 16 Application Development Programming with Visual Basic
CIS16 Application Development and Programming using Visual Basic.net
Arrays Part 2.
Presentation transcript:

Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Nine Arrays

Objectives After studying this chapter, you should be able to: Declare and initialize a one-dimensional array Store data in a one-dimensional array Determine the number of array elements and the highest subscript Traverse a one-dimensional array Code a loop using the For Each…Next statement Compute the total and average of a one-dimensional array’s contents Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Objectives (cont'd.) Find the highest value in a one-dimensional array Associate a list box with a one-dimensional array Use a one-dimensional array as an accumulator Sort a one-dimensional array Create and initialize a two-dimensional array Store data in a two-dimensional array Sum the values in a two-dimensional array Search a two-dimensional array Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Arrays Simple variable (or scalar variable): a variable that is unrelated to any other variable in memory Array: A group of variables with the same name and data type that are related in some way Used to temporarily store related data in memory Increases the efficiency of a program Commonly used arrays: One-dimensional Two-dimensional Microsoft Visual Basic 2010: Reloaded, Fourth Edition

One-Dimensional Arrays Can be viewed as a column of variables Each variable in the array has the same name and data type Subscript: A unique number that identifies each variable in a one-dimensional array Starts at 0 for first element in the array Use array name and subscript to refer to each individual variable in the array Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 9-1: Illustration of the naming convention for the one-dimensional sales array Microsoft Visual Basic 2010: Reloaded, Fourth Edition

One-Dimensional Arrays (cont'd.) Element: an individual variable in the array When an array is declared: Must specify the data type, name, and highest subscript to be used First element has a subscript of 0 The array will contain one element more than the highest subscript because subscripts start at 0 Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 9-2: How to declare a one-dimensional array Microsoft Visual Basic 2010: Reloaded, Fourth Edition

One-Dimensional Arrays (cont'd.) Each element in the array is initialized if no values are provided String array elements are initialized to the keyword Nothing Numeric array elements are initialized to 0 Boolean array elements are initialized to False Date array elements are initialized to 12:00 AM January 1, 0001 Microsoft Visual Basic 2010: Reloaded, Fourth Edition

One-Dimensional Arrays (cont'd.) Initial values can be specified for array elements Populating the array: assigning initial values to an array List the values in a comma-separated list enclosed in curly braces ({}) After declaration, can use an assignment statement to store a value in an array element Length property: indicates number of elements GetUpperBound method: returns the highest subscript Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 9-3: How to store data in a one-dimensional array Microsoft Visual Basic 2010: Reloaded, Fourth Edition

One-Dimensional Arrays (cont'd.) Figure 9-4: How to use a one-dimensional array’s Length property Microsoft Visual Basic 2010: Reloaded, Fourth Edition

One-Dimensional Arrays (cont'd.) Figure 9-5: How to use a one-dimensional array’s GetUpperBound method Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Traversing a One-Dimensional Array Traverse an array: look at each array element, one by one, from beginning to end of the array Use a loop to traverse an array Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 9-6: How to traverse a one-dimensional array Microsoft Visual Basic 2010: Reloaded, Fourth Edition

The For Each…Next Statement Used to code a loop that processes each element in a group or array Creates a variable used to represent each element in the group or array Data type of the element must match the data type of the group Microsoft Visual Basic 2010: Reloaded, Fourth Edition

The For Each…Next Statement (cont'd.) Figure 9-7: How to use the For Each…Next statement Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Starward Coffee – Calculating a Total and an Average Figure 9-9: Problem specification for the Starward Coffee Application Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Starward Coffee – Calculating a Total and an Average (cont’d.) Figure 9-8: Sample run of the Starward Coffee application Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Starward Coffee – Calculating a Total and an Average (cont’d.) Figure 9-10: Partial code for the Starward Coffee application Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 9-10: Partial code for the Starward Coffee application (cont’d Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Starward Coffee – Calculating a Total and an Average (cont’d.) Figure 9-10: Partial code for the Starward Coffee application (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Car-Mart – Finding the Highest Value Figure 9-11: Problem specification for the Car-Mart application Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Car-Mart – Finding the Highest Value (cont’d.) Figure 9-12: Sample run of the Car-Mart application Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 9-13: Get Highest button’s Click event procedure Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Hunt Auditorium – Arrays and Collections Figure 9-14: Problem specification for the Hunt Auditorium application Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Hunt Auditorium – Arrays and Collections (cont'd.) Figure 9-15: Sample run of the Hunt Auditorium application Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Hunt Auditorium – Arrays and Collections (cont'd.) Figure 9-16: Partial code and an illustration for the Hunt Auditorium application Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Hunt Auditorium – Arrays and Collections (cont'd.) Figure 9-15: Partial code and an illustration for the Hunt Auditorium application (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Hinsbrook School – Accumulator Array Figure 9-17: Problem specification for the Hinsbrook School application Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Hinsbrook School – Accumulator Array (cont'd.) Figure 9-18: Sample run of the Hinsbrook School application Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Hinsbrook School – Accumulator Array (cont'd.) Figure 9-19: Partial code for the Hinsbrook School application Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 9-19: Partial code for the Hinsbrook School application (cont’d Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Sorting a One-Dimensional Array Sorting: arranging data in a specific order Ascending: first element is smallest, last element is largest Descending: first element is largest, last element is smallest Array.Sort method: used to sort elements in a one-dimensional array in ascending order Array.Reverse method: used after Array.Sort method to change to descending order Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 9-20: How to use the Array.Sort and Array.Reverse methods Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Two-Dimensional Arrays Resembles a table with rows and columns Each element is identified by a unique combination of two subscripts: (row, column) Subscripts are zero-relative Refer to an element using the name followed by the (row, column) pair in parentheses Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 9-21: Names of some of the variables contained in the two-dimensional orders array Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Two-Dimensional Arrays (cont'd.) Declared with highest row subscript and highest column subscript (zero-relative) Number of rows = highest row subscript + 1 Number of columns = highest column subscript + 1 Can specify initial values for array elements If no initial values are declared, array elements are automatically initialized Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 9-22: How to declare a two-dimensional array Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Two-Dimensional Arrays (cont'd.) Figure 9-23: How to store data in a two-dimensional array Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 9-23: How to store data in a two-dimensional array (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 9-24: How to use a two-dimensional array’s GetUpperBound method Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Traversing a Two-Dimensional Array To traverse a two-dimensional array, use two loops: Outer loop: tracks the row subscript Nested loop: tracks the column subscript Can also use a For Each…Next loop Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Traversing a Two-Dimensional Array (cont'd.) Figure 9-25: How to traverse a two-dimensional array Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 9-25: How to traverse a two-dimensional array (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Jenko Booksellers – Calculating a Total Figure 9-26: Problem specification for the Jenko Booksellers application Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Jenko Booksellers – Calculating a Total (cont’d.) Figure 9-27: Sample run of the Jenko Booksellers application Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Jenko Booksellers – Calculating a Total (cont’d.) Figure 9-28: Calculate button’s Click event procedure Microsoft Visual Basic 2010: Reloaded, Fourth Edition

O’Reilly Studios – Searching a Two-Dimensional Array Figure 9-29: Problem specification for the O’Reilly Studios application Microsoft Visual Basic 2010: Reloaded, Fourth Edition

O’Reilly Studios – Searching a Two-Dimensional Array (cont’d.) Figure 9-30: Sample run of the O’Reilly Studios application Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 9-31: Partial code for the O’Reilly Studios application Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Programming Tutorial 1 Lottery Game application Generates and displays six unique random numbers for a Lottery Game Figure 9-33: MainForm in the Lottery Game application Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Programming Tutorial 2 Stay Fit Health Club application Displays monthly fees for three different membership levels Figure 9-33: MainForm for the Stay Fit Health Club application Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Programming Example Professor Coleman application Allows a user to select a letter grade from a list box Figure 9-43: MainForm in the Professor Coleman application Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Summary Arrays: used to store related data in memory All variables in an array have the same name and data type To declare a one-dimensional array, provide the highest subscript or initial values One-dimensional array: each element is uniquely identified by its position (subscript) in the array Refer to an element in a one-dimensional array by using array name and element’s subscript Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Summary (cont'd.) Length property: returns the number of elements in an array GetUpperBound method: returns the highest subscript in the array Use a loop to traverse a one-dimensional array Can use variables in an array as accumulators Array.Sort method: sorts the elements in a one-dimensional array in ascending order Array.Reverse method: reverses the order of elements in a one-dimensional array Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Summary (cont'd.) Two-dimensional array: each element is uniquely identified by its position (row and column subscripts) in the array To declare a two-dimensional array, provide the highest row and column subscripts or initial values Number of rows in a two-dimensional array is the highest row subscript value + 1 Number of columns in a two-dimensional array is the highest column subscript value + 1 Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Summary (cont'd.) Refer to an element in a two-dimensional array by using array name and element’s row and column subscripts separated by a comma Use a two-dimensional array’s GetUpperBound method to determine the highest row subscript and the highest column subscript in the array Can traverse a two-dimensional array using either two loops (outer loop and nested loop) or the For Each…Next statement Microsoft Visual Basic 2010: Reloaded, Fourth Edition