0% found this document useful (0 votes)
186 views40 pages

Lec 10 Arrays

The document discusses arrays in Visual Basic .NET, including how to declare and populate one-dimensional arrays, sort and search array elements, and use multidimensional arrays. It provides learning outcomes, key terms, and examples of declaring, initializing, passing to procedures, and manipulating array data, such as converting elements, counting frequencies, and finding averages. The examples are accompanied by exercises for students to practice working with arrays.

Uploaded by

Yovin Lekamge
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
186 views40 pages

Lec 10 Arrays

The document discusses arrays in Visual Basic .NET, including how to declare and populate one-dimensional arrays, sort and search array elements, and use multidimensional arrays. It provides learning outcomes, key terms, and examples of declaring, initializing, passing to procedures, and manipulating array data, such as converting elements, counting frequencies, and finding averages. The examples are accompanied by exercises for students to practice working with arrays.

Uploaded by

Yovin Lekamge
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 40

Visual Basic .

Net
AAPP00-8-3-2

Arrays
Lecture 10
Topic & Structure of the lesson
•Introduction
•One Dimensional array
•Sorting an array
•Searching an array
•Multidimensional array

AAPP00-8-3-2 VBN Visual Basic .Net Slide 2 (of 49)


Learning Outcomes

At the end of this lecture you will be able to :


1. Declare a one dimensional array
2. Store data into an array
3. Pass an array to a sub procedure
4. Sort an array using bubble sort
5. Search an array using linear search

AAPP00-8-3-2 VBN Visual Basic .Net Slide 3 (of 49)


Key Words

If you have mastered this topic, you should be


able to use the following terms correctly in your
assignments and tests:
1. Dim
2. Index
3. Sort
4. Search
5. Rnd

AAPP00-8-3-2 VBN Visual Basic .Net Slide 4 (of 49)


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

AAPP00-8-3-2 VBN Visual Basic .Net Slide 5 (of 49)


How do arrays work?
Numbers(0) 56
Name of Array
(all the same) Numbers(1) 34 The first
array
Numbers(2) 12 element
has an
Numbers(3) 32 index of 0.
Dim Numbers(5) (In VB6
Numbers(4) 45 you could
change the
Numbers(5) 23 lower
bound. In
VB2005/8,
it cannot
Only the index number makes
be
the difference
changed.)

AAPP00-8-3-2 VBN Visual Basic .Net Slide 6 (of 49)


Declaring Arrays

Arrays start at 0 so the following declares


an array of 11 elements.

Dim num(10) As Integer

AAPP00-8-3-2 VBN Visual Basic .Net Slide 7 (of 49)


Example
Dim n(5) as Integer
Private Sub Command1_Click()
For i = 0 To 5
n(i) = i
Next
End Sub

012345 Six Elements

AAPP00-8-3-2 VBN Visual Basic .Net Slide 9 (of 49)


Exercise 1
age(4) 1. Write a statement to declare the array
2
2 Write a statement to print 8 from the array

1 3 Write a statement to total all elements in the


array. Hint: you will need a loop.
3 4 Write a statement to double each element in the
array i.e. multiply each element by 2
6 5 What is age(0) +age(4) ?

AAPP00-8-3-2 VBN Visual Basic .Net Slide 10 (of 49)


Example
Dim num(5) As Integer
Private Sub Command1_Click()
‘convert negative numbers to positive
For x = 0 To 5
If num(x) < 0 Then
num(x) = -1 * num(x)
End If
Next x
End Sub

AAPP00-8-3-2 VBN Visual Basic .Net Slide 11 (of 49)


Example
Dim frequency(6), i, dieNo As Integer

‘count the number of times each number is ‘rolled’


For i = 1 To 1000
dieNo = 1 + Int(Rnd() * 6)
frequency(dieNo) = frequency(dieNo) + 1
Next
MessageBox.Show( “These are the results of the die roll: ”)
For i = 1 To 6
MsgBox (“No. “ & i & “ was rolled “
& frequency(x) & “ times”)
Next

AAPP00-8-3-2 VBN Visual Basic .Net Slide 12 (of 49)


Exercise 2

Write 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 by 3.
Print the elements of the array before and after the
multiplication using 2 List Boxes.

AAPP00-8-3-2 VBN Visual Basic .Net Slide 13 (of 49)


