Chapter 8 P 1 Arrays and Grids Singledimension

  • Slides: 7
Download presentation
Chapter 8 P 1 Arrays and Grids Single-dimension arrays Definition An array is a

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 (<Lower bound To> Upper. Bound) As Type Example P. 227

Chapter 8 P 2 Write a Visual Basic program to read in numbers to

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.

Chapter 8 P 3 For Each/Next Statement - traverse an array without having to

Chapter 8 P 3 For Each/Next Statement - traverse an array without having to specify the index - general syntax Dim Array. Name As Type Dim Element. Name As Variant For Each Element. Name In Array. Name do something Next <Element. Name> - Example Dim s. Employee (0 To 99) As String Dim v. Name As Variant For Each v. Name in s. Employee v. Name = “ “ Next v. Name -Example P. 228 Note: The variable name for the element of the array is variant.

Chapter 8 P 4 Initializing an array using For Each Example ‘Set all elements

Chapter 8 P 4 Initializing an array using For Each Example ‘Set all elements in an array to 0 For Each v. Number In some array v. Number = 0 next v. Number

Chapter 8 P 5 Find the largest element in a list In a list

Chapter 8 P 5 Find the largest element in a list In a list of size 3, we have the following logic. i. Max = A True Max < B False If i. Max< B Then i. Max = B End If Max = B True Max = C Max<C If i. Max < C Then i. Max = C End If write i. Max

Chapter 8 P 6 For large lists we need a loop. Write a flowchart

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.

Chapter 8 P 7 Max = First element True There are more elements False

Chapter 8 P 7 Max = First element True There are more elements False Exit True Max < Next Element False Max = Next Element Increment the index of the list