arrays chapter microsoft visual basic.net: reloaded 1

53
Arrays Chapter Microsoft Visual Basic .NET: Reloaded 1

Upload: tobias-fenney

Post on 28-Mar-2015

226 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

Arrays

Chapter Microsoft Visual Basic .NET: Reloaded

1

Page 2: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

2Microsoft Visual Basic .NET: Reloaded

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: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

3Microsoft Visual Basic .NET: Reloaded

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: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

4Microsoft Visual Basic .NET: Reloaded

Objectives (continued)

• Enter code in the form’s Load event procedure

• Create and manipulate parallel one-dimensional arrays

• Create and initialize a two-dimensional array

• Store data in a two-dimensional array

• Search a two-dimensional array

Page 5: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

5Microsoft Visual Basic .NET: Reloaded

Using Arrays

• Simple variable

• Also called scalar variable

• Is unrelated to any other variable in memory

• Array

• A group of variables that have the same name and data type and are related in some way

• You can picture an array as a group of small adjacent boxes inside the computer’s memory

Page 6: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

6Microsoft Visual Basic .NET: Reloaded

One-Dimensional Arrays

• One-dimensional array

• Visualize as a column of variables

• Each variable is identified by a unique number

• Unique number is called “subscript”

• Subscript is placed inside parentheses immediately following array name

• Example strStates(1) – read “strStates sub one”

• Individual variables referred to as “elements”

Page 7: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

7Microsoft Visual Basic .NET: Reloaded

One-Dimensional Arrays (continued)

• Declare using keywords Dim or Private

• highestSubscript argument in declaration used to specify the number of elements in the array

• number of array elements is 1 more than the value in highestSubscript argument

Page 8: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

8Microsoft Visual Basic .NET: Reloaded

One-Dimensional Arrays (continued)

Page 9: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

9Microsoft Visual Basic .NET: Reloaded

HOW TO…

Page 10: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

10Microsoft Visual Basic .NET: Reloaded

Storing Data in a One-Dimensional Array

Page 11: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

11Microsoft Visual Basic .NET: Reloaded

Manipulating One-Dimensional Arrays

• Sample procedures will demonstrate how to perform tasks using a one-dimensional array• Display contents of an array

• Access an array element using its subscript

• Search the array

• Calculate the average of the data stored in a numeric array

• Find the highest value stored in an array

• Update the array elements

• Sort the array elements

Page 12: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

12Microsoft Visual Basic .NET: Reloaded

Displaying the Contents of a One-Dimensional Array

Page 13: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

13Microsoft Visual Basic .NET: Reloaded

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

Page 14: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

14Microsoft Visual Basic .NET: Reloaded

The For Each…Next Statement

• Use to code a loop containing instructions to be processed for each element in a group

• Syntax template shown in next slide contains words “element” and “group”• “element”

• Declare a variable of the same data type as the elements of the array and place this variable in the “element” position of the template

• “group” • The variable name of the array

Page 15: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

15Microsoft Visual Basic .NET: Reloaded

HOW TO…

Page 16: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

16Microsoft Visual Basic .NET: Reloaded

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

Display salary amount corresponding to code entered by user

Page 17: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

17Microsoft Visual Basic .NET: Reloaded

Searching a One-Dimensional Array

• Determine number of salespeople selling above a certain amount

Page 18: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

18Microsoft Visual Basic .NET: Reloaded

Calculating the Average Amount Stored in a One-Dimensional Numeric Array

• Calculate and display average test score earned by students on final exam

Page 19: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

19Microsoft Visual Basic .NET: Reloaded

Determining the Highest Value Stored in a One-Dimensional Array

• Display highest amount earned in a week

Page 20: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

20Microsoft Visual Basic .NET: Reloaded

Updating the Values Stored in a One-Dimensional Array

• Procedure allows manager to increase price of each item and display item’s new price

Page 21: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

21Microsoft Visual Basic .NET: Reloaded

Sorting the Data Stored in a One-Dimensional Array

• Array.Sort(arrayName) method

• Sorts the elements of a one-dimensional array in “ascending order”

• First element contains smallest value

• Last element contains largest value

• To sort in descending order

• First sort in ascending order

• Then use Array.Reverse(arrayname) method to reverse the position of the elements in the array

Page 22: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

22Microsoft Visual Basic .NET: Reloaded

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

• btnSortAscending_Click procedure sorts intNumbers array into ascending order

Page 23: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

23Microsoft Visual Basic .NET: Reloaded

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

Page 24: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

24Microsoft Visual Basic .NET: Reloaded

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

• Method sorts array in descending order and displays the results

Page 25: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

25Microsoft Visual Basic .NET: Reloaded

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

Page 26: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

26Microsoft Visual Basic .NET: Reloaded

Using a Module-Level One-Dimensional Array

• Application displays names contained in a sequential access file, giving user choice of ascending or descending order of display

• First, declare array variable in the Form’s general declarations section

• Then, fill array from data in file using code in the form’s Load event procedure

• Load event occurs before the first time a form is displayed

Page 27: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

27Microsoft Visual Basic .NET: Reloaded

Using a Module-Level One-Dimensional Array (continued)

Page 28: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

28Microsoft Visual Basic .NET: Reloaded

Using a Module-Level One-Dimensional Array (continued)

Page 29: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

29Microsoft Visual Basic .NET: Reloaded

Parallel One-Dimensional Arrays

• Two or more one-dimensional arrays whose elements are related by their position (subscript)