Solution
Dim evenArray(20) As Integer
Private Sub Command1_Click()
Dim evenNumber, i as Integer
evenNumber = 2
For i = 1 To 20
evenArray(i) = evenNumber
List1.AddItem evenArray(i)
evenNumber = evenNumber + 2
Next
For i = 1 To 20
evenArray(i) = evenArray(i) * 3
List2.AddItem evenArray(i)
Next
End Sub
AAPP00-8-3-2 VBN Visual Basic .Net Slide 14 (of 49)
Example of String Arrays
Arrays can contain any data type, including strings
Dim Cars(2) As String
Dim chosenCar As Integer
Cars(0) = “Proton Satria”
Cars(1) = “Nissan Sentra”
Cars(2) = “Honda Accord”
‘random number between low and high is
‘rand = low + Int(high – low + 1) * Rnd())
chosenCar = Int(3 * Rnd())
MessageBox.Show (“I will give you a “ &
Cars(chosenCar))
AAPP00-8-3-2 VBN Visual Basic .Net Slide 15 (of 49)
Exercise 3

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

AAPP00-8-3-2 VBN Visual Basic .Net Slide 17 (of 49)


Exercise 4

Write a program that constructs an array of 20


integers, initialized with a random number from 1 to
20.
Create another array of 6 elements. Using this array,
find out how many numbers of the 20 integer array are
multiples of 2, 3, 4 and 5.

AAPP00-8-3-2 VBN Visual Basic .Net Slide 22 (of 49)


Solution 1/2
Dim num(20) As Integer
Dim count(5) As Integer
Private Sub Command1_Click()
Dim j As Integer
For j = 0 To 20
num(j) = 1 + Int(Rnd * 20)
Next

For j = 1 To 20
If num(j) Mod 2 = 0 Then
count(2) = count(2) + 1
End If
If num(j) Mod 3 = 0 Then
count(3) = count(3) + 1
End If

AAPP00-8-3-2 VBN Visual Basic .Net Slide 23 (of 49)


