Presentation Description

Comments

Presentation Transcript

Arrays:

Arrays Visual Basic

Slide 2:

Slide 2 of 48 Topic & Structure of the lesson Introduction One Dimensional Array Sorting an array Searching an array

Learning Outcomes:

Slide 3 of 48 Learning Outcomes At the end of this lecture you will be able to : Declare a one dimensional array Store data into an array Use Option Base Declare an Unbound Array Use Specific Bound Arrays

Key Words:

Slide 4 of 48 Key Words If you have mastered this topic, you should be able to use the following terms correctly in your assignments and tests : Dim Option Base LBound UBound Index

References:

Slide 5 of 48 References An Introduction to Programming Using Visual Basic 6.0 David I Schneider Chapter 7 VB 6 How to Program Deitel and Deitel Chapter 7

Slide 6:

Slide 6 of 48 Definition of Arrays An array is a consecutive group of memory locations that all have the same name and the same type To refer to a particular location or element in the array we specify the array name and the index value

Without Option Base:

Group Exercise:

Slide 11 of 48 Group Exercise Num(4) 2 1 3 6 8 Write a statement to declare the array Write a statement to to print 8 from the array Write a statement to total all elements in the array Write a statement to double each element in the array i.e multiply each element by 2 What is num(0) + num(4) ?

Slide 14:

Slide 14 of 48 Group Exercise I Build a program that constructs an array of 20 integers, in which the elements of the array are initialized to the first 20 even numbers. After the initialization process, multiply all the elements of the array with 3. Print the elements of the array before and after the multiplication using two List Boxes.

Slide 17:

Slide 17 of 48 Exercise II Build a program that constructs an array of 20 integers, initialized with a random number from 1 to 20. Generate another array of integers with the lower bound of 2 and the upper bound of 5. Using this array, find out how many numbers of the 20 integer array are multiples of 2, 3, 4 and 5.

Homework:

Slide 18 of 48 Homework The table below gives names and test scores from a math contest. Write a program to display the names of the students scoring above the average for these eight students. Richard 135 Geraldine 114 James 92 John 91 Paul 150 Max 114 Robert 91 Barbara 124

Slide 19:

Slide 19 of 48 Visual Basic Arrays Procedures, Searching and Sorting

Slide 20:

Slide 20 of 48 Learning Outcomes At the end of this lecture you will be able to : Write a program Using LBound and UBound statements Pass an array to a sub procedure Sort an array using bubble sort Search an array using linear search

Slide 21:

Slide 21 of 48 Key Words If you have mastered this topic, you should be able to use the following terms correctly in your assignments and exams : Dim LBound UBound RND Sort Search

Slide 23:

Slide 23 of 48 Group Exercise Build a program that constructs an array of 20 integers, initialized with a random number from 1 to 20. Create another array of integers with the lower bound of 2 and the upper bound of 5. Using this array, find out how many numbers of the 20 integer array are multiples of 2, 3, 4 and 5.

Slide 26:

Slide 26 of 48 Passing Arrays to Procedures For a procedure to receive an array through a call, the parameter list must specify that an array will be received. For example: Private Sub ProcessArray(x() As Integer) The following call passes array Months to the above procedure: Call ProcessArray(Months()) Remember: Arrays are passed by reference, not value!

Slide 27:

Slide 27 of 48 Example of an Array Program using Procedures Dim num(1 To 30) As Integer Dim y As Integer Private Sub Form_Load() Call InitializeArray(num()) Call UpdateValues(num()) End Sub

Group Exercise:

Slide 33:

Slide 33 of 48 Searching Arrays There are two ways to search through an array: Linear Search Binary Search Compares each element with the search key Eliminates searching through redundant arrays by comparing the search key with the middle element (array must be sorted)

Group Exercise:

Slide 35 of 48 Group Exercise Write a program to store 10 random numbers into an array called Num Accept a value from the user using an InputBox Search the array to find the value If the number is found display using MsgBox “Number Found” otherwise display “Not Found”

Slide 37:

Slide 37 of 48 Program Using Binary Search (cont) If (key < a(middle) Then high = middle – 1 Else low = middle + 1 End If End If Loop Print “Not found”

Slide 38:

Slide 38 of 48 Group Exercise The table below gives names and test scores from a math contest. 1. Write a program to store the names and marks into an array 2. Display the names of the students scoring above the average mark Richard 135 Geraldine 114 James 92 John 91 Paul 150 Max 114 Robert 91 Barbara 124

Slide 41:

Slide 42:

Slide 42 of 48 Option explicit ‘store values into a two dimensional array Dim n(1 to 2, 1 to 3) As Integer Private Sub Command1_Click() For i = 1 To 2 For j = 1 To 3 n(i, j) = Val(InputBox(“Enter Num" & “Row” & i & “Col” & j)) Next j Next i ‘display contents of the array to listbox For i = 1 To 2 For j = 1 To 3 List1.AddItem n(i, j) Next j Next i End Sub Multi-Dimensional Array

Exercise:

Slide 43 of 48 Exercise The scores for the top three golfers at the 1999 golf tournament are shown in table below Round 1 2 3 4 Tiger Woods 70 66 65 69 Tom Kite 77 69 66 70 Tommy Tolles 72 72 72 67 Write a procedure to enter the data to an array Write a program to compute the total score for each player

Solution:

Slide 44 of 48 Solution Dim nom(1 to 3) as string,score (1 to 3, 1 to 4) as integer Private sub command1_click() Dim player, round,total as integer For player = 1 to 3 nom(player) = inputbox(“enter name”) for round 1 = 1 to 4 nom(player,round) = val(inputbox(“enter score”)) next round Next player ‘compute score for each payer For player = 1 to 3 let total = 0 For round = 1 to 4 let total = total + score(player,round) Next round Pic1.print “The total score for” ; nom(player); “is’’; total Next player

Homework:

Slide 46 of 48 Homework Write a program to store random numbers from 1 to 10 into a one dimensional array and determine the largest number

Slide 47:

Slide 47 of 48 Homework 100 students were asked to rate the quality of a particular lecturer in their class. They give a rating on a scale of 1 to 10 (1 being terrible and 10 being excellent). Place the 100 responses in an array of integers and summarize the results of the poll. Note: The responses are to be randomized.