Declaration as Form-Level variable

Introduction to
Computing
Dr. Nadeem A Khan
Lecture 27
Chapter 7: Arrays (Schneider)
► Read
all examples and comments
► Practice
on exercise problems
for example:
Exercise 7.1:
Exercise 7.2:
Exercise 7.3:
Exercise 7.4:
Exercise 7.5:
1-5, 7-11, 14,17,18, 25, 27, 31, 37
5-8, 11,12, 15,17,21,25
1,5,9,17,19,21,24,25-28,33,35,37
1-6, 21, 29,31
1-4, 8-13, 21-23
Arrays
► Value
assignment:
Dim names(1 To 3) as String
Let names(1)=“Aslam”
Let names(2)=“Khalid”
Let names(3)=“Akbar”
Picture1.Print names(3), names(2),names(1)
Arrays
►
Range of an Array:
Dim arrayName (1 to n) As varType
Dim arrayName (m to n) As varType
e.g:
Dim names(1 To 3) as String
Dim scores(10 To 20) as Single
=> Range of array need not begin with 1
Note:Dim value(5) As Integer =Dim(0 To 5) As Integer
Arrays
► Example1:
Display the name of the student from a
class of 5 who got the rank specified by
the user
Arrays
Dim student(1 To 5) As String
Sub Form_Load ( )
Rem Fill data in array
Let student(1)= “Samina”
Let student(2)=“Akbar”
Let student(3)=“Rizwan”
Let student(4)=“Aslam”
Let student(5)=“Iram”
End Sub
Arrays
Sub Command1_Click( )
Dim n As Integer
Rem Access the student array
Let n=Val(Text1.Text)
Picture1.Cls
If n<=5 Then
Picture1.Print student(n); “ got rank”; n
Else
Picture1.Print “Rank”; n; “does not exist”
End If
End Sub
Arrays
=>
For repeated usage it is efficient to
declare a form-level/global array and
assign data once in the Form-Load
event
Arrays
► If
the amount of data is not known in
advance?
Arrays
► If
the amount of data is not known in
advance?
Solution 1: Declare an array that is large enough
and use part of it
Employ a counter variable: See example 2 of
Section 7.2
Arrays
► If
the amount of data is not known in
advance?
Solution 2: Use
ReDim
(Dynamic Array)
Arrays
► ReDim
Declaration:
ReDim arrayName (1 to n ) As varType
e.g:
ReDim score(1 to numStudents) As Single
=> subscripts: variables and expressions
=> Can be placed only within a procedure
Arrays
► Declaration
as a Local variable:
ReDim arrayName (1 to n ) As varType
within a procedure
=> Created and erased in memory each time the
procedure is called
Arrays
► Declaration
as Form-Level variable:
 Place as General declaration:
Dim arrayName ( ) As varType
 Place in one procedure
ReDim arrayName (1 to n ) As varType
=>only after ReDim has been executed:
- its range is established
- useable only after it
Arrays
► Example
2:
Display the name of the student from a
class of who got the rank specified by the
user
Arrays
► Given
in example 2:
 The name of the students are in a file
(CLASS.TXT) arranged according to their
ranks
 Class strength is given as the first item of
the file
Arrays
Dim student( ) As String
Dim classStrength As Integer
Sub Form_Load ( )
Dim i As Integer
Rem Fill data in array
Open “CLASS.TXT” For Input As #1
Input #1, classStrength
ReDim student (1 To classStrength) As String
For i=1 To classStrength
Input #1, student(i)
Next i
Close #1
End Sub
Arrays
Sub Command1_Click( )
Dim n As Integer
Rem Access the student array
Let n=Val(Text1.Text)
Picture1.Cls
If n<=classStrength Then
Picture1.Print student(n); “ got rank”; n
Else
Picture1.Print “Rank”; n; “does not exist”
End If
End Sub
Arrays
► What
item?
if the class strength is not there as the first
Arrays
► What
if the class strength is not there as the first
item in the data file?
Ans: Count the number of items in the file first and
use it to dimension (ReDim) the array
Using Arrays: Ordered Arrays
► Ordered
array
vs
Unordered array
E.g:
 Ascending order:
[each element]<=[next element]
Using Arrays: Ordered Arrays
► Ordered
array
vs
Unordered array
=> Advantage: Efficient Searching
Using Arrays: Ordered Arrays
► Example:
Request a name and inform if in the ordered list
Using Arrays: Ordered Arrays
Dim nom(1 To 5) As String
‘General Declaration
Sub Form_Load
Rem Place the names in the array in ascending order
Let nom(1) =“AKBAR”
Let nom(2) =“ASLAM”
Let nom(3) =“BUSHRA”
Let nom(4) =“TONY”
Let nom(5) =“ZAID”
End Sub
Sub Command1_Click
Dim n As Integer, name2Find As String
Let name2Find = Ucase(Trim(Text1.Text))
Let n=0
Do
Let n=n+1
Loop Until (nom(n) >= name2Find) Or (n=5)
If nom(n) =name2Find Then
Picture1.Print “Found”
Else
Picture1.Print “Not found”
End If
End Sub
Using Arrays: Ordered Arrays
=> Average search was half the array dimension
Passing Arrays Between Procedure
Passing Arrays
Sub Command1_Click( )
ReDim score(1 To 5) As Integer
Call FillArray(score( ))
Picture1.Cls
Picture1.Print “Average score is”: Sum(score( ))/5
End Sub
Sub FillArray (s( ) As Integer)
Let s(1)=85
Let s(2)=92
Let s(3)=75
Let s(4)=68
Let s(5)=84
End Sub
Passing Arrays
Function Sum (s( ) As Integer) As Integer
Dim total As Integer, index As Integer
Rem Add up scores
Let total=0
For index =1 To 5
Let total = total + s(index)
Next index
Sum = total
End Function
Passing Arrays
► What
if the dimension of the array is not known
before hand?
Ans: Passing on both the array as well its
dimension as two arguments will be required
End