arrays. objectives declare and initialize a one-dimensional array store data in a one-dimensional...
Post on 21-Dec-2015
230 views
TRANSCRIPT
![Page 1: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/1.jpg)
Arrays
![Page 2: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/2.jpg)
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
• 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 3: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/3.jpg)
Objectives (cont’d)
• Create and manipulate parallel one-dimensional arrays
• Create a structure
• Declare a structure variable
• Create and manipulate a one-dimensional array of structures
• Create and initialize a two-dimensional array
• Store data in a two-dimensional array
• Search a two-dimensional array
![Page 4: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/4.jpg)
Arrays
• A simple or scalar variable is one that is unrelated to any other variable in memory
• An array is a group of variables that have the same name and data type and are related in some way
![Page 5: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/5.jpg)
Arrays (continued)
• The most commonly used arrays are one-dimensional and two-dimensional
• Programmers use arrays to store related data in the internal memory of the computer
![Page 6: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/6.jpg)
One-Dimensional Arrays
• A one-dimensional array is simply a row (or column) of variables
• Each element in an array is identified by a subscript, which Visual Basic .NET assigns to the variable when the array is created
![Page 7: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/7.jpg)
One-Dimensional Arrays (continued)
• You refer to each variable in an array by its name and the variable’s subscript
Names of the variables in a one-dimensional array named states
![Page 8: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/8.jpg)
One-Dimensional Arrays (continued)
• Declaring a one-dimensional array
– Version 1
{Dim | Private} arrayname(highestSubscript) As datatype
– Version 2
{Dim | Private} arrayname() As datatype = {initialValues}
![Page 9: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/9.jpg)
One-Dimensional Arrays (continued)
• Examples of declaring an array
– Dim cities(3) As String
– Private states() As String = {“Hawaii”, “Alaska”, “Maine”}
![Page 10: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/10.jpg)
Storing Data in a One-Dimensional Array
• In most cases, you use an assignment statement to enter data into an existing array
• Syntax: arrayname(subscript) = value
• Examples
– cities(0) = “Madrid”
– cities(1) = “Paris”
– cities(2) = “Rome”
![Page 11: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/11.jpg)
Manipulating One-Dimensional Arrays
• You will learn how to perform the following tasks using a one-dimensional array:
– Display the 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
![Page 12: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/12.jpg)
Manipulating One-Dimensional Arrays (continued)
• You will learn how to perform the following tasks using a one-dimensional array (continued):
– Find the highest value stored in an array
– Update the array elements
– Sort the array elements
![Page 13: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/13.jpg)
Displaying the Contents of a One-Dimensional Array
• uiDisplayButton’s Click event procedure
– Demonstrates how you can display the contents of an array in a label control
– Uses the For…Next statement to display each array element
– You also could use the Do…Loop statement or the For Each…Next statement
![Page 14: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/14.jpg)
Displaying the Contents of a One-Dimensional Array
(continued)
uiDisplayButton’s Click event procedure
![Page 15: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/15.jpg)
The For Each…Next Statement
Syntax and an example of the For Each…Next statement
![Page 16: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/16.jpg)
Using the Subscript to Access an Element in a One-
Dimensional Array• XYZ Corporation pays its managers based
on six different salary codes, 1 through 6
• Each code corresponds to a different salary
• uiSalaryButton’s Click event procedure displays the salary corresponding to the code entered by the user
![Page 17: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/17.jpg)
Searching a One-Dimensional Array
• The sales manager at Jacobsen Motors wants a procedure that allows him to determine the number of salespeople selling above a certain amount, which he will enter
• uiSearchButton’s Click event procedure searches the array, looking for values that are greater than the amount entered by the sales manager
![Page 18: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/18.jpg)
Calculating the Average Amount Stored in a One-Dimensional
Numeric Array• uiCalcAvgButton’s Click event procedure
calculates and displays the average test score
uiCalcAvgButton’s Click event procedure
![Page 19: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/19.jpg)
Calculating the Average Amount Stored in a One-Dimensional
Numeric Array (continued)
uiCalcAvgButton’s Click event procedure (continued)
![Page 20: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/20.jpg)
Determining the Highest Value Stored in a One-Dimensional
Array• Sharon Johnson wants a procedure that
displays the highest amount she has earned in a week
• uiHighestButton’s Click event procedure will search the array, looking for the highest amount
![Page 21: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/21.jpg)
Updating the Values Stored in a One-Dimensional Array
• The sales manager at Jillian Company wants a procedure that:
– Allows her to increase the price of each item the company sells
– Displays each item’s new price in the uiNewPricesLabel control
• uiUpdateButton’s Click event procedure performs these tasks
![Page 22: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/22.jpg)
Sorting the Data Stored in a One-Dimensional Array
• Arranging data in a specific order is called sorting
• Array.Sort method
– Can be used to sort the elements in a one-dimensional array in ascending order
– Syntax: Array.Sort(arrayname)
• uiSortButton’s Click event procedure uses the Array.Sort method to sort the numbers array in ascending order
![Page 23: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/23.jpg)
Sorting the Data Stored in a One-Dimensional Array
(continued)• To sort a one-dimensional array in
descending order:
– Use Array.Sort to sort the array in ascending order
– Use Array.Reverse to reverse the array elements
• Syntax of the Array.Reverse method: Array.Reverse(arrayname)
![Page 24: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/24.jpg)
Using a Module-Level One-Dimensional Array
• Names application
– Needs to display the names contained in a sequential access file
– Should give the user the choice of displaying the names in either ascending or descending order
• The names array is declared in the form’s Declarations section, making it a module-level array
![Page 25: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/25.jpg)
Parallel One-Dimensional Arrays
• Arrays that are related by an element’s position (subscript)
• Searching one array gives you the subscript for the other array
• To store a price list, which includes a string and a number, you can use two one-dimensional arrays
– A String array to store the product IDs
– An Integer array to store the prices
![Page 26: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/26.jpg)
Parallel One-Dimensional Arrays (continued)
Illustration of a price list stored in two one-dimensional arrays
![Page 27: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/27.jpg)
Structures
• Structure statement: can be used to create your own data types in Visual Basic .NET
• Data types created using the Structure statement are referred to as user-defined data types or structures
• Members included in the structure can be variables, constants, or procedures
• In most cases, members are variables
![Page 28: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/28.jpg)
Structures (continued)
Syntax and an example of the Structure statement
![Page 29: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/29.jpg)
Using a Structure to Declare a Variable
• Variables declared using a structure are often referred to as structure variables
Syntax and an example of declaring a structure variable
![Page 30: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/30.jpg)
Using a Structure to Declare a Variable (continued)
Syntax and examples of storing data in a member variable
![Page 31: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/31.jpg)
Creating an Array of Structure Variables
• Assigning initial values to an array is referred to as populating the array
• Refer to a member variable in an array element using the syntax:
arrayname(subscript).memberVariableName
![Page 32: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/32.jpg)
Two-Dimensional Arrays
• A two-dimensional array resembles a table in that the variables are in rows and columns
Illustration of a two-dimensional array
![Page 33: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/33.jpg)
Two-Dimensional Arrays (continued)
• Each variable (element) in a two-dimensional array is identified by a unique combination of two subscripts
• The subscripts specify the variable’s row and column position in the array
• Refer to each variable in a two-dimensional array by the array’s name and the row and column subscripts
![Page 34: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/34.jpg)
Two-Dimensional Arrays (continued)
Syntax versions and examples of declaring a two-dimensional array
![Page 35: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/35.jpg)
Storing Data in a Two-Dimensional Array
Syntax and examples of entering data into a two-dimensional array
![Page 36: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/36.jpg)
Searching a Two-Dimensional Array
uiDisplayPriceButton’s Click event procedure using a two-dimensional array
![Page 37: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/37.jpg)
Searching a Two-Dimensional Array (continued)
uiDisplayPriceButton’s Click event procedure using a two-dimensional array (continued)
![Page 38: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/38.jpg)
The Tax Calculator Application
• John Blackfeather, the owner and manager of the Perrytown Gift Shop, should be able to use the application to calculate the weekly federal withholding tax for his employees
• To calculate the federal withholding tax, the user would need to enter the taxable wages in the Taxable wages text box and then click the Calculate Tax button
![Page 39: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/39.jpg)
The Tax Calculator Application (continued)
Interface for the Tax Calculator application
![Page 40: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/40.jpg)
The Tax Calculator Application (continued)
TOE chart for the Tax Calculator application
![Page 41: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/41.jpg)
Coding the uiCalculateButton Click Event Procedure
Pseudocode for the uiCalculateButton’s Click event procedure
![Page 42: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/42.jpg)
Coding the uiCalculateButton Click Event Procedure
(continued)
Pseudocode for the uiCalculateButton’s Click event procedure (continued)
![Page 43: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/43.jpg)
Summary
• Two versions of the syntax used to declare a one-dimensional array:
– {Dim | Private} arrayname(highestSubscript) As datatype
– {Dim | Private} arrayname() As datatype = {initialValues}
![Page 44: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/44.jpg)
Summary (continued)
• To refer to a variable in an array, use the array’s name followed by the variable’s subscript
• To create parallel one-dimensional arrays, create two one-dimensional arrays
• To create an array of structures, use the Structure statement to create a record structure, then use the record structure to declare the array
![Page 45: Arrays. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d565503460f94a34a4c/html5/thumbnails/45.jpg)
Summary (continued)
• Two versions of the syntax used to declare a two-dimensional array:
– {Dim | Private} arrayname(highestRowSubscript, highestColumnSubscript) As datatype
– {Dim | Private} arrayname(,) As datatype = {{initialValues}, {initialValues},…{initialValues}}
• Syntax used to refer to a variable included in a two-dimensional array:arrayname(rowSubscript, columnSubscript)