Download presentation
Presentation is loading. Please wait.
Published byAustin Mitchell Modified over 8 years ago
1
Chapter 8 P 1 Arrays and Grids Single-dimension arrays Definition An array is a sequence of elements all referred to with a common name. Other terms: table, subscripted list Definition The number which denotes the position of an element in the list is the index or subscript of the element. Declaring an array General syntax Dim Arrayname ( UpperBound) As Type Example P. 227
2
Chapter 8 P 2 Write a Visual Basic program to read in numbers to an array and print them out. (1) Declare an array of size 5. (2) Read numbers into a text box and place them into the array. You must keep track of the index when you do this. (3) Write a loop to put the elements of the array into 5 label boxes. Write code to add all the elements of the array. Put the result into a new label. Turn in the code for this program before you leave.
3
Chapter 8 P 3 For Each/Next Statement - traverse an array without having to specify the index - general syntax Dim ArrayName As Type Dim ElementName As Variant For Each ElementName In ArrayName do something Next - Example Dim sEmployee (0 To 99) As String Dim vName As Variant For Each vName in sEmployee vName = “ “ Next vName -Example P. 228 Note: The variable name for the element of the array is variant.
4
Chapter 8 P 4 Initializing an array using For Each Example ‘Set all elements in an array to 0 For Each vNumber In some array vNumber = 0 next vNumber
5
Chapter 8 P 5 Find the largest element in a list In a list of size 3, we have the following logic. Max = A Max < B True False Max = B Max<C True Max = C iMax = A If iMax< B Then iMax = B End If If iMax < C Then iMax = C End If write iMax
6
Chapter 8 P 6 For large lists we need a loop. Write a flowchart to indicate how to find the largest element in a list.
7
Chapter 8 P 7 Max = First element Max < Next Element Max = Next Element There are more elements True False Increment the index of the list False Exit True
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.