• Called parallel because element at subscript 0 in first array corresponds to element at subscript 0 in another array

• Example:

• strID(0) corresponds to intPrice(0) and

• strID(1) corresponds to intPrice(1),

• and so on…

Page 30: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

30Microsoft Visual Basic .NET: Reloaded

Parallel One-Dimensional Arrays (continued)

Page 31: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

31Microsoft Visual Basic .NET: Reloaded

Parallel One-Dimensional Arrays (continued)

Page 32: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

32Microsoft Visual Basic .NET: Reloaded

Parallel One-Dimensional Arrays (continued)

Page 33: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

33Microsoft Visual Basic .NET: Reloaded

Two-Dimensional Arrays

• Resemble a table in that variables are store in rows and columns

• Each element is identified by a unique combination of two subscripts

• The first subscript indicates the element’s row

• The second subscript indicates the column

• Each subscript begins with 0

• The element in the first row and column has a subscript of (0,0)

Page 34: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

34Microsoft Visual Basic .NET: Reloaded

Two-Dimensional Arrays (continued)

Page 35: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

35Microsoft Visual Basic .NET: Reloaded

HOW TO…

Page 36: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

36Microsoft Visual Basic .NET: Reloaded

Storing Data in a Two-Dimensional Array

Page 37: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

37Microsoft Visual Basic .NET: Reloaded

Searching a Two-Dimensional Array

Page 38: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

38Microsoft Visual Basic .NET: Reloaded

Programming Example – Perrytown Gift Shop Application

• Application is to assist in employee weekly payroll process

• Calculates and displays the federal withholding tax (FWT)

• Gets and displays taxable wages

• Gets and displays marital status

Page 39: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

39Microsoft Visual Basic .NET: Reloaded

TOE Chart

Page 40: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

40Microsoft Visual Basic .NET: Reloaded

User Interface

Page 41: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

41Microsoft Visual Basic .NET: Reloaded

Objects, Properties, and Settings

Page 42: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

42Microsoft Visual Basic .NET: Reloaded

Objects, Properties, and Settings (continued)

Page 43: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

43Microsoft Visual Basic .NET: Reloaded

Tab Order

Page 44: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

44Microsoft Visual Basic .NET: Reloaded

Pseudocode

btnExit Click event procedure

close application

btnCalc Click event procedure

Include the following in a Try/Catch block:

remove any dollar signs and spaces from the txtTaxable control

assign the taxable wages store in txtTaxable to a variable

assign the marital status entered in txtStatus to a variable

if marital status is not either M or S

display an appropriate message

else

if marital status is M

use the Married tax table, stored in an array

else

use the singles tax table, stored in an array

Page 45: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

45Microsoft Visual Basic .NET: Reloaded

Pseudocode (continued)

end ifend if

Repeat while there are still rows in the tax table to search and taxable wages have not been foundif taxable wages are less than or equal to value stored in the first column of the current row in tax table

use information stored in 2nd, 3rd, and 4th columns in taxtable to calculate the federal withholding taxindicate that the taxable wages were found by assigning

thevalue True to blnFound variable

elseadd 1 to the contents of the intRow variable to continue

the searchrow in the tax table

Display the federal withholding tax in the lblFwt controlUse general Catch statement to handle and display any errors

Page 46: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

46Microsoft Visual Basic .NET: Reloaded

Code

Page 47: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

47Microsoft Visual Basic .NET: Reloaded

Code (continued)

Page 48: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

48Microsoft Visual Basic .NET: Reloaded

Summary

• Programmers use arrays to temporarily store related data in computer’s internal memory

• All variables in an array have the same name and data type

• Each element in a one-dimensional array is identified by a unique subscript

• Appears in parentheses after array’s name

Page 49: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

49Microsoft Visual Basic .NET: Reloaded

Summary (continued)

• Each element in a two-dimensional array is identified by a unique combination of two subscripts:• Row subscript

• Column subscript

• Subscripts appear in parentheses after the array’s name

• You list the row subscript first, followed by a comma and the column subscript

• The first subscript in a one-dimensional array is 0 (zero)

Page 50: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

50Microsoft Visual Basic .NET: Reloaded

Summary (continued)

• First row subscript in a two-dimensional array is 0 (zero) likewise, the first column subscript also is 0 (zero)

• When declaring a one-dimensional array, provide either highest subscript or initial values

• When declaring a two-dimensional array, you provide either the highest row and column subscripts or the initial values

Page 51: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

51Microsoft Visual Basic .NET: Reloaded

Summary (continued)

• The number of elements in a one-dimensional array is one more than its highest subscript

• The number of rows in a two-dimensional array is one more than its highest row subscript likewise, the number of columns is one more than its highest column subscript

• You usually use an assignment statement to store data in an array

• You refer to an element in a one-dimensional array using the array’s name followed by the element’s subscript

Page 52: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

52Microsoft Visual Basic .NET: Reloaded

Summary (continued)

• You refer to an element in a two-dimensional array using the array’s name followed by the element’s row and column subscripts, which are separated by a comma

• You can use the For Each…Next statement to code a loop whose instructions you want processed for each element in an array

• You can use the Length property to determine the number of elements in the array

Page 53: Arrays Chapter Microsoft Visual Basic.NET: Reloaded 1

53Microsoft Visual Basic .NET: Reloaded

Summary (continued)

• You use the Array.Sort method to sort the elements in a one-dimensional array

• You use the Array.Reverse method to reverse the order of the elements in an array

• The elements in parallel arrays are related by their subscript (or position) in each array