Solution 2/2
If num(j) Mod 4 = 0 Then
count(4) = count(4) + 1
End If
If num(j) Mod 5 = 0 Then
count(5) = count(5) + 1
End If
Next j
MsgBox ( “Multiple of Two’s" & count(2))
MsgBox (“Multiple of Three’s" & count(3))
MsgBox ( “Multiple of Four’s" & count(4))
MsgBox (“Multiple of Five’s & count(5))
End Sub

AAPP00-8-3-2 VBN Visual Basic .Net Slide 24 (of 49)


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 the array Months to the


above procedure:
Call ProcessArray(Months())

Remember: Arrays are passed by reference, not value!

AAPP00-8-3-2 VBN Visual Basic .Net Slide 25 (of 49)


Example of an Array Program
using Procedures 1/2

Dim num(30) As Integer


Dim y As Integer

Private Sub Form_Load()


Call InitializeArray(num())
Call UpdateValues(num())
End Sub

AAPP00-8-3-2 VBN Visual Basic .Net Slide 26 (of 49)


Example of an Array Program using Procedures 2/2
Private Sub InitializeArray(x() As Integer)
Dim i as integer
For i = 1 To 30
x(i) = 1 + Int(Rnd() * 10)
List1.AddItem x(i)
Next
End Sub
Private Sub UpdateValues(x() As Integer)
Dim i as integer;
For i = 1 To 30
x(i) = x(i) * x(i)
List2.AddItem x(i)
Next
End Sub
AAPP00-8-3-2 VBN Visual Basic .Net Slide 27 (of 49)
Exercise 5

Write a program that will pass an array


of 10 integers to a sub program. The
sub program will initialize the array to a
random number from 1 to 10.

AAPP00-8-3-2 VBN Visual Basic .Net Slide 28 (of 49)


Sorting Arrays

The simplest way to sort an array is through a bubble


sort. The lighter number floats up to the top.

X(1) 5 X(1) 3 X(1) 3


X(2) 3 X(2) 3 X(2) 5
Temp 5 Temp 5 Temp 5

Temp = X(1) X(1) = X(2) X(2) = Temp

Step 1 Step 2 Step 3

AAPP00-8-3-2 VBN Visual Basic .Net Slide 29 (of 49)


Program that Sorts Arrays
Private Sub Command1_Click()
Dim num(10) As Integer
Dim y, maxNo, temp As Integer
For y = 1 To 10
num(y) = 1 + Int(Rnd() * 100)
Next y
For maxNo = 1 To 10 Step 1
For y = 1 To 9 Step 1
If num(y) > num(y + 1) Then
temp = num(y)
num(y) = num(y + 1)
num(y + 1) = temp
End If
Next y
Next maxNo
AAPP00-8-3-2 VBN Visual Basic .Net Slide 30 (of 49)
Exercise 6

Write a program to sort the following array


into descending order
1578903264

After Sorting
9876543210

AAPP00-8-3-2 VBN Visual Basic .Net Slide 31 (of 49)


Searching Arrays

There are (at least) two ways to search through


an array:
Linear search
Compares each element with the search key

Binary search
Eliminates searching through redundant
arrays by comparing the search key with
the middle element (array must be
sorted) Like searching through a
dictionary

AAPP00-8-3-2 VBN Visual Basic .Net Slide 32 (of 49)


Exercise 7

 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”

AAPP00-8-3-2 VBN Visual Basic .Net Slide 34 (of 49)


Program Using Linear Search
Dim a(10) As Integer
Dim x, key As Integer
For x = 1 to 10
a(x) = val(Inputbox(“Enter Num”)
Next
key = val(Inputbox(“Enter Num to Search”)
For x = 0 To 10
If a(x) = key
MsgBox “Found”
Exit Sub
End If
Next
MsgBox (The number “ & key “was not found”
AAPP00-8-3-2 VBN Visual Basic .Net Slide 33 (of 49)
Program Using Binary Search

Dim a(10),x, low, middle, high, key As Integer


For x = 1 to 10
a(x) = val(Inputbox(“Enter Num”)
Next
key = val(Inputbox(“Enter Num to Search”)
low = 0
high = 10

AAPP00-8-3-2 VBN Visual Basic .Net Slide 35 (of 49)


Program Using Binary Search (cont)
Do While (low <= high)
middle = (low + high) / 2
If (key = a(middle)) Then
MsgBox “Found”
Exit Sub
Elseif (key < a(middle) Then
high = middle – 1
Else
low = middle + 1
End If
Loop
MsgBox “Not found”
AAPP00-8-3-2 VBN Visual Basic .Net Slide 36 (of 49)
Multi-dimensional Arrays

While single dimensional arrays look like


this:
A B C D E

A two-dimensional array looks like this:


A1 B1 C1 D1 E1
A2 B2 C2 D2 E2
A3 B3 C3 D3 E3

AAPP00-8-3-2 VBN Visual Basic .Net Slide 38 (of 49)


Declaring a Multi-dimensional Array

0 1 2 3 4 5 6 7
0 If we were to represent
1 a chess board using a
multi-dimensional
2
array, we will have the
3 following declaration:
4
Dim chess(7,7) As Integer
5
6
Red square: chess(5,4)
7

AAPP00-8-3-2 VBN Visual Basic .Net Slide 39 (of 49)


Example of using a Multi-Dimensional Array

Option explicit
‘store values into a two dimensional array
Dim n(2, 3) As Integer
Private Sub Command1_Click()
For i = 0 To 2
For j = 0 To 3
n(i, j) = CInt(InputBox(“Enter value for Row ” & i
& “Column ” & j))
Next
Next
‘display contents of the array to listbox
For i = 0 To 2
For j = 0 To 3
List1.AddItem n(i, j)
Next
Next
End Sub

AAPP00-8-3-2 VBN Visual Basic .Net Slide 41 (of 49)


Exercise 8
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

AAPP00-8-3-2 VBN Visual Basic .Net Slide 42 (of 49)


Solution
Dim name(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
name(player) = inputbox(“enter name”)
for round 1 = 1 to 4
name(player, round) = CInt(inputbox(“enter score”))
next
next
‘compute score for each payer
For player = 1 to 3
total = 0
For round = 1 to 4
total = total + score(player,round)
Next
MsgBox (“The total score for” & name(player) & “is ’’ & total)
Next

AAPP00-8-3-2 VBN Visual Basic .Net Slide 43 (of 49)


Exercise 9

• Write a program to store random


numbers from 1 to 10 into a one
dimensional array and determine the
largest number

AAPP00-8-3-2 VBN Visual Basic .Net Slide 45 (of 49)


Exercise 10

100 students were asked to rate the quality of a


particular lecturer in their class. They gave 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.

AAPP00-8-3-2 VBN Visual Basic .Net Slide 46 (of 49)


Solution
Dim frequency(10), x, poll As Integer
Private Sub Command1_Click()
For x = 1 To 100
poll = 1 + Int(Rnd() * 10)
frequency(poll) = frequency(poll) + 1
Next
Print "These are the results of the poll:"
For x = 1 To 10
Print "No. " & x & " = " & frequency(x)
Next
End Sub

AAPP00-8-3-2 VBN Visual Basic .Net Slide 47 (of 49)


Next lecture

Files

AAPP00-8-3-2 VBN Visual Basic .Net Slide 49 (of 49)


Q&A

AAPP00-8-3-2 VBN Visual Basic .Net Slide 48 (of 49)

You might also like