Download presentation
Presentation is loading. Please wait.
Published byVincent Thomas Modified over 8 years ago
1
Arrays Chapter 8
2
Chapter 8 - Part 12 Variable and Variable Array Variable Stores only one value Variable Array Variable that has 1 symbolic name but holds many different items of the same type
3
Chapter 8 - Part 13 Variable Example Compute the average rate of return for a group of 10 companies. Display the names of companies with rates that exceed the average. Adapted from Burrows Visual Basic.NET book.
4
Chapter 8 - Part 14
5
5 Problems Very time-consuming coding—especially if have 10, 300, 900 companies Almost duplicate code Can’t use loop because each value is stored in a different variable; each variable has a different name. Using a loop would overwrite previous variable contents.
6
Chapter 8 - Part 16 Array One variable that acts like a group of variables same name same data type Individual items accessed through subscript (or index) Numeric identification of specific element within an array 0 assigned to first element Last element’s subscript # is always -1 of total # of elements
7
Chapter 8 - Part 17 Array Declaration Declare Dim, Static, or Public UpperSubscript Positive number Highest subscript (element #) in array 1 st element subscript = 0 Last element subscript = 10 Total elements: 110, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 Numeric array elements initialized to 0 upon declaration
8
Chapter 8 - Part 18 Dim intSales(4) As Integer intSales(0)0 intSales(1)0 intSales(2)0 intSales(3)0 intSales(4)0 decSales sub zero decSales sub one decSales sub two decSales sub three decSales sub four
9
Chapter 8 - Part 19 Implicit Array Sizing/Initialization No upper subscript explicitly set Array size specified implicitly, based on initialization list { 2…12} Can’t provide both an upper subscript & an initialization list (error occurs)
10
Chapter 8 - Part 110 Using Named Constants Const intUPPER_SUB As Integer = 100 Dim intArray(intUPPER_SUB) As Integer intUPPER_SUB is a constant. Use the constant to specify the upper subscript of the array declaration. Upper subscript is 100 101 total elements (starting w/ subscript 0)
11
Chapter 8 - Part 111 Checkpoint Do 8.1, 8.2, and 8.3 on p. 485
12
Chapter 8 - Part 112 Checkpoint Answers a. Dim intEmpNums(99) As Integer c. Dim intMiles() As Integer = {10, 20, 30, 40, 50} d. Dim strNames(12) As String
13
Chapter 8 - Part 113 Basic Array Population/Assignment decSales(0) = 500 decSales(1) = 750 decSales(2) = 100 decSales(3) = 1000 decSales(4) = 210
14
Chapter 8 - Part 114 Array w/ 5 Elements 0500 1750 2100 32000 4210 decSales Array name Element numbers Elements
15
Chapter 8 - Part 115 decSales(0)500 decSales(1)750 decSales(2)100 decSales(3)2000 decSales(4)210 decSales sub zero decSales sub one
16
Chapter 8 - Part 116 Populating an Array One of first steps is to populate the array (i.e., enter data into the array). Population Methods via Loop: User input boxes Another array Database table Can’t simply copy all values from one array to another without looping.
17
Chapter 8 - Part 117 Assignment Via For…Next Loop 'Declare array w/ 10 elements Dim intSeries(9) as Integer 'Declare counter variable Dim intCount As Integer 'Assigns 100 to each element in array For intCount = 0 To 9 intSeries(intCount) = 100 Next intCount
18
Chapter 8 - Part 118 Populate through InputBox For intCount = 0 To 9 intSeries(intCount) = Val(InputBox(“Enter a number”) Next intCount
19
Chapter 8 - Part 119 Assignment Via Do While Loop 'Declare array w/ 10 elements Dim intSeries(9) as Integer 'Declare counter variable Dim intCount As Integer 'Assigns 100 to each element in array Do While intCount < 10 intSeries(intCount) = 100 intCount = intCount + 1 Loop
20
Chapter 8 - Part 120 Array Bounds Checking Does not allow a statement to use a subscript element number that is outside the range of elements for an array. Won’t allow intSeries(10) = 100 because the declaration was Dim intSeries(9) as Integer. Occurs at Run Time; no error message at Design Time. See another example on p. 481. See dialog box error message on p. 482.
21
Chapter 8 - Part 121 For Each…Next (p. 482) Special loop designed to access values from arrays. ElementVar Previously declared variable Variable to represent an element in array during an iteration. Same data type as the array Array Name of the array
22
Chapter 8 - Part 122 Checkpoint 8.5 and 8.6 Answers Dim intCount As Integer For intCount = 0 To 25 MessageBox.Show(intPoints(intCount).ToString) Next intCount Dim intNumber As Integer For Each intNumber In intPoints MessageBox.Show(intNumber.ToString) Next intNumber
23
Chapter 8 - Part 123 Learning Activity Interface List Box (lstOutput) Button (btnCalcPay) Button (btnExit) Declarations Constant for pay rate = $6 Array to store hours worked for six employees Counter variable Pay variable Populate array with For…Next loop
24
Chapter 8 - Part 124 Loop to Populate intHours Array
25
Chapter 8 - Part 125 Calculate & Output Gross Pay
26
Chapter 8 - Part 126 Copying Array Contents Arrays are object variables—holding memory addresses Can’t do this: Dim intOldValues(5) as Integer Dim intNewValues(5) as Integer intNewValues = intOldValues Causes intNewValues to refer to same array as intOldValues
27
Chapter 8 - Part 127 Instead… Use a loop to copy element to element For intCount = 0 to 5 intNewValue(intCount) = intOldValue(intCount) Next intCount Maintains two separate arrays intOldVale() intNewValue()
28
Chapter 8 - Part 128 Parallel Arrays Two or more arrays that hold related data (sometimes of unlike data types) Dim strName(3) as String Dim decSales(3) as Decimal Access related elements in each array with common subscript
29
Chapter 8 - Part 129 Parallel Array Example First company’s name is stored in strName(0), and the first company’s sales value is stored in decSales(0). strName(0)strName(1)strName(2)strName(3) ABC Inc.L2 Inc.XYZ TiredFOG Inc. 7505001000210 decSales(0)decSales(1)decSales(2)decSales(3)
30
Chapter 8 - Part 130 Example: Populating Parallel Arrays Dim intCount As Integer Dim strNames(4) As String Dim intAges(4) As Integer For intCount = 0 to 5 strNames(intCount) = InputBox() intAges(intCount) = InputBox() Next intCount
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.