lecture (11)one-dimensional arrays
Post on 30-May-2018
228 Views
Preview:
TRANSCRIPT
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 126
1
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
FSB23103 Object OrientedProgramming
Lecture 12
One-dimensional Arrays
Mdm Ratnawati Ibrahim
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 226
2
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
Topics
bull Arrays - single
definition
declaration
types
initialization
index length
bull Methods and properties of SystemArray class
bull Change array size ndash ReDim
bull Case study ndash Random numbersbull Passing array to a method
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 326
3
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
Array ndash definition (1)
bull A type of ADTs (abstract data types)bull A collection of data which
have the same name and type
are stored in a consecutive group of
computer memory locationsbull eg
weekDays
courseMarks
Monday Tuesday Wednesday Thursday Friday
85 70 90 65
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 426
4
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
Array ndash definition (2)
bull An item in an array is called an elementbull The position of an element in an array is referred
to as its index
bull eg an array named weekDays
Monday Tuesday Wednesday Thursday Friday
indices 0 21 3 4
Note The indices are used to locate data in an array
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 526
5
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
Array - declaration
bullIn VB an array is declared just like any othervariables
bull eg
allocate 5 memory locations for storingstrings of an array named weekDays
the size (or length) of the array is 5
the indices range from 0 to 4
Note In other programming languages eg Java C JavaScript
the array weekDays is declared as weekDays(5) instead
Dim weekDays(4) As String
Array
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 626
6
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
Array ndash data types
bullAn array can hold any data type primitive data type eg
bull Integer
bull String
bull Boolean
ADTs egbull Controls - Button TextBox
bull VB library - Random
bull User-defined- Circle Square Account
bull The only constraint is that all the elements inan array must be of the same type
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 726
7
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
Array ndash initialisation (1)
bullBy default the VB compiler sets the initial valuesfor all elements of an array
0 for numbers (eg Integer andDouble)
for strings Nothing for objects
bull Arrays may be initialised
on declaration
using assignment statement
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 826
8
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
Array ndash initialisation (2)
bullArrays may be initialised on declarationbull eg
Dim weekDays() As String = Monday Tuesday _
Wednesday Thursday _ Friday
Monday Tuesday Wednesday Thursday Friday
weekDays(0) weekDays(1) weekDays(2) weekDays(3) weekDays(4)
Note the size of the array is not specified
it is inferred by the number of initial values
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 926
9
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
Array ndash initialisation (3)
bullAn array may be initialized using assignmentstatements
bull eg
Dim weekDays(4) As String
weekDays(0) = Monday
weekDays(1) = Tuesday
weekDays(2) = Wednesday
weekDays(3) = Thursday
weekDays(4) = Friday
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 102610
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Array methods and properties(1)
bull All arrays have access to the methods andproperties of SystemArray class
bull eg
Lengthbull
total number of elements in the array getUpperBound(0)
bull returns the upper bound of the one-dimensionalarray
Sortbull sort the array in an ascending order
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 112611
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Array methods and properties(2)
bull eg
Dim myList() As Integer = 2 3 4 1
Dim size As Integer = myListLength
Dim highestIndexNo =
myListGetUpperBound(0)
ArraySort(myList)
2 3 4 1myList
size
highestIndexNo
myList
4
3
1 2 3 4
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 122612
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example of Using arrays (1)
bull Show week days
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 132613
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example of Using arrays (2)
bull Form design
ListBox
lstBox
Button
cmdShow
Button
cmdClose
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 142614
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Private Sub cmdCalculate_Click( _
ByVal sender As SystemObject _
ByVal e As SystemEventArgs) _
Handles cmdCalculateClick
Dim weekDays() As String = Monday _Tuesday Wednesday _
Thursday Friday
Dim i As Integer
lstBoxItemsAdd(Index Day)
For i = 0 To weekDaysgetUpperBound(0)
lstBoxItemsAdd( CStr(i) amp Tab amp weekDays(i) )
Next
End Sub
Example of Using arrays (3)
bull cmdShow_Click
FOR
loop
Array
element
Array
Method
Add an item
to ListBox
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 152615
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Array size
bull Once an array is created
its lengthsize is fixed
its index ranges from 0 to its upper bound
bull eg array weekDays(4)
Monday Tuesday Wednesday Thursday Friday
indices 0 21 3 4
weekDaysLength is 5
weekDaysGetUpperBound(0) is 4
Dim weekDays() As String = Monday Tuesday _
Wednesday Thursday Friday
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 162616
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Valid array indices
bull When a program is executed Visual Studio automatically
performs bounds checking to ensurethe program does not access data outside the bounds of an array
bull Valid indies of array elements are from 0 to its upperbound
bull Referencing array elements outside the array boundscauses a runtime error
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 172617
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Change array size
bull ReDim
bull used to change the lengthsize of an array atrun-time ie as program executes
bull eg
ReDim Preserve weekDays(6)
weekDays(5) = Saturday
weekDays(6) = Sunday
Monday Tuesday Wednesday Thursday Friday Saturday Sunday
indices
0 21 3 4 5 6
The keyword
Preserve ensures
that data stored in
the array are saved
weekDaysLength is 7
weekDaysGetUpperBound(0) is 6
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 182618
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (1)
bull Ten random numbers
Random numbers
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 192619
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (2)
bull Program requirements
generate and display 10 random numbers inthe range 1 to 100
sort the numbers
calculate and display the min max and meanof 10 numbers generated
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2026
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2126
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 222622
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (5)
Private Sub cmdSort_Click(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles cmdSortClick
Dim i As Integer
clear the listbox
lstBoxItemsClear()
ArraySort(data)
display the sorted data in the listbox
For i = 0 To N - 1
lstBoxItemsAdd(CStr(data(i)))
NextEnd Sub
Private Sub cmdCalculate_Click(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles cmdCalculateClick
CalculateAndDisplayStats()
End Sub
Button
cmdSort
bull Program code
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 232623
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Private Sub CalculateAndDisplayStats()
Dim minD As Integer = data(0)Dim maxD As Integer = data(0)
Dim total As Integer = data(0)Dim mean As Double
Dim i As Integer
For i = 1 To N - 1
total += data(i)
If data(i) lt minD Then
minD = data(i)End If
If data(i) gt maxD Then
maxD = data(i)End If
Nextmean = MathRound(total N 2)
lblMinText = Min amp CStr(minD)
lblMaxText = Max amp CStr(maxD)
lblMeanText = Mean amp CStr(mean)
End Sub
End Class
Example (6)
Display
results
Calculate
the Min
Max mean
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 242624
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (1)
bull An array can be passed to a method byincluding the array name in the argument list
eg
bull For a method to receive an array through a callthe parameter list must indicate that an arraywill be received
eg
Function Average(ByVal arr() As Integer) As Double
mean = Average(courseMarks)
ArrayFunction call
Function Average
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 252625
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (2)
bull eg Calculate the average course mark
Dim courseMarks() As Integer = 85 70 90 65
Dim mean As Double
mean = Average(courseMarks)
MessageBoxShow( Mean amp CStr(mean) )
Private Function Average(ByVal arr() As Integer) As Double
Dim i As IntegerDim count As Integer = 0
Dim total As Integer = 0
For i = 0 To arrLength -1total += arr(i)count += 1
Next
Return MathRound(total count 1)
End Function
Note The size of the
array arr is not
specified ndash it can be
of any sizeFor
loopArray
property
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2626
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Summary
bull An array is a collection of data with a single name
bull All the elements in an array are of the same type
bull The index of an array starts at 0
bull All arrays have access to methods and properties of SystemArray class eg
Length getUpperBound(0)
Sort
bull Referencing array elements outside the array boundscauses a runtime error
bull The size of an can be changed at run-time using ReDim
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 226
2
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
Topics
bull Arrays - single
definition
declaration
types
initialization
index length
bull Methods and properties of SystemArray class
bull Change array size ndash ReDim
bull Case study ndash Random numbersbull Passing array to a method
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 326
3
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
Array ndash definition (1)
bull A type of ADTs (abstract data types)bull A collection of data which
have the same name and type
are stored in a consecutive group of
computer memory locationsbull eg
weekDays
courseMarks
Monday Tuesday Wednesday Thursday Friday
85 70 90 65
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 426
4
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
Array ndash definition (2)
bull An item in an array is called an elementbull The position of an element in an array is referred
to as its index
bull eg an array named weekDays
Monday Tuesday Wednesday Thursday Friday
indices 0 21 3 4
Note The indices are used to locate data in an array
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 526
5
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
Array - declaration
bullIn VB an array is declared just like any othervariables
bull eg
allocate 5 memory locations for storingstrings of an array named weekDays
the size (or length) of the array is 5
the indices range from 0 to 4
Note In other programming languages eg Java C JavaScript
the array weekDays is declared as weekDays(5) instead
Dim weekDays(4) As String
Array
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 626
6
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
Array ndash data types
bullAn array can hold any data type primitive data type eg
bull Integer
bull String
bull Boolean
ADTs egbull Controls - Button TextBox
bull VB library - Random
bull User-defined- Circle Square Account
bull The only constraint is that all the elements inan array must be of the same type
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 726
7
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
Array ndash initialisation (1)
bullBy default the VB compiler sets the initial valuesfor all elements of an array
0 for numbers (eg Integer andDouble)
for strings Nothing for objects
bull Arrays may be initialised
on declaration
using assignment statement
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 826
8
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
Array ndash initialisation (2)
bullArrays may be initialised on declarationbull eg
Dim weekDays() As String = Monday Tuesday _
Wednesday Thursday _ Friday
Monday Tuesday Wednesday Thursday Friday
weekDays(0) weekDays(1) weekDays(2) weekDays(3) weekDays(4)
Note the size of the array is not specified
it is inferred by the number of initial values
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 926
9
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
Array ndash initialisation (3)
bullAn array may be initialized using assignmentstatements
bull eg
Dim weekDays(4) As String
weekDays(0) = Monday
weekDays(1) = Tuesday
weekDays(2) = Wednesday
weekDays(3) = Thursday
weekDays(4) = Friday
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 102610
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Array methods and properties(1)
bull All arrays have access to the methods andproperties of SystemArray class
bull eg
Lengthbull
total number of elements in the array getUpperBound(0)
bull returns the upper bound of the one-dimensionalarray
Sortbull sort the array in an ascending order
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 112611
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Array methods and properties(2)
bull eg
Dim myList() As Integer = 2 3 4 1
Dim size As Integer = myListLength
Dim highestIndexNo =
myListGetUpperBound(0)
ArraySort(myList)
2 3 4 1myList
size
highestIndexNo
myList
4
3
1 2 3 4
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 122612
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example of Using arrays (1)
bull Show week days
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 132613
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example of Using arrays (2)
bull Form design
ListBox
lstBox
Button
cmdShow
Button
cmdClose
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 142614
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Private Sub cmdCalculate_Click( _
ByVal sender As SystemObject _
ByVal e As SystemEventArgs) _
Handles cmdCalculateClick
Dim weekDays() As String = Monday _Tuesday Wednesday _
Thursday Friday
Dim i As Integer
lstBoxItemsAdd(Index Day)
For i = 0 To weekDaysgetUpperBound(0)
lstBoxItemsAdd( CStr(i) amp Tab amp weekDays(i) )
Next
End Sub
Example of Using arrays (3)
bull cmdShow_Click
FOR
loop
Array
element
Array
Method
Add an item
to ListBox
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 152615
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Array size
bull Once an array is created
its lengthsize is fixed
its index ranges from 0 to its upper bound
bull eg array weekDays(4)
Monday Tuesday Wednesday Thursday Friday
indices 0 21 3 4
weekDaysLength is 5
weekDaysGetUpperBound(0) is 4
Dim weekDays() As String = Monday Tuesday _
Wednesday Thursday Friday
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 162616
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Valid array indices
bull When a program is executed Visual Studio automatically
performs bounds checking to ensurethe program does not access data outside the bounds of an array
bull Valid indies of array elements are from 0 to its upperbound
bull Referencing array elements outside the array boundscauses a runtime error
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 172617
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Change array size
bull ReDim
bull used to change the lengthsize of an array atrun-time ie as program executes
bull eg
ReDim Preserve weekDays(6)
weekDays(5) = Saturday
weekDays(6) = Sunday
Monday Tuesday Wednesday Thursday Friday Saturday Sunday
indices
0 21 3 4 5 6
The keyword
Preserve ensures
that data stored in
the array are saved
weekDaysLength is 7
weekDaysGetUpperBound(0) is 6
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 182618
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (1)
bull Ten random numbers
Random numbers
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 192619
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (2)
bull Program requirements
generate and display 10 random numbers inthe range 1 to 100
sort the numbers
calculate and display the min max and meanof 10 numbers generated
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2026
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2126
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 222622
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (5)
Private Sub cmdSort_Click(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles cmdSortClick
Dim i As Integer
clear the listbox
lstBoxItemsClear()
ArraySort(data)
display the sorted data in the listbox
For i = 0 To N - 1
lstBoxItemsAdd(CStr(data(i)))
NextEnd Sub
Private Sub cmdCalculate_Click(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles cmdCalculateClick
CalculateAndDisplayStats()
End Sub
Button
cmdSort
bull Program code
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 232623
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Private Sub CalculateAndDisplayStats()
Dim minD As Integer = data(0)Dim maxD As Integer = data(0)
Dim total As Integer = data(0)Dim mean As Double
Dim i As Integer
For i = 1 To N - 1
total += data(i)
If data(i) lt minD Then
minD = data(i)End If
If data(i) gt maxD Then
maxD = data(i)End If
Nextmean = MathRound(total N 2)
lblMinText = Min amp CStr(minD)
lblMaxText = Max amp CStr(maxD)
lblMeanText = Mean amp CStr(mean)
End Sub
End Class
Example (6)
Display
results
Calculate
the Min
Max mean
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 242624
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (1)
bull An array can be passed to a method byincluding the array name in the argument list
eg
bull For a method to receive an array through a callthe parameter list must indicate that an arraywill be received
eg
Function Average(ByVal arr() As Integer) As Double
mean = Average(courseMarks)
ArrayFunction call
Function Average
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 252625
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (2)
bull eg Calculate the average course mark
Dim courseMarks() As Integer = 85 70 90 65
Dim mean As Double
mean = Average(courseMarks)
MessageBoxShow( Mean amp CStr(mean) )
Private Function Average(ByVal arr() As Integer) As Double
Dim i As IntegerDim count As Integer = 0
Dim total As Integer = 0
For i = 0 To arrLength -1total += arr(i)count += 1
Next
Return MathRound(total count 1)
End Function
Note The size of the
array arr is not
specified ndash it can be
of any sizeFor
loopArray
property
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2626
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Summary
bull An array is a collection of data with a single name
bull All the elements in an array are of the same type
bull The index of an array starts at 0
bull All arrays have access to methods and properties of SystemArray class eg
Length getUpperBound(0)
Sort
bull Referencing array elements outside the array boundscauses a runtime error
bull The size of an can be changed at run-time using ReDim
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 326
3
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
Array ndash definition (1)
bull A type of ADTs (abstract data types)bull A collection of data which
have the same name and type
are stored in a consecutive group of
computer memory locationsbull eg
weekDays
courseMarks
Monday Tuesday Wednesday Thursday Friday
85 70 90 65
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 426
4
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
Array ndash definition (2)
bull An item in an array is called an elementbull The position of an element in an array is referred
to as its index
bull eg an array named weekDays
Monday Tuesday Wednesday Thursday Friday
indices 0 21 3 4
Note The indices are used to locate data in an array
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 526
5
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
Array - declaration
bullIn VB an array is declared just like any othervariables
bull eg
allocate 5 memory locations for storingstrings of an array named weekDays
the size (or length) of the array is 5
the indices range from 0 to 4
Note In other programming languages eg Java C JavaScript
the array weekDays is declared as weekDays(5) instead
Dim weekDays(4) As String
Array
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 626
6
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
Array ndash data types
bullAn array can hold any data type primitive data type eg
bull Integer
bull String
bull Boolean
ADTs egbull Controls - Button TextBox
bull VB library - Random
bull User-defined- Circle Square Account
bull The only constraint is that all the elements inan array must be of the same type
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 726
7
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
Array ndash initialisation (1)
bullBy default the VB compiler sets the initial valuesfor all elements of an array
0 for numbers (eg Integer andDouble)
for strings Nothing for objects
bull Arrays may be initialised
on declaration
using assignment statement
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 826
8
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
Array ndash initialisation (2)
bullArrays may be initialised on declarationbull eg
Dim weekDays() As String = Monday Tuesday _
Wednesday Thursday _ Friday
Monday Tuesday Wednesday Thursday Friday
weekDays(0) weekDays(1) weekDays(2) weekDays(3) weekDays(4)
Note the size of the array is not specified
it is inferred by the number of initial values
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 926
9
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
Array ndash initialisation (3)
bullAn array may be initialized using assignmentstatements
bull eg
Dim weekDays(4) As String
weekDays(0) = Monday
weekDays(1) = Tuesday
weekDays(2) = Wednesday
weekDays(3) = Thursday
weekDays(4) = Friday
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 102610
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Array methods and properties(1)
bull All arrays have access to the methods andproperties of SystemArray class
bull eg
Lengthbull
total number of elements in the array getUpperBound(0)
bull returns the upper bound of the one-dimensionalarray
Sortbull sort the array in an ascending order
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 112611
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Array methods and properties(2)
bull eg
Dim myList() As Integer = 2 3 4 1
Dim size As Integer = myListLength
Dim highestIndexNo =
myListGetUpperBound(0)
ArraySort(myList)
2 3 4 1myList
size
highestIndexNo
myList
4
3
1 2 3 4
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 122612
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example of Using arrays (1)
bull Show week days
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 132613
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example of Using arrays (2)
bull Form design
ListBox
lstBox
Button
cmdShow
Button
cmdClose
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 142614
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Private Sub cmdCalculate_Click( _
ByVal sender As SystemObject _
ByVal e As SystemEventArgs) _
Handles cmdCalculateClick
Dim weekDays() As String = Monday _Tuesday Wednesday _
Thursday Friday
Dim i As Integer
lstBoxItemsAdd(Index Day)
For i = 0 To weekDaysgetUpperBound(0)
lstBoxItemsAdd( CStr(i) amp Tab amp weekDays(i) )
Next
End Sub
Example of Using arrays (3)
bull cmdShow_Click
FOR
loop
Array
element
Array
Method
Add an item
to ListBox
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 152615
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Array size
bull Once an array is created
its lengthsize is fixed
its index ranges from 0 to its upper bound
bull eg array weekDays(4)
Monday Tuesday Wednesday Thursday Friday
indices 0 21 3 4
weekDaysLength is 5
weekDaysGetUpperBound(0) is 4
Dim weekDays() As String = Monday Tuesday _
Wednesday Thursday Friday
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 162616
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Valid array indices
bull When a program is executed Visual Studio automatically
performs bounds checking to ensurethe program does not access data outside the bounds of an array
bull Valid indies of array elements are from 0 to its upperbound
bull Referencing array elements outside the array boundscauses a runtime error
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 172617
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Change array size
bull ReDim
bull used to change the lengthsize of an array atrun-time ie as program executes
bull eg
ReDim Preserve weekDays(6)
weekDays(5) = Saturday
weekDays(6) = Sunday
Monday Tuesday Wednesday Thursday Friday Saturday Sunday
indices
0 21 3 4 5 6
The keyword
Preserve ensures
that data stored in
the array are saved
weekDaysLength is 7
weekDaysGetUpperBound(0) is 6
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 182618
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (1)
bull Ten random numbers
Random numbers
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 192619
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (2)
bull Program requirements
generate and display 10 random numbers inthe range 1 to 100
sort the numbers
calculate and display the min max and meanof 10 numbers generated
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2026
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2126
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 222622
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (5)
Private Sub cmdSort_Click(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles cmdSortClick
Dim i As Integer
clear the listbox
lstBoxItemsClear()
ArraySort(data)
display the sorted data in the listbox
For i = 0 To N - 1
lstBoxItemsAdd(CStr(data(i)))
NextEnd Sub
Private Sub cmdCalculate_Click(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles cmdCalculateClick
CalculateAndDisplayStats()
End Sub
Button
cmdSort
bull Program code
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 232623
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Private Sub CalculateAndDisplayStats()
Dim minD As Integer = data(0)Dim maxD As Integer = data(0)
Dim total As Integer = data(0)Dim mean As Double
Dim i As Integer
For i = 1 To N - 1
total += data(i)
If data(i) lt minD Then
minD = data(i)End If
If data(i) gt maxD Then
maxD = data(i)End If
Nextmean = MathRound(total N 2)
lblMinText = Min amp CStr(minD)
lblMaxText = Max amp CStr(maxD)
lblMeanText = Mean amp CStr(mean)
End Sub
End Class
Example (6)
Display
results
Calculate
the Min
Max mean
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 242624
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (1)
bull An array can be passed to a method byincluding the array name in the argument list
eg
bull For a method to receive an array through a callthe parameter list must indicate that an arraywill be received
eg
Function Average(ByVal arr() As Integer) As Double
mean = Average(courseMarks)
ArrayFunction call
Function Average
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 252625
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (2)
bull eg Calculate the average course mark
Dim courseMarks() As Integer = 85 70 90 65
Dim mean As Double
mean = Average(courseMarks)
MessageBoxShow( Mean amp CStr(mean) )
Private Function Average(ByVal arr() As Integer) As Double
Dim i As IntegerDim count As Integer = 0
Dim total As Integer = 0
For i = 0 To arrLength -1total += arr(i)count += 1
Next
Return MathRound(total count 1)
End Function
Note The size of the
array arr is not
specified ndash it can be
of any sizeFor
loopArray
property
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2626
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Summary
bull An array is a collection of data with a single name
bull All the elements in an array are of the same type
bull The index of an array starts at 0
bull All arrays have access to methods and properties of SystemArray class eg
Length getUpperBound(0)
Sort
bull Referencing array elements outside the array boundscauses a runtime error
bull The size of an can be changed at run-time using ReDim
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 426
4
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
Array ndash definition (2)
bull An item in an array is called an elementbull The position of an element in an array is referred
to as its index
bull eg an array named weekDays
Monday Tuesday Wednesday Thursday Friday
indices 0 21 3 4
Note The indices are used to locate data in an array
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 526
5
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
Array - declaration
bullIn VB an array is declared just like any othervariables
bull eg
allocate 5 memory locations for storingstrings of an array named weekDays
the size (or length) of the array is 5
the indices range from 0 to 4
Note In other programming languages eg Java C JavaScript
the array weekDays is declared as weekDays(5) instead
Dim weekDays(4) As String
Array
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 626
6
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
Array ndash data types
bullAn array can hold any data type primitive data type eg
bull Integer
bull String
bull Boolean
ADTs egbull Controls - Button TextBox
bull VB library - Random
bull User-defined- Circle Square Account
bull The only constraint is that all the elements inan array must be of the same type
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 726
7
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
Array ndash initialisation (1)
bullBy default the VB compiler sets the initial valuesfor all elements of an array
0 for numbers (eg Integer andDouble)
for strings Nothing for objects
bull Arrays may be initialised
on declaration
using assignment statement
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 826
8
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
Array ndash initialisation (2)
bullArrays may be initialised on declarationbull eg
Dim weekDays() As String = Monday Tuesday _
Wednesday Thursday _ Friday
Monday Tuesday Wednesday Thursday Friday
weekDays(0) weekDays(1) weekDays(2) weekDays(3) weekDays(4)
Note the size of the array is not specified
it is inferred by the number of initial values
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 926
9
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
Array ndash initialisation (3)
bullAn array may be initialized using assignmentstatements
bull eg
Dim weekDays(4) As String
weekDays(0) = Monday
weekDays(1) = Tuesday
weekDays(2) = Wednesday
weekDays(3) = Thursday
weekDays(4) = Friday
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 102610
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Array methods and properties(1)
bull All arrays have access to the methods andproperties of SystemArray class
bull eg
Lengthbull
total number of elements in the array getUpperBound(0)
bull returns the upper bound of the one-dimensionalarray
Sortbull sort the array in an ascending order
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 112611
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Array methods and properties(2)
bull eg
Dim myList() As Integer = 2 3 4 1
Dim size As Integer = myListLength
Dim highestIndexNo =
myListGetUpperBound(0)
ArraySort(myList)
2 3 4 1myList
size
highestIndexNo
myList
4
3
1 2 3 4
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 122612
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example of Using arrays (1)
bull Show week days
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 132613
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example of Using arrays (2)
bull Form design
ListBox
lstBox
Button
cmdShow
Button
cmdClose
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 142614
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Private Sub cmdCalculate_Click( _
ByVal sender As SystemObject _
ByVal e As SystemEventArgs) _
Handles cmdCalculateClick
Dim weekDays() As String = Monday _Tuesday Wednesday _
Thursday Friday
Dim i As Integer
lstBoxItemsAdd(Index Day)
For i = 0 To weekDaysgetUpperBound(0)
lstBoxItemsAdd( CStr(i) amp Tab amp weekDays(i) )
Next
End Sub
Example of Using arrays (3)
bull cmdShow_Click
FOR
loop
Array
element
Array
Method
Add an item
to ListBox
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 152615
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Array size
bull Once an array is created
its lengthsize is fixed
its index ranges from 0 to its upper bound
bull eg array weekDays(4)
Monday Tuesday Wednesday Thursday Friday
indices 0 21 3 4
weekDaysLength is 5
weekDaysGetUpperBound(0) is 4
Dim weekDays() As String = Monday Tuesday _
Wednesday Thursday Friday
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 162616
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Valid array indices
bull When a program is executed Visual Studio automatically
performs bounds checking to ensurethe program does not access data outside the bounds of an array
bull Valid indies of array elements are from 0 to its upperbound
bull Referencing array elements outside the array boundscauses a runtime error
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 172617
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Change array size
bull ReDim
bull used to change the lengthsize of an array atrun-time ie as program executes
bull eg
ReDim Preserve weekDays(6)
weekDays(5) = Saturday
weekDays(6) = Sunday
Monday Tuesday Wednesday Thursday Friday Saturday Sunday
indices
0 21 3 4 5 6
The keyword
Preserve ensures
that data stored in
the array are saved
weekDaysLength is 7
weekDaysGetUpperBound(0) is 6
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 182618
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (1)
bull Ten random numbers
Random numbers
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 192619
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (2)
bull Program requirements
generate and display 10 random numbers inthe range 1 to 100
sort the numbers
calculate and display the min max and meanof 10 numbers generated
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2026
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2126
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 222622
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (5)
Private Sub cmdSort_Click(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles cmdSortClick
Dim i As Integer
clear the listbox
lstBoxItemsClear()
ArraySort(data)
display the sorted data in the listbox
For i = 0 To N - 1
lstBoxItemsAdd(CStr(data(i)))
NextEnd Sub
Private Sub cmdCalculate_Click(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles cmdCalculateClick
CalculateAndDisplayStats()
End Sub
Button
cmdSort
bull Program code
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 232623
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Private Sub CalculateAndDisplayStats()
Dim minD As Integer = data(0)Dim maxD As Integer = data(0)
Dim total As Integer = data(0)Dim mean As Double
Dim i As Integer
For i = 1 To N - 1
total += data(i)
If data(i) lt minD Then
minD = data(i)End If
If data(i) gt maxD Then
maxD = data(i)End If
Nextmean = MathRound(total N 2)
lblMinText = Min amp CStr(minD)
lblMaxText = Max amp CStr(maxD)
lblMeanText = Mean amp CStr(mean)
End Sub
End Class
Example (6)
Display
results
Calculate
the Min
Max mean
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 242624
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (1)
bull An array can be passed to a method byincluding the array name in the argument list
eg
bull For a method to receive an array through a callthe parameter list must indicate that an arraywill be received
eg
Function Average(ByVal arr() As Integer) As Double
mean = Average(courseMarks)
ArrayFunction call
Function Average
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 252625
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (2)
bull eg Calculate the average course mark
Dim courseMarks() As Integer = 85 70 90 65
Dim mean As Double
mean = Average(courseMarks)
MessageBoxShow( Mean amp CStr(mean) )
Private Function Average(ByVal arr() As Integer) As Double
Dim i As IntegerDim count As Integer = 0
Dim total As Integer = 0
For i = 0 To arrLength -1total += arr(i)count += 1
Next
Return MathRound(total count 1)
End Function
Note The size of the
array arr is not
specified ndash it can be
of any sizeFor
loopArray
property
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2626
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Summary
bull An array is a collection of data with a single name
bull All the elements in an array are of the same type
bull The index of an array starts at 0
bull All arrays have access to methods and properties of SystemArray class eg
Length getUpperBound(0)
Sort
bull Referencing array elements outside the array boundscauses a runtime error
bull The size of an can be changed at run-time using ReDim
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 526
5
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
Array - declaration
bullIn VB an array is declared just like any othervariables
bull eg
allocate 5 memory locations for storingstrings of an array named weekDays
the size (or length) of the array is 5
the indices range from 0 to 4
Note In other programming languages eg Java C JavaScript
the array weekDays is declared as weekDays(5) instead
Dim weekDays(4) As String
Array
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 626
6
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
Array ndash data types
bullAn array can hold any data type primitive data type eg
bull Integer
bull String
bull Boolean
ADTs egbull Controls - Button TextBox
bull VB library - Random
bull User-defined- Circle Square Account
bull The only constraint is that all the elements inan array must be of the same type
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 726
7
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
Array ndash initialisation (1)
bullBy default the VB compiler sets the initial valuesfor all elements of an array
0 for numbers (eg Integer andDouble)
for strings Nothing for objects
bull Arrays may be initialised
on declaration
using assignment statement
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 826
8
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
Array ndash initialisation (2)
bullArrays may be initialised on declarationbull eg
Dim weekDays() As String = Monday Tuesday _
Wednesday Thursday _ Friday
Monday Tuesday Wednesday Thursday Friday
weekDays(0) weekDays(1) weekDays(2) weekDays(3) weekDays(4)
Note the size of the array is not specified
it is inferred by the number of initial values
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 926
9
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
Array ndash initialisation (3)
bullAn array may be initialized using assignmentstatements
bull eg
Dim weekDays(4) As String
weekDays(0) = Monday
weekDays(1) = Tuesday
weekDays(2) = Wednesday
weekDays(3) = Thursday
weekDays(4) = Friday
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 102610
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Array methods and properties(1)
bull All arrays have access to the methods andproperties of SystemArray class
bull eg
Lengthbull
total number of elements in the array getUpperBound(0)
bull returns the upper bound of the one-dimensionalarray
Sortbull sort the array in an ascending order
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 112611
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Array methods and properties(2)
bull eg
Dim myList() As Integer = 2 3 4 1
Dim size As Integer = myListLength
Dim highestIndexNo =
myListGetUpperBound(0)
ArraySort(myList)
2 3 4 1myList
size
highestIndexNo
myList
4
3
1 2 3 4
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 122612
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example of Using arrays (1)
bull Show week days
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 132613
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example of Using arrays (2)
bull Form design
ListBox
lstBox
Button
cmdShow
Button
cmdClose
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 142614
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Private Sub cmdCalculate_Click( _
ByVal sender As SystemObject _
ByVal e As SystemEventArgs) _
Handles cmdCalculateClick
Dim weekDays() As String = Monday _Tuesday Wednesday _
Thursday Friday
Dim i As Integer
lstBoxItemsAdd(Index Day)
For i = 0 To weekDaysgetUpperBound(0)
lstBoxItemsAdd( CStr(i) amp Tab amp weekDays(i) )
Next
End Sub
Example of Using arrays (3)
bull cmdShow_Click
FOR
loop
Array
element
Array
Method
Add an item
to ListBox
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 152615
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Array size
bull Once an array is created
its lengthsize is fixed
its index ranges from 0 to its upper bound
bull eg array weekDays(4)
Monday Tuesday Wednesday Thursday Friday
indices 0 21 3 4
weekDaysLength is 5
weekDaysGetUpperBound(0) is 4
Dim weekDays() As String = Monday Tuesday _
Wednesday Thursday Friday
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 162616
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Valid array indices
bull When a program is executed Visual Studio automatically
performs bounds checking to ensurethe program does not access data outside the bounds of an array
bull Valid indies of array elements are from 0 to its upperbound
bull Referencing array elements outside the array boundscauses a runtime error
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 172617
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Change array size
bull ReDim
bull used to change the lengthsize of an array atrun-time ie as program executes
bull eg
ReDim Preserve weekDays(6)
weekDays(5) = Saturday
weekDays(6) = Sunday
Monday Tuesday Wednesday Thursday Friday Saturday Sunday
indices
0 21 3 4 5 6
The keyword
Preserve ensures
that data stored in
the array are saved
weekDaysLength is 7
weekDaysGetUpperBound(0) is 6
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 182618
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (1)
bull Ten random numbers
Random numbers
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 192619
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (2)
bull Program requirements
generate and display 10 random numbers inthe range 1 to 100
sort the numbers
calculate and display the min max and meanof 10 numbers generated
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2026
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2126
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 222622
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (5)
Private Sub cmdSort_Click(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles cmdSortClick
Dim i As Integer
clear the listbox
lstBoxItemsClear()
ArraySort(data)
display the sorted data in the listbox
For i = 0 To N - 1
lstBoxItemsAdd(CStr(data(i)))
NextEnd Sub
Private Sub cmdCalculate_Click(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles cmdCalculateClick
CalculateAndDisplayStats()
End Sub
Button
cmdSort
bull Program code
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 232623
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Private Sub CalculateAndDisplayStats()
Dim minD As Integer = data(0)Dim maxD As Integer = data(0)
Dim total As Integer = data(0)Dim mean As Double
Dim i As Integer
For i = 1 To N - 1
total += data(i)
If data(i) lt minD Then
minD = data(i)End If
If data(i) gt maxD Then
maxD = data(i)End If
Nextmean = MathRound(total N 2)
lblMinText = Min amp CStr(minD)
lblMaxText = Max amp CStr(maxD)
lblMeanText = Mean amp CStr(mean)
End Sub
End Class
Example (6)
Display
results
Calculate
the Min
Max mean
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 242624
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (1)
bull An array can be passed to a method byincluding the array name in the argument list
eg
bull For a method to receive an array through a callthe parameter list must indicate that an arraywill be received
eg
Function Average(ByVal arr() As Integer) As Double
mean = Average(courseMarks)
ArrayFunction call
Function Average
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 252625
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (2)
bull eg Calculate the average course mark
Dim courseMarks() As Integer = 85 70 90 65
Dim mean As Double
mean = Average(courseMarks)
MessageBoxShow( Mean amp CStr(mean) )
Private Function Average(ByVal arr() As Integer) As Double
Dim i As IntegerDim count As Integer = 0
Dim total As Integer = 0
For i = 0 To arrLength -1total += arr(i)count += 1
Next
Return MathRound(total count 1)
End Function
Note The size of the
array arr is not
specified ndash it can be
of any sizeFor
loopArray
property
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2626
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Summary
bull An array is a collection of data with a single name
bull All the elements in an array are of the same type
bull The index of an array starts at 0
bull All arrays have access to methods and properties of SystemArray class eg
Length getUpperBound(0)
Sort
bull Referencing array elements outside the array boundscauses a runtime error
bull The size of an can be changed at run-time using ReDim
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 626
6
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
Array ndash data types
bullAn array can hold any data type primitive data type eg
bull Integer
bull String
bull Boolean
ADTs egbull Controls - Button TextBox
bull VB library - Random
bull User-defined- Circle Square Account
bull The only constraint is that all the elements inan array must be of the same type
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 726
7
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
Array ndash initialisation (1)
bullBy default the VB compiler sets the initial valuesfor all elements of an array
0 for numbers (eg Integer andDouble)
for strings Nothing for objects
bull Arrays may be initialised
on declaration
using assignment statement
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 826
8
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
Array ndash initialisation (2)
bullArrays may be initialised on declarationbull eg
Dim weekDays() As String = Monday Tuesday _
Wednesday Thursday _ Friday
Monday Tuesday Wednesday Thursday Friday
weekDays(0) weekDays(1) weekDays(2) weekDays(3) weekDays(4)
Note the size of the array is not specified
it is inferred by the number of initial values
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 926
9
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
Array ndash initialisation (3)
bullAn array may be initialized using assignmentstatements
bull eg
Dim weekDays(4) As String
weekDays(0) = Monday
weekDays(1) = Tuesday
weekDays(2) = Wednesday
weekDays(3) = Thursday
weekDays(4) = Friday
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 102610
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Array methods and properties(1)
bull All arrays have access to the methods andproperties of SystemArray class
bull eg
Lengthbull
total number of elements in the array getUpperBound(0)
bull returns the upper bound of the one-dimensionalarray
Sortbull sort the array in an ascending order
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 112611
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Array methods and properties(2)
bull eg
Dim myList() As Integer = 2 3 4 1
Dim size As Integer = myListLength
Dim highestIndexNo =
myListGetUpperBound(0)
ArraySort(myList)
2 3 4 1myList
size
highestIndexNo
myList
4
3
1 2 3 4
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 122612
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example of Using arrays (1)
bull Show week days
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 132613
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example of Using arrays (2)
bull Form design
ListBox
lstBox
Button
cmdShow
Button
cmdClose
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 142614
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Private Sub cmdCalculate_Click( _
ByVal sender As SystemObject _
ByVal e As SystemEventArgs) _
Handles cmdCalculateClick
Dim weekDays() As String = Monday _Tuesday Wednesday _
Thursday Friday
Dim i As Integer
lstBoxItemsAdd(Index Day)
For i = 0 To weekDaysgetUpperBound(0)
lstBoxItemsAdd( CStr(i) amp Tab amp weekDays(i) )
Next
End Sub
Example of Using arrays (3)
bull cmdShow_Click
FOR
loop
Array
element
Array
Method
Add an item
to ListBox
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 152615
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Array size
bull Once an array is created
its lengthsize is fixed
its index ranges from 0 to its upper bound
bull eg array weekDays(4)
Monday Tuesday Wednesday Thursday Friday
indices 0 21 3 4
weekDaysLength is 5
weekDaysGetUpperBound(0) is 4
Dim weekDays() As String = Monday Tuesday _
Wednesday Thursday Friday
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 162616
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Valid array indices
bull When a program is executed Visual Studio automatically
performs bounds checking to ensurethe program does not access data outside the bounds of an array
bull Valid indies of array elements are from 0 to its upperbound
bull Referencing array elements outside the array boundscauses a runtime error
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 172617
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Change array size
bull ReDim
bull used to change the lengthsize of an array atrun-time ie as program executes
bull eg
ReDim Preserve weekDays(6)
weekDays(5) = Saturday
weekDays(6) = Sunday
Monday Tuesday Wednesday Thursday Friday Saturday Sunday
indices
0 21 3 4 5 6
The keyword
Preserve ensures
that data stored in
the array are saved
weekDaysLength is 7
weekDaysGetUpperBound(0) is 6
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 182618
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (1)
bull Ten random numbers
Random numbers
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 192619
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (2)
bull Program requirements
generate and display 10 random numbers inthe range 1 to 100
sort the numbers
calculate and display the min max and meanof 10 numbers generated
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2026
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2126
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 222622
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (5)
Private Sub cmdSort_Click(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles cmdSortClick
Dim i As Integer
clear the listbox
lstBoxItemsClear()
ArraySort(data)
display the sorted data in the listbox
For i = 0 To N - 1
lstBoxItemsAdd(CStr(data(i)))
NextEnd Sub
Private Sub cmdCalculate_Click(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles cmdCalculateClick
CalculateAndDisplayStats()
End Sub
Button
cmdSort
bull Program code
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 232623
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Private Sub CalculateAndDisplayStats()
Dim minD As Integer = data(0)Dim maxD As Integer = data(0)
Dim total As Integer = data(0)Dim mean As Double
Dim i As Integer
For i = 1 To N - 1
total += data(i)
If data(i) lt minD Then
minD = data(i)End If
If data(i) gt maxD Then
maxD = data(i)End If
Nextmean = MathRound(total N 2)
lblMinText = Min amp CStr(minD)
lblMaxText = Max amp CStr(maxD)
lblMeanText = Mean amp CStr(mean)
End Sub
End Class
Example (6)
Display
results
Calculate
the Min
Max mean
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 242624
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (1)
bull An array can be passed to a method byincluding the array name in the argument list
eg
bull For a method to receive an array through a callthe parameter list must indicate that an arraywill be received
eg
Function Average(ByVal arr() As Integer) As Double
mean = Average(courseMarks)
ArrayFunction call
Function Average
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 252625
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (2)
bull eg Calculate the average course mark
Dim courseMarks() As Integer = 85 70 90 65
Dim mean As Double
mean = Average(courseMarks)
MessageBoxShow( Mean amp CStr(mean) )
Private Function Average(ByVal arr() As Integer) As Double
Dim i As IntegerDim count As Integer = 0
Dim total As Integer = 0
For i = 0 To arrLength -1total += arr(i)count += 1
Next
Return MathRound(total count 1)
End Function
Note The size of the
array arr is not
specified ndash it can be
of any sizeFor
loopArray
property
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2626
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Summary
bull An array is a collection of data with a single name
bull All the elements in an array are of the same type
bull The index of an array starts at 0
bull All arrays have access to methods and properties of SystemArray class eg
Length getUpperBound(0)
Sort
bull Referencing array elements outside the array boundscauses a runtime error
bull The size of an can be changed at run-time using ReDim
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 726
7
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
Array ndash initialisation (1)
bullBy default the VB compiler sets the initial valuesfor all elements of an array
0 for numbers (eg Integer andDouble)
for strings Nothing for objects
bull Arrays may be initialised
on declaration
using assignment statement
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 826
8
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
Array ndash initialisation (2)
bullArrays may be initialised on declarationbull eg
Dim weekDays() As String = Monday Tuesday _
Wednesday Thursday _ Friday
Monday Tuesday Wednesday Thursday Friday
weekDays(0) weekDays(1) weekDays(2) weekDays(3) weekDays(4)
Note the size of the array is not specified
it is inferred by the number of initial values
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 926
9
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
Array ndash initialisation (3)
bullAn array may be initialized using assignmentstatements
bull eg
Dim weekDays(4) As String
weekDays(0) = Monday
weekDays(1) = Tuesday
weekDays(2) = Wednesday
weekDays(3) = Thursday
weekDays(4) = Friday
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 102610
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Array methods and properties(1)
bull All arrays have access to the methods andproperties of SystemArray class
bull eg
Lengthbull
total number of elements in the array getUpperBound(0)
bull returns the upper bound of the one-dimensionalarray
Sortbull sort the array in an ascending order
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 112611
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Array methods and properties(2)
bull eg
Dim myList() As Integer = 2 3 4 1
Dim size As Integer = myListLength
Dim highestIndexNo =
myListGetUpperBound(0)
ArraySort(myList)
2 3 4 1myList
size
highestIndexNo
myList
4
3
1 2 3 4
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 122612
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example of Using arrays (1)
bull Show week days
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 132613
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example of Using arrays (2)
bull Form design
ListBox
lstBox
Button
cmdShow
Button
cmdClose
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 142614
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Private Sub cmdCalculate_Click( _
ByVal sender As SystemObject _
ByVal e As SystemEventArgs) _
Handles cmdCalculateClick
Dim weekDays() As String = Monday _Tuesday Wednesday _
Thursday Friday
Dim i As Integer
lstBoxItemsAdd(Index Day)
For i = 0 To weekDaysgetUpperBound(0)
lstBoxItemsAdd( CStr(i) amp Tab amp weekDays(i) )
Next
End Sub
Example of Using arrays (3)
bull cmdShow_Click
FOR
loop
Array
element
Array
Method
Add an item
to ListBox
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 152615
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Array size
bull Once an array is created
its lengthsize is fixed
its index ranges from 0 to its upper bound
bull eg array weekDays(4)
Monday Tuesday Wednesday Thursday Friday
indices 0 21 3 4
weekDaysLength is 5
weekDaysGetUpperBound(0) is 4
Dim weekDays() As String = Monday Tuesday _
Wednesday Thursday Friday
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 162616
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Valid array indices
bull When a program is executed Visual Studio automatically
performs bounds checking to ensurethe program does not access data outside the bounds of an array
bull Valid indies of array elements are from 0 to its upperbound
bull Referencing array elements outside the array boundscauses a runtime error
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 172617
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Change array size
bull ReDim
bull used to change the lengthsize of an array atrun-time ie as program executes
bull eg
ReDim Preserve weekDays(6)
weekDays(5) = Saturday
weekDays(6) = Sunday
Monday Tuesday Wednesday Thursday Friday Saturday Sunday
indices
0 21 3 4 5 6
The keyword
Preserve ensures
that data stored in
the array are saved
weekDaysLength is 7
weekDaysGetUpperBound(0) is 6
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 182618
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (1)
bull Ten random numbers
Random numbers
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 192619
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (2)
bull Program requirements
generate and display 10 random numbers inthe range 1 to 100
sort the numbers
calculate and display the min max and meanof 10 numbers generated
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2026
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2126
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 222622
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (5)
Private Sub cmdSort_Click(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles cmdSortClick
Dim i As Integer
clear the listbox
lstBoxItemsClear()
ArraySort(data)
display the sorted data in the listbox
For i = 0 To N - 1
lstBoxItemsAdd(CStr(data(i)))
NextEnd Sub
Private Sub cmdCalculate_Click(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles cmdCalculateClick
CalculateAndDisplayStats()
End Sub
Button
cmdSort
bull Program code
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 232623
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Private Sub CalculateAndDisplayStats()
Dim minD As Integer = data(0)Dim maxD As Integer = data(0)
Dim total As Integer = data(0)Dim mean As Double
Dim i As Integer
For i = 1 To N - 1
total += data(i)
If data(i) lt minD Then
minD = data(i)End If
If data(i) gt maxD Then
maxD = data(i)End If
Nextmean = MathRound(total N 2)
lblMinText = Min amp CStr(minD)
lblMaxText = Max amp CStr(maxD)
lblMeanText = Mean amp CStr(mean)
End Sub
End Class
Example (6)
Display
results
Calculate
the Min
Max mean
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 242624
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (1)
bull An array can be passed to a method byincluding the array name in the argument list
eg
bull For a method to receive an array through a callthe parameter list must indicate that an arraywill be received
eg
Function Average(ByVal arr() As Integer) As Double
mean = Average(courseMarks)
ArrayFunction call
Function Average
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 252625
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (2)
bull eg Calculate the average course mark
Dim courseMarks() As Integer = 85 70 90 65
Dim mean As Double
mean = Average(courseMarks)
MessageBoxShow( Mean amp CStr(mean) )
Private Function Average(ByVal arr() As Integer) As Double
Dim i As IntegerDim count As Integer = 0
Dim total As Integer = 0
For i = 0 To arrLength -1total += arr(i)count += 1
Next
Return MathRound(total count 1)
End Function
Note The size of the
array arr is not
specified ndash it can be
of any sizeFor
loopArray
property
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2626
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Summary
bull An array is a collection of data with a single name
bull All the elements in an array are of the same type
bull The index of an array starts at 0
bull All arrays have access to methods and properties of SystemArray class eg
Length getUpperBound(0)
Sort
bull Referencing array elements outside the array boundscauses a runtime error
bull The size of an can be changed at run-time using ReDim
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 826
8
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
Array ndash initialisation (2)
bullArrays may be initialised on declarationbull eg
Dim weekDays() As String = Monday Tuesday _
Wednesday Thursday _ Friday
Monday Tuesday Wednesday Thursday Friday
weekDays(0) weekDays(1) weekDays(2) weekDays(3) weekDays(4)
Note the size of the array is not specified
it is inferred by the number of initial values
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 926
9
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
Array ndash initialisation (3)
bullAn array may be initialized using assignmentstatements
bull eg
Dim weekDays(4) As String
weekDays(0) = Monday
weekDays(1) = Tuesday
weekDays(2) = Wednesday
weekDays(3) = Thursday
weekDays(4) = Friday
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 102610
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Array methods and properties(1)
bull All arrays have access to the methods andproperties of SystemArray class
bull eg
Lengthbull
total number of elements in the array getUpperBound(0)
bull returns the upper bound of the one-dimensionalarray
Sortbull sort the array in an ascending order
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 112611
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Array methods and properties(2)
bull eg
Dim myList() As Integer = 2 3 4 1
Dim size As Integer = myListLength
Dim highestIndexNo =
myListGetUpperBound(0)
ArraySort(myList)
2 3 4 1myList
size
highestIndexNo
myList
4
3
1 2 3 4
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 122612
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example of Using arrays (1)
bull Show week days
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 132613
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example of Using arrays (2)
bull Form design
ListBox
lstBox
Button
cmdShow
Button
cmdClose
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 142614
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Private Sub cmdCalculate_Click( _
ByVal sender As SystemObject _
ByVal e As SystemEventArgs) _
Handles cmdCalculateClick
Dim weekDays() As String = Monday _Tuesday Wednesday _
Thursday Friday
Dim i As Integer
lstBoxItemsAdd(Index Day)
For i = 0 To weekDaysgetUpperBound(0)
lstBoxItemsAdd( CStr(i) amp Tab amp weekDays(i) )
Next
End Sub
Example of Using arrays (3)
bull cmdShow_Click
FOR
loop
Array
element
Array
Method
Add an item
to ListBox
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 152615
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Array size
bull Once an array is created
its lengthsize is fixed
its index ranges from 0 to its upper bound
bull eg array weekDays(4)
Monday Tuesday Wednesday Thursday Friday
indices 0 21 3 4
weekDaysLength is 5
weekDaysGetUpperBound(0) is 4
Dim weekDays() As String = Monday Tuesday _
Wednesday Thursday Friday
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 162616
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Valid array indices
bull When a program is executed Visual Studio automatically
performs bounds checking to ensurethe program does not access data outside the bounds of an array
bull Valid indies of array elements are from 0 to its upperbound
bull Referencing array elements outside the array boundscauses a runtime error
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 172617
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Change array size
bull ReDim
bull used to change the lengthsize of an array atrun-time ie as program executes
bull eg
ReDim Preserve weekDays(6)
weekDays(5) = Saturday
weekDays(6) = Sunday
Monday Tuesday Wednesday Thursday Friday Saturday Sunday
indices
0 21 3 4 5 6
The keyword
Preserve ensures
that data stored in
the array are saved
weekDaysLength is 7
weekDaysGetUpperBound(0) is 6
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 182618
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (1)
bull Ten random numbers
Random numbers
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 192619
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (2)
bull Program requirements
generate and display 10 random numbers inthe range 1 to 100
sort the numbers
calculate and display the min max and meanof 10 numbers generated
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2026
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2126
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 222622
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (5)
Private Sub cmdSort_Click(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles cmdSortClick
Dim i As Integer
clear the listbox
lstBoxItemsClear()
ArraySort(data)
display the sorted data in the listbox
For i = 0 To N - 1
lstBoxItemsAdd(CStr(data(i)))
NextEnd Sub
Private Sub cmdCalculate_Click(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles cmdCalculateClick
CalculateAndDisplayStats()
End Sub
Button
cmdSort
bull Program code
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 232623
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Private Sub CalculateAndDisplayStats()
Dim minD As Integer = data(0)Dim maxD As Integer = data(0)
Dim total As Integer = data(0)Dim mean As Double
Dim i As Integer
For i = 1 To N - 1
total += data(i)
If data(i) lt minD Then
minD = data(i)End If
If data(i) gt maxD Then
maxD = data(i)End If
Nextmean = MathRound(total N 2)
lblMinText = Min amp CStr(minD)
lblMaxText = Max amp CStr(maxD)
lblMeanText = Mean amp CStr(mean)
End Sub
End Class
Example (6)
Display
results
Calculate
the Min
Max mean
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 242624
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (1)
bull An array can be passed to a method byincluding the array name in the argument list
eg
bull For a method to receive an array through a callthe parameter list must indicate that an arraywill be received
eg
Function Average(ByVal arr() As Integer) As Double
mean = Average(courseMarks)
ArrayFunction call
Function Average
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 252625
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (2)
bull eg Calculate the average course mark
Dim courseMarks() As Integer = 85 70 90 65
Dim mean As Double
mean = Average(courseMarks)
MessageBoxShow( Mean amp CStr(mean) )
Private Function Average(ByVal arr() As Integer) As Double
Dim i As IntegerDim count As Integer = 0
Dim total As Integer = 0
For i = 0 To arrLength -1total += arr(i)count += 1
Next
Return MathRound(total count 1)
End Function
Note The size of the
array arr is not
specified ndash it can be
of any sizeFor
loopArray
property
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2626
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Summary
bull An array is a collection of data with a single name
bull All the elements in an array are of the same type
bull The index of an array starts at 0
bull All arrays have access to methods and properties of SystemArray class eg
Length getUpperBound(0)
Sort
bull Referencing array elements outside the array boundscauses a runtime error
bull The size of an can be changed at run-time using ReDim
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 926
9
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
FSB23103
Array ndash initialisation (3)
bullAn array may be initialized using assignmentstatements
bull eg
Dim weekDays(4) As String
weekDays(0) = Monday
weekDays(1) = Tuesday
weekDays(2) = Wednesday
weekDays(3) = Thursday
weekDays(4) = Friday
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 102610
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Array methods and properties(1)
bull All arrays have access to the methods andproperties of SystemArray class
bull eg
Lengthbull
total number of elements in the array getUpperBound(0)
bull returns the upper bound of the one-dimensionalarray
Sortbull sort the array in an ascending order
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 112611
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Array methods and properties(2)
bull eg
Dim myList() As Integer = 2 3 4 1
Dim size As Integer = myListLength
Dim highestIndexNo =
myListGetUpperBound(0)
ArraySort(myList)
2 3 4 1myList
size
highestIndexNo
myList
4
3
1 2 3 4
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 122612
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example of Using arrays (1)
bull Show week days
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 132613
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example of Using arrays (2)
bull Form design
ListBox
lstBox
Button
cmdShow
Button
cmdClose
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 142614
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Private Sub cmdCalculate_Click( _
ByVal sender As SystemObject _
ByVal e As SystemEventArgs) _
Handles cmdCalculateClick
Dim weekDays() As String = Monday _Tuesday Wednesday _
Thursday Friday
Dim i As Integer
lstBoxItemsAdd(Index Day)
For i = 0 To weekDaysgetUpperBound(0)
lstBoxItemsAdd( CStr(i) amp Tab amp weekDays(i) )
Next
End Sub
Example of Using arrays (3)
bull cmdShow_Click
FOR
loop
Array
element
Array
Method
Add an item
to ListBox
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 152615
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Array size
bull Once an array is created
its lengthsize is fixed
its index ranges from 0 to its upper bound
bull eg array weekDays(4)
Monday Tuesday Wednesday Thursday Friday
indices 0 21 3 4
weekDaysLength is 5
weekDaysGetUpperBound(0) is 4
Dim weekDays() As String = Monday Tuesday _
Wednesday Thursday Friday
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 162616
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Valid array indices
bull When a program is executed Visual Studio automatically
performs bounds checking to ensurethe program does not access data outside the bounds of an array
bull Valid indies of array elements are from 0 to its upperbound
bull Referencing array elements outside the array boundscauses a runtime error
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 172617
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Change array size
bull ReDim
bull used to change the lengthsize of an array atrun-time ie as program executes
bull eg
ReDim Preserve weekDays(6)
weekDays(5) = Saturday
weekDays(6) = Sunday
Monday Tuesday Wednesday Thursday Friday Saturday Sunday
indices
0 21 3 4 5 6
The keyword
Preserve ensures
that data stored in
the array are saved
weekDaysLength is 7
weekDaysGetUpperBound(0) is 6
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 182618
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (1)
bull Ten random numbers
Random numbers
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 192619
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (2)
bull Program requirements
generate and display 10 random numbers inthe range 1 to 100
sort the numbers
calculate and display the min max and meanof 10 numbers generated
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2026
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2126
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 222622
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (5)
Private Sub cmdSort_Click(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles cmdSortClick
Dim i As Integer
clear the listbox
lstBoxItemsClear()
ArraySort(data)
display the sorted data in the listbox
For i = 0 To N - 1
lstBoxItemsAdd(CStr(data(i)))
NextEnd Sub
Private Sub cmdCalculate_Click(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles cmdCalculateClick
CalculateAndDisplayStats()
End Sub
Button
cmdSort
bull Program code
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 232623
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Private Sub CalculateAndDisplayStats()
Dim minD As Integer = data(0)Dim maxD As Integer = data(0)
Dim total As Integer = data(0)Dim mean As Double
Dim i As Integer
For i = 1 To N - 1
total += data(i)
If data(i) lt minD Then
minD = data(i)End If
If data(i) gt maxD Then
maxD = data(i)End If
Nextmean = MathRound(total N 2)
lblMinText = Min amp CStr(minD)
lblMaxText = Max amp CStr(maxD)
lblMeanText = Mean amp CStr(mean)
End Sub
End Class
Example (6)
Display
results
Calculate
the Min
Max mean
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 242624
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (1)
bull An array can be passed to a method byincluding the array name in the argument list
eg
bull For a method to receive an array through a callthe parameter list must indicate that an arraywill be received
eg
Function Average(ByVal arr() As Integer) As Double
mean = Average(courseMarks)
ArrayFunction call
Function Average
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 252625
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (2)
bull eg Calculate the average course mark
Dim courseMarks() As Integer = 85 70 90 65
Dim mean As Double
mean = Average(courseMarks)
MessageBoxShow( Mean amp CStr(mean) )
Private Function Average(ByVal arr() As Integer) As Double
Dim i As IntegerDim count As Integer = 0
Dim total As Integer = 0
For i = 0 To arrLength -1total += arr(i)count += 1
Next
Return MathRound(total count 1)
End Function
Note The size of the
array arr is not
specified ndash it can be
of any sizeFor
loopArray
property
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2626
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Summary
bull An array is a collection of data with a single name
bull All the elements in an array are of the same type
bull The index of an array starts at 0
bull All arrays have access to methods and properties of SystemArray class eg
Length getUpperBound(0)
Sort
bull Referencing array elements outside the array boundscauses a runtime error
bull The size of an can be changed at run-time using ReDim
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 102610
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Array methods and properties(1)
bull All arrays have access to the methods andproperties of SystemArray class
bull eg
Lengthbull
total number of elements in the array getUpperBound(0)
bull returns the upper bound of the one-dimensionalarray
Sortbull sort the array in an ascending order
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 112611
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Array methods and properties(2)
bull eg
Dim myList() As Integer = 2 3 4 1
Dim size As Integer = myListLength
Dim highestIndexNo =
myListGetUpperBound(0)
ArraySort(myList)
2 3 4 1myList
size
highestIndexNo
myList
4
3
1 2 3 4
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 122612
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example of Using arrays (1)
bull Show week days
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 132613
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example of Using arrays (2)
bull Form design
ListBox
lstBox
Button
cmdShow
Button
cmdClose
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 142614
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Private Sub cmdCalculate_Click( _
ByVal sender As SystemObject _
ByVal e As SystemEventArgs) _
Handles cmdCalculateClick
Dim weekDays() As String = Monday _Tuesday Wednesday _
Thursday Friday
Dim i As Integer
lstBoxItemsAdd(Index Day)
For i = 0 To weekDaysgetUpperBound(0)
lstBoxItemsAdd( CStr(i) amp Tab amp weekDays(i) )
Next
End Sub
Example of Using arrays (3)
bull cmdShow_Click
FOR
loop
Array
element
Array
Method
Add an item
to ListBox
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 152615
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Array size
bull Once an array is created
its lengthsize is fixed
its index ranges from 0 to its upper bound
bull eg array weekDays(4)
Monday Tuesday Wednesday Thursday Friday
indices 0 21 3 4
weekDaysLength is 5
weekDaysGetUpperBound(0) is 4
Dim weekDays() As String = Monday Tuesday _
Wednesday Thursday Friday
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 162616
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Valid array indices
bull When a program is executed Visual Studio automatically
performs bounds checking to ensurethe program does not access data outside the bounds of an array
bull Valid indies of array elements are from 0 to its upperbound
bull Referencing array elements outside the array boundscauses a runtime error
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 172617
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Change array size
bull ReDim
bull used to change the lengthsize of an array atrun-time ie as program executes
bull eg
ReDim Preserve weekDays(6)
weekDays(5) = Saturday
weekDays(6) = Sunday
Monday Tuesday Wednesday Thursday Friday Saturday Sunday
indices
0 21 3 4 5 6
The keyword
Preserve ensures
that data stored in
the array are saved
weekDaysLength is 7
weekDaysGetUpperBound(0) is 6
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 182618
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (1)
bull Ten random numbers
Random numbers
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 192619
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (2)
bull Program requirements
generate and display 10 random numbers inthe range 1 to 100
sort the numbers
calculate and display the min max and meanof 10 numbers generated
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2026
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2126
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 222622
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (5)
Private Sub cmdSort_Click(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles cmdSortClick
Dim i As Integer
clear the listbox
lstBoxItemsClear()
ArraySort(data)
display the sorted data in the listbox
For i = 0 To N - 1
lstBoxItemsAdd(CStr(data(i)))
NextEnd Sub
Private Sub cmdCalculate_Click(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles cmdCalculateClick
CalculateAndDisplayStats()
End Sub
Button
cmdSort
bull Program code
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 232623
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Private Sub CalculateAndDisplayStats()
Dim minD As Integer = data(0)Dim maxD As Integer = data(0)
Dim total As Integer = data(0)Dim mean As Double
Dim i As Integer
For i = 1 To N - 1
total += data(i)
If data(i) lt minD Then
minD = data(i)End If
If data(i) gt maxD Then
maxD = data(i)End If
Nextmean = MathRound(total N 2)
lblMinText = Min amp CStr(minD)
lblMaxText = Max amp CStr(maxD)
lblMeanText = Mean amp CStr(mean)
End Sub
End Class
Example (6)
Display
results
Calculate
the Min
Max mean
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 242624
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (1)
bull An array can be passed to a method byincluding the array name in the argument list
eg
bull For a method to receive an array through a callthe parameter list must indicate that an arraywill be received
eg
Function Average(ByVal arr() As Integer) As Double
mean = Average(courseMarks)
ArrayFunction call
Function Average
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 252625
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (2)
bull eg Calculate the average course mark
Dim courseMarks() As Integer = 85 70 90 65
Dim mean As Double
mean = Average(courseMarks)
MessageBoxShow( Mean amp CStr(mean) )
Private Function Average(ByVal arr() As Integer) As Double
Dim i As IntegerDim count As Integer = 0
Dim total As Integer = 0
For i = 0 To arrLength -1total += arr(i)count += 1
Next
Return MathRound(total count 1)
End Function
Note The size of the
array arr is not
specified ndash it can be
of any sizeFor
loopArray
property
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2626
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Summary
bull An array is a collection of data with a single name
bull All the elements in an array are of the same type
bull The index of an array starts at 0
bull All arrays have access to methods and properties of SystemArray class eg
Length getUpperBound(0)
Sort
bull Referencing array elements outside the array boundscauses a runtime error
bull The size of an can be changed at run-time using ReDim
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 112611
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Array methods and properties(2)
bull eg
Dim myList() As Integer = 2 3 4 1
Dim size As Integer = myListLength
Dim highestIndexNo =
myListGetUpperBound(0)
ArraySort(myList)
2 3 4 1myList
size
highestIndexNo
myList
4
3
1 2 3 4
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 122612
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example of Using arrays (1)
bull Show week days
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 132613
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example of Using arrays (2)
bull Form design
ListBox
lstBox
Button
cmdShow
Button
cmdClose
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 142614
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Private Sub cmdCalculate_Click( _
ByVal sender As SystemObject _
ByVal e As SystemEventArgs) _
Handles cmdCalculateClick
Dim weekDays() As String = Monday _Tuesday Wednesday _
Thursday Friday
Dim i As Integer
lstBoxItemsAdd(Index Day)
For i = 0 To weekDaysgetUpperBound(0)
lstBoxItemsAdd( CStr(i) amp Tab amp weekDays(i) )
Next
End Sub
Example of Using arrays (3)
bull cmdShow_Click
FOR
loop
Array
element
Array
Method
Add an item
to ListBox
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 152615
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Array size
bull Once an array is created
its lengthsize is fixed
its index ranges from 0 to its upper bound
bull eg array weekDays(4)
Monday Tuesday Wednesday Thursday Friday
indices 0 21 3 4
weekDaysLength is 5
weekDaysGetUpperBound(0) is 4
Dim weekDays() As String = Monday Tuesday _
Wednesday Thursday Friday
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 162616
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Valid array indices
bull When a program is executed Visual Studio automatically
performs bounds checking to ensurethe program does not access data outside the bounds of an array
bull Valid indies of array elements are from 0 to its upperbound
bull Referencing array elements outside the array boundscauses a runtime error
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 172617
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Change array size
bull ReDim
bull used to change the lengthsize of an array atrun-time ie as program executes
bull eg
ReDim Preserve weekDays(6)
weekDays(5) = Saturday
weekDays(6) = Sunday
Monday Tuesday Wednesday Thursday Friday Saturday Sunday
indices
0 21 3 4 5 6
The keyword
Preserve ensures
that data stored in
the array are saved
weekDaysLength is 7
weekDaysGetUpperBound(0) is 6
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 182618
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (1)
bull Ten random numbers
Random numbers
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 192619
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (2)
bull Program requirements
generate and display 10 random numbers inthe range 1 to 100
sort the numbers
calculate and display the min max and meanof 10 numbers generated
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2026
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2126
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 222622
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (5)
Private Sub cmdSort_Click(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles cmdSortClick
Dim i As Integer
clear the listbox
lstBoxItemsClear()
ArraySort(data)
display the sorted data in the listbox
For i = 0 To N - 1
lstBoxItemsAdd(CStr(data(i)))
NextEnd Sub
Private Sub cmdCalculate_Click(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles cmdCalculateClick
CalculateAndDisplayStats()
End Sub
Button
cmdSort
bull Program code
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 232623
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Private Sub CalculateAndDisplayStats()
Dim minD As Integer = data(0)Dim maxD As Integer = data(0)
Dim total As Integer = data(0)Dim mean As Double
Dim i As Integer
For i = 1 To N - 1
total += data(i)
If data(i) lt minD Then
minD = data(i)End If
If data(i) gt maxD Then
maxD = data(i)End If
Nextmean = MathRound(total N 2)
lblMinText = Min amp CStr(minD)
lblMaxText = Max amp CStr(maxD)
lblMeanText = Mean amp CStr(mean)
End Sub
End Class
Example (6)
Display
results
Calculate
the Min
Max mean
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 242624
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (1)
bull An array can be passed to a method byincluding the array name in the argument list
eg
bull For a method to receive an array through a callthe parameter list must indicate that an arraywill be received
eg
Function Average(ByVal arr() As Integer) As Double
mean = Average(courseMarks)
ArrayFunction call
Function Average
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 252625
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (2)
bull eg Calculate the average course mark
Dim courseMarks() As Integer = 85 70 90 65
Dim mean As Double
mean = Average(courseMarks)
MessageBoxShow( Mean amp CStr(mean) )
Private Function Average(ByVal arr() As Integer) As Double
Dim i As IntegerDim count As Integer = 0
Dim total As Integer = 0
For i = 0 To arrLength -1total += arr(i)count += 1
Next
Return MathRound(total count 1)
End Function
Note The size of the
array arr is not
specified ndash it can be
of any sizeFor
loopArray
property
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2626
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Summary
bull An array is a collection of data with a single name
bull All the elements in an array are of the same type
bull The index of an array starts at 0
bull All arrays have access to methods and properties of SystemArray class eg
Length getUpperBound(0)
Sort
bull Referencing array elements outside the array boundscauses a runtime error
bull The size of an can be changed at run-time using ReDim
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 122612
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example of Using arrays (1)
bull Show week days
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 132613
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example of Using arrays (2)
bull Form design
ListBox
lstBox
Button
cmdShow
Button
cmdClose
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 142614
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Private Sub cmdCalculate_Click( _
ByVal sender As SystemObject _
ByVal e As SystemEventArgs) _
Handles cmdCalculateClick
Dim weekDays() As String = Monday _Tuesday Wednesday _
Thursday Friday
Dim i As Integer
lstBoxItemsAdd(Index Day)
For i = 0 To weekDaysgetUpperBound(0)
lstBoxItemsAdd( CStr(i) amp Tab amp weekDays(i) )
Next
End Sub
Example of Using arrays (3)
bull cmdShow_Click
FOR
loop
Array
element
Array
Method
Add an item
to ListBox
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 152615
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Array size
bull Once an array is created
its lengthsize is fixed
its index ranges from 0 to its upper bound
bull eg array weekDays(4)
Monday Tuesday Wednesday Thursday Friday
indices 0 21 3 4
weekDaysLength is 5
weekDaysGetUpperBound(0) is 4
Dim weekDays() As String = Monday Tuesday _
Wednesday Thursday Friday
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 162616
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Valid array indices
bull When a program is executed Visual Studio automatically
performs bounds checking to ensurethe program does not access data outside the bounds of an array
bull Valid indies of array elements are from 0 to its upperbound
bull Referencing array elements outside the array boundscauses a runtime error
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 172617
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Change array size
bull ReDim
bull used to change the lengthsize of an array atrun-time ie as program executes
bull eg
ReDim Preserve weekDays(6)
weekDays(5) = Saturday
weekDays(6) = Sunday
Monday Tuesday Wednesday Thursday Friday Saturday Sunday
indices
0 21 3 4 5 6
The keyword
Preserve ensures
that data stored in
the array are saved
weekDaysLength is 7
weekDaysGetUpperBound(0) is 6
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 182618
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (1)
bull Ten random numbers
Random numbers
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 192619
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (2)
bull Program requirements
generate and display 10 random numbers inthe range 1 to 100
sort the numbers
calculate and display the min max and meanof 10 numbers generated
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2026
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2126
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 222622
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (5)
Private Sub cmdSort_Click(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles cmdSortClick
Dim i As Integer
clear the listbox
lstBoxItemsClear()
ArraySort(data)
display the sorted data in the listbox
For i = 0 To N - 1
lstBoxItemsAdd(CStr(data(i)))
NextEnd Sub
Private Sub cmdCalculate_Click(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles cmdCalculateClick
CalculateAndDisplayStats()
End Sub
Button
cmdSort
bull Program code
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 232623
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Private Sub CalculateAndDisplayStats()
Dim minD As Integer = data(0)Dim maxD As Integer = data(0)
Dim total As Integer = data(0)Dim mean As Double
Dim i As Integer
For i = 1 To N - 1
total += data(i)
If data(i) lt minD Then
minD = data(i)End If
If data(i) gt maxD Then
maxD = data(i)End If
Nextmean = MathRound(total N 2)
lblMinText = Min amp CStr(minD)
lblMaxText = Max amp CStr(maxD)
lblMeanText = Mean amp CStr(mean)
End Sub
End Class
Example (6)
Display
results
Calculate
the Min
Max mean
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 242624
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (1)
bull An array can be passed to a method byincluding the array name in the argument list
eg
bull For a method to receive an array through a callthe parameter list must indicate that an arraywill be received
eg
Function Average(ByVal arr() As Integer) As Double
mean = Average(courseMarks)
ArrayFunction call
Function Average
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 252625
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (2)
bull eg Calculate the average course mark
Dim courseMarks() As Integer = 85 70 90 65
Dim mean As Double
mean = Average(courseMarks)
MessageBoxShow( Mean amp CStr(mean) )
Private Function Average(ByVal arr() As Integer) As Double
Dim i As IntegerDim count As Integer = 0
Dim total As Integer = 0
For i = 0 To arrLength -1total += arr(i)count += 1
Next
Return MathRound(total count 1)
End Function
Note The size of the
array arr is not
specified ndash it can be
of any sizeFor
loopArray
property
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2626
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Summary
bull An array is a collection of data with a single name
bull All the elements in an array are of the same type
bull The index of an array starts at 0
bull All arrays have access to methods and properties of SystemArray class eg
Length getUpperBound(0)
Sort
bull Referencing array elements outside the array boundscauses a runtime error
bull The size of an can be changed at run-time using ReDim
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 132613
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example of Using arrays (2)
bull Form design
ListBox
lstBox
Button
cmdShow
Button
cmdClose
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 142614
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Private Sub cmdCalculate_Click( _
ByVal sender As SystemObject _
ByVal e As SystemEventArgs) _
Handles cmdCalculateClick
Dim weekDays() As String = Monday _Tuesday Wednesday _
Thursday Friday
Dim i As Integer
lstBoxItemsAdd(Index Day)
For i = 0 To weekDaysgetUpperBound(0)
lstBoxItemsAdd( CStr(i) amp Tab amp weekDays(i) )
Next
End Sub
Example of Using arrays (3)
bull cmdShow_Click
FOR
loop
Array
element
Array
Method
Add an item
to ListBox
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 152615
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Array size
bull Once an array is created
its lengthsize is fixed
its index ranges from 0 to its upper bound
bull eg array weekDays(4)
Monday Tuesday Wednesday Thursday Friday
indices 0 21 3 4
weekDaysLength is 5
weekDaysGetUpperBound(0) is 4
Dim weekDays() As String = Monday Tuesday _
Wednesday Thursday Friday
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 162616
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Valid array indices
bull When a program is executed Visual Studio automatically
performs bounds checking to ensurethe program does not access data outside the bounds of an array
bull Valid indies of array elements are from 0 to its upperbound
bull Referencing array elements outside the array boundscauses a runtime error
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 172617
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Change array size
bull ReDim
bull used to change the lengthsize of an array atrun-time ie as program executes
bull eg
ReDim Preserve weekDays(6)
weekDays(5) = Saturday
weekDays(6) = Sunday
Monday Tuesday Wednesday Thursday Friday Saturday Sunday
indices
0 21 3 4 5 6
The keyword
Preserve ensures
that data stored in
the array are saved
weekDaysLength is 7
weekDaysGetUpperBound(0) is 6
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 182618
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (1)
bull Ten random numbers
Random numbers
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 192619
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (2)
bull Program requirements
generate and display 10 random numbers inthe range 1 to 100
sort the numbers
calculate and display the min max and meanof 10 numbers generated
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2026
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2126
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 222622
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (5)
Private Sub cmdSort_Click(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles cmdSortClick
Dim i As Integer
clear the listbox
lstBoxItemsClear()
ArraySort(data)
display the sorted data in the listbox
For i = 0 To N - 1
lstBoxItemsAdd(CStr(data(i)))
NextEnd Sub
Private Sub cmdCalculate_Click(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles cmdCalculateClick
CalculateAndDisplayStats()
End Sub
Button
cmdSort
bull Program code
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 232623
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Private Sub CalculateAndDisplayStats()
Dim minD As Integer = data(0)Dim maxD As Integer = data(0)
Dim total As Integer = data(0)Dim mean As Double
Dim i As Integer
For i = 1 To N - 1
total += data(i)
If data(i) lt minD Then
minD = data(i)End If
If data(i) gt maxD Then
maxD = data(i)End If
Nextmean = MathRound(total N 2)
lblMinText = Min amp CStr(minD)
lblMaxText = Max amp CStr(maxD)
lblMeanText = Mean amp CStr(mean)
End Sub
End Class
Example (6)
Display
results
Calculate
the Min
Max mean
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 242624
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (1)
bull An array can be passed to a method byincluding the array name in the argument list
eg
bull For a method to receive an array through a callthe parameter list must indicate that an arraywill be received
eg
Function Average(ByVal arr() As Integer) As Double
mean = Average(courseMarks)
ArrayFunction call
Function Average
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 252625
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (2)
bull eg Calculate the average course mark
Dim courseMarks() As Integer = 85 70 90 65
Dim mean As Double
mean = Average(courseMarks)
MessageBoxShow( Mean amp CStr(mean) )
Private Function Average(ByVal arr() As Integer) As Double
Dim i As IntegerDim count As Integer = 0
Dim total As Integer = 0
For i = 0 To arrLength -1total += arr(i)count += 1
Next
Return MathRound(total count 1)
End Function
Note The size of the
array arr is not
specified ndash it can be
of any sizeFor
loopArray
property
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2626
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Summary
bull An array is a collection of data with a single name
bull All the elements in an array are of the same type
bull The index of an array starts at 0
bull All arrays have access to methods and properties of SystemArray class eg
Length getUpperBound(0)
Sort
bull Referencing array elements outside the array boundscauses a runtime error
bull The size of an can be changed at run-time using ReDim
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 142614
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Private Sub cmdCalculate_Click( _
ByVal sender As SystemObject _
ByVal e As SystemEventArgs) _
Handles cmdCalculateClick
Dim weekDays() As String = Monday _Tuesday Wednesday _
Thursday Friday
Dim i As Integer
lstBoxItemsAdd(Index Day)
For i = 0 To weekDaysgetUpperBound(0)
lstBoxItemsAdd( CStr(i) amp Tab amp weekDays(i) )
Next
End Sub
Example of Using arrays (3)
bull cmdShow_Click
FOR
loop
Array
element
Array
Method
Add an item
to ListBox
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 152615
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Array size
bull Once an array is created
its lengthsize is fixed
its index ranges from 0 to its upper bound
bull eg array weekDays(4)
Monday Tuesday Wednesday Thursday Friday
indices 0 21 3 4
weekDaysLength is 5
weekDaysGetUpperBound(0) is 4
Dim weekDays() As String = Monday Tuesday _
Wednesday Thursday Friday
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 162616
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Valid array indices
bull When a program is executed Visual Studio automatically
performs bounds checking to ensurethe program does not access data outside the bounds of an array
bull Valid indies of array elements are from 0 to its upperbound
bull Referencing array elements outside the array boundscauses a runtime error
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 172617
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Change array size
bull ReDim
bull used to change the lengthsize of an array atrun-time ie as program executes
bull eg
ReDim Preserve weekDays(6)
weekDays(5) = Saturday
weekDays(6) = Sunday
Monday Tuesday Wednesday Thursday Friday Saturday Sunday
indices
0 21 3 4 5 6
The keyword
Preserve ensures
that data stored in
the array are saved
weekDaysLength is 7
weekDaysGetUpperBound(0) is 6
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 182618
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (1)
bull Ten random numbers
Random numbers
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 192619
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (2)
bull Program requirements
generate and display 10 random numbers inthe range 1 to 100
sort the numbers
calculate and display the min max and meanof 10 numbers generated
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2026
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2126
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 222622
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (5)
Private Sub cmdSort_Click(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles cmdSortClick
Dim i As Integer
clear the listbox
lstBoxItemsClear()
ArraySort(data)
display the sorted data in the listbox
For i = 0 To N - 1
lstBoxItemsAdd(CStr(data(i)))
NextEnd Sub
Private Sub cmdCalculate_Click(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles cmdCalculateClick
CalculateAndDisplayStats()
End Sub
Button
cmdSort
bull Program code
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 232623
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Private Sub CalculateAndDisplayStats()
Dim minD As Integer = data(0)Dim maxD As Integer = data(0)
Dim total As Integer = data(0)Dim mean As Double
Dim i As Integer
For i = 1 To N - 1
total += data(i)
If data(i) lt minD Then
minD = data(i)End If
If data(i) gt maxD Then
maxD = data(i)End If
Nextmean = MathRound(total N 2)
lblMinText = Min amp CStr(minD)
lblMaxText = Max amp CStr(maxD)
lblMeanText = Mean amp CStr(mean)
End Sub
End Class
Example (6)
Display
results
Calculate
the Min
Max mean
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 242624
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (1)
bull An array can be passed to a method byincluding the array name in the argument list
eg
bull For a method to receive an array through a callthe parameter list must indicate that an arraywill be received
eg
Function Average(ByVal arr() As Integer) As Double
mean = Average(courseMarks)
ArrayFunction call
Function Average
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 252625
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (2)
bull eg Calculate the average course mark
Dim courseMarks() As Integer = 85 70 90 65
Dim mean As Double
mean = Average(courseMarks)
MessageBoxShow( Mean amp CStr(mean) )
Private Function Average(ByVal arr() As Integer) As Double
Dim i As IntegerDim count As Integer = 0
Dim total As Integer = 0
For i = 0 To arrLength -1total += arr(i)count += 1
Next
Return MathRound(total count 1)
End Function
Note The size of the
array arr is not
specified ndash it can be
of any sizeFor
loopArray
property
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2626
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Summary
bull An array is a collection of data with a single name
bull All the elements in an array are of the same type
bull The index of an array starts at 0
bull All arrays have access to methods and properties of SystemArray class eg
Length getUpperBound(0)
Sort
bull Referencing array elements outside the array boundscauses a runtime error
bull The size of an can be changed at run-time using ReDim
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 152615
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Array size
bull Once an array is created
its lengthsize is fixed
its index ranges from 0 to its upper bound
bull eg array weekDays(4)
Monday Tuesday Wednesday Thursday Friday
indices 0 21 3 4
weekDaysLength is 5
weekDaysGetUpperBound(0) is 4
Dim weekDays() As String = Monday Tuesday _
Wednesday Thursday Friday
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 162616
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Valid array indices
bull When a program is executed Visual Studio automatically
performs bounds checking to ensurethe program does not access data outside the bounds of an array
bull Valid indies of array elements are from 0 to its upperbound
bull Referencing array elements outside the array boundscauses a runtime error
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 172617
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Change array size
bull ReDim
bull used to change the lengthsize of an array atrun-time ie as program executes
bull eg
ReDim Preserve weekDays(6)
weekDays(5) = Saturday
weekDays(6) = Sunday
Monday Tuesday Wednesday Thursday Friday Saturday Sunday
indices
0 21 3 4 5 6
The keyword
Preserve ensures
that data stored in
the array are saved
weekDaysLength is 7
weekDaysGetUpperBound(0) is 6
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 182618
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (1)
bull Ten random numbers
Random numbers
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 192619
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (2)
bull Program requirements
generate and display 10 random numbers inthe range 1 to 100
sort the numbers
calculate and display the min max and meanof 10 numbers generated
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2026
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2126
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 222622
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (5)
Private Sub cmdSort_Click(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles cmdSortClick
Dim i As Integer
clear the listbox
lstBoxItemsClear()
ArraySort(data)
display the sorted data in the listbox
For i = 0 To N - 1
lstBoxItemsAdd(CStr(data(i)))
NextEnd Sub
Private Sub cmdCalculate_Click(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles cmdCalculateClick
CalculateAndDisplayStats()
End Sub
Button
cmdSort
bull Program code
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 232623
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Private Sub CalculateAndDisplayStats()
Dim minD As Integer = data(0)Dim maxD As Integer = data(0)
Dim total As Integer = data(0)Dim mean As Double
Dim i As Integer
For i = 1 To N - 1
total += data(i)
If data(i) lt minD Then
minD = data(i)End If
If data(i) gt maxD Then
maxD = data(i)End If
Nextmean = MathRound(total N 2)
lblMinText = Min amp CStr(minD)
lblMaxText = Max amp CStr(maxD)
lblMeanText = Mean amp CStr(mean)
End Sub
End Class
Example (6)
Display
results
Calculate
the Min
Max mean
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 242624
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (1)
bull An array can be passed to a method byincluding the array name in the argument list
eg
bull For a method to receive an array through a callthe parameter list must indicate that an arraywill be received
eg
Function Average(ByVal arr() As Integer) As Double
mean = Average(courseMarks)
ArrayFunction call
Function Average
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 252625
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (2)
bull eg Calculate the average course mark
Dim courseMarks() As Integer = 85 70 90 65
Dim mean As Double
mean = Average(courseMarks)
MessageBoxShow( Mean amp CStr(mean) )
Private Function Average(ByVal arr() As Integer) As Double
Dim i As IntegerDim count As Integer = 0
Dim total As Integer = 0
For i = 0 To arrLength -1total += arr(i)count += 1
Next
Return MathRound(total count 1)
End Function
Note The size of the
array arr is not
specified ndash it can be
of any sizeFor
loopArray
property
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2626
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Summary
bull An array is a collection of data with a single name
bull All the elements in an array are of the same type
bull The index of an array starts at 0
bull All arrays have access to methods and properties of SystemArray class eg
Length getUpperBound(0)
Sort
bull Referencing array elements outside the array boundscauses a runtime error
bull The size of an can be changed at run-time using ReDim
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 162616
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Valid array indices
bull When a program is executed Visual Studio automatically
performs bounds checking to ensurethe program does not access data outside the bounds of an array
bull Valid indies of array elements are from 0 to its upperbound
bull Referencing array elements outside the array boundscauses a runtime error
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 172617
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Change array size
bull ReDim
bull used to change the lengthsize of an array atrun-time ie as program executes
bull eg
ReDim Preserve weekDays(6)
weekDays(5) = Saturday
weekDays(6) = Sunday
Monday Tuesday Wednesday Thursday Friday Saturday Sunday
indices
0 21 3 4 5 6
The keyword
Preserve ensures
that data stored in
the array are saved
weekDaysLength is 7
weekDaysGetUpperBound(0) is 6
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 182618
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (1)
bull Ten random numbers
Random numbers
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 192619
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (2)
bull Program requirements
generate and display 10 random numbers inthe range 1 to 100
sort the numbers
calculate and display the min max and meanof 10 numbers generated
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2026
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2126
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 222622
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (5)
Private Sub cmdSort_Click(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles cmdSortClick
Dim i As Integer
clear the listbox
lstBoxItemsClear()
ArraySort(data)
display the sorted data in the listbox
For i = 0 To N - 1
lstBoxItemsAdd(CStr(data(i)))
NextEnd Sub
Private Sub cmdCalculate_Click(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles cmdCalculateClick
CalculateAndDisplayStats()
End Sub
Button
cmdSort
bull Program code
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 232623
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Private Sub CalculateAndDisplayStats()
Dim minD As Integer = data(0)Dim maxD As Integer = data(0)
Dim total As Integer = data(0)Dim mean As Double
Dim i As Integer
For i = 1 To N - 1
total += data(i)
If data(i) lt minD Then
minD = data(i)End If
If data(i) gt maxD Then
maxD = data(i)End If
Nextmean = MathRound(total N 2)
lblMinText = Min amp CStr(minD)
lblMaxText = Max amp CStr(maxD)
lblMeanText = Mean amp CStr(mean)
End Sub
End Class
Example (6)
Display
results
Calculate
the Min
Max mean
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 242624
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (1)
bull An array can be passed to a method byincluding the array name in the argument list
eg
bull For a method to receive an array through a callthe parameter list must indicate that an arraywill be received
eg
Function Average(ByVal arr() As Integer) As Double
mean = Average(courseMarks)
ArrayFunction call
Function Average
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 252625
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (2)
bull eg Calculate the average course mark
Dim courseMarks() As Integer = 85 70 90 65
Dim mean As Double
mean = Average(courseMarks)
MessageBoxShow( Mean amp CStr(mean) )
Private Function Average(ByVal arr() As Integer) As Double
Dim i As IntegerDim count As Integer = 0
Dim total As Integer = 0
For i = 0 To arrLength -1total += arr(i)count += 1
Next
Return MathRound(total count 1)
End Function
Note The size of the
array arr is not
specified ndash it can be
of any sizeFor
loopArray
property
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2626
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Summary
bull An array is a collection of data with a single name
bull All the elements in an array are of the same type
bull The index of an array starts at 0
bull All arrays have access to methods and properties of SystemArray class eg
Length getUpperBound(0)
Sort
bull Referencing array elements outside the array boundscauses a runtime error
bull The size of an can be changed at run-time using ReDim
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 172617
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Change array size
bull ReDim
bull used to change the lengthsize of an array atrun-time ie as program executes
bull eg
ReDim Preserve weekDays(6)
weekDays(5) = Saturday
weekDays(6) = Sunday
Monday Tuesday Wednesday Thursday Friday Saturday Sunday
indices
0 21 3 4 5 6
The keyword
Preserve ensures
that data stored in
the array are saved
weekDaysLength is 7
weekDaysGetUpperBound(0) is 6
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 182618
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (1)
bull Ten random numbers
Random numbers
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 192619
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (2)
bull Program requirements
generate and display 10 random numbers inthe range 1 to 100
sort the numbers
calculate and display the min max and meanof 10 numbers generated
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2026
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2126
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 222622
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (5)
Private Sub cmdSort_Click(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles cmdSortClick
Dim i As Integer
clear the listbox
lstBoxItemsClear()
ArraySort(data)
display the sorted data in the listbox
For i = 0 To N - 1
lstBoxItemsAdd(CStr(data(i)))
NextEnd Sub
Private Sub cmdCalculate_Click(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles cmdCalculateClick
CalculateAndDisplayStats()
End Sub
Button
cmdSort
bull Program code
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 232623
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Private Sub CalculateAndDisplayStats()
Dim minD As Integer = data(0)Dim maxD As Integer = data(0)
Dim total As Integer = data(0)Dim mean As Double
Dim i As Integer
For i = 1 To N - 1
total += data(i)
If data(i) lt minD Then
minD = data(i)End If
If data(i) gt maxD Then
maxD = data(i)End If
Nextmean = MathRound(total N 2)
lblMinText = Min amp CStr(minD)
lblMaxText = Max amp CStr(maxD)
lblMeanText = Mean amp CStr(mean)
End Sub
End Class
Example (6)
Display
results
Calculate
the Min
Max mean
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 242624
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (1)
bull An array can be passed to a method byincluding the array name in the argument list
eg
bull For a method to receive an array through a callthe parameter list must indicate that an arraywill be received
eg
Function Average(ByVal arr() As Integer) As Double
mean = Average(courseMarks)
ArrayFunction call
Function Average
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 252625
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (2)
bull eg Calculate the average course mark
Dim courseMarks() As Integer = 85 70 90 65
Dim mean As Double
mean = Average(courseMarks)
MessageBoxShow( Mean amp CStr(mean) )
Private Function Average(ByVal arr() As Integer) As Double
Dim i As IntegerDim count As Integer = 0
Dim total As Integer = 0
For i = 0 To arrLength -1total += arr(i)count += 1
Next
Return MathRound(total count 1)
End Function
Note The size of the
array arr is not
specified ndash it can be
of any sizeFor
loopArray
property
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2626
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Summary
bull An array is a collection of data with a single name
bull All the elements in an array are of the same type
bull The index of an array starts at 0
bull All arrays have access to methods and properties of SystemArray class eg
Length getUpperBound(0)
Sort
bull Referencing array elements outside the array boundscauses a runtime error
bull The size of an can be changed at run-time using ReDim
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 182618
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (1)
bull Ten random numbers
Random numbers
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 192619
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (2)
bull Program requirements
generate and display 10 random numbers inthe range 1 to 100
sort the numbers
calculate and display the min max and meanof 10 numbers generated
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2026
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2126
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 222622
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (5)
Private Sub cmdSort_Click(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles cmdSortClick
Dim i As Integer
clear the listbox
lstBoxItemsClear()
ArraySort(data)
display the sorted data in the listbox
For i = 0 To N - 1
lstBoxItemsAdd(CStr(data(i)))
NextEnd Sub
Private Sub cmdCalculate_Click(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles cmdCalculateClick
CalculateAndDisplayStats()
End Sub
Button
cmdSort
bull Program code
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 232623
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Private Sub CalculateAndDisplayStats()
Dim minD As Integer = data(0)Dim maxD As Integer = data(0)
Dim total As Integer = data(0)Dim mean As Double
Dim i As Integer
For i = 1 To N - 1
total += data(i)
If data(i) lt minD Then
minD = data(i)End If
If data(i) gt maxD Then
maxD = data(i)End If
Nextmean = MathRound(total N 2)
lblMinText = Min amp CStr(minD)
lblMaxText = Max amp CStr(maxD)
lblMeanText = Mean amp CStr(mean)
End Sub
End Class
Example (6)
Display
results
Calculate
the Min
Max mean
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 242624
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (1)
bull An array can be passed to a method byincluding the array name in the argument list
eg
bull For a method to receive an array through a callthe parameter list must indicate that an arraywill be received
eg
Function Average(ByVal arr() As Integer) As Double
mean = Average(courseMarks)
ArrayFunction call
Function Average
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 252625
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (2)
bull eg Calculate the average course mark
Dim courseMarks() As Integer = 85 70 90 65
Dim mean As Double
mean = Average(courseMarks)
MessageBoxShow( Mean amp CStr(mean) )
Private Function Average(ByVal arr() As Integer) As Double
Dim i As IntegerDim count As Integer = 0
Dim total As Integer = 0
For i = 0 To arrLength -1total += arr(i)count += 1
Next
Return MathRound(total count 1)
End Function
Note The size of the
array arr is not
specified ndash it can be
of any sizeFor
loopArray
property
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2626
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Summary
bull An array is a collection of data with a single name
bull All the elements in an array are of the same type
bull The index of an array starts at 0
bull All arrays have access to methods and properties of SystemArray class eg
Length getUpperBound(0)
Sort
bull Referencing array elements outside the array boundscauses a runtime error
bull The size of an can be changed at run-time using ReDim
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 192619
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (2)
bull Program requirements
generate and display 10 random numbers inthe range 1 to 100
sort the numbers
calculate and display the min max and meanof 10 numbers generated
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2026
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2126
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 222622
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (5)
Private Sub cmdSort_Click(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles cmdSortClick
Dim i As Integer
clear the listbox
lstBoxItemsClear()
ArraySort(data)
display the sorted data in the listbox
For i = 0 To N - 1
lstBoxItemsAdd(CStr(data(i)))
NextEnd Sub
Private Sub cmdCalculate_Click(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles cmdCalculateClick
CalculateAndDisplayStats()
End Sub
Button
cmdSort
bull Program code
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 232623
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Private Sub CalculateAndDisplayStats()
Dim minD As Integer = data(0)Dim maxD As Integer = data(0)
Dim total As Integer = data(0)Dim mean As Double
Dim i As Integer
For i = 1 To N - 1
total += data(i)
If data(i) lt minD Then
minD = data(i)End If
If data(i) gt maxD Then
maxD = data(i)End If
Nextmean = MathRound(total N 2)
lblMinText = Min amp CStr(minD)
lblMaxText = Max amp CStr(maxD)
lblMeanText = Mean amp CStr(mean)
End Sub
End Class
Example (6)
Display
results
Calculate
the Min
Max mean
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 242624
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (1)
bull An array can be passed to a method byincluding the array name in the argument list
eg
bull For a method to receive an array through a callthe parameter list must indicate that an arraywill be received
eg
Function Average(ByVal arr() As Integer) As Double
mean = Average(courseMarks)
ArrayFunction call
Function Average
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 252625
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (2)
bull eg Calculate the average course mark
Dim courseMarks() As Integer = 85 70 90 65
Dim mean As Double
mean = Average(courseMarks)
MessageBoxShow( Mean amp CStr(mean) )
Private Function Average(ByVal arr() As Integer) As Double
Dim i As IntegerDim count As Integer = 0
Dim total As Integer = 0
For i = 0 To arrLength -1total += arr(i)count += 1
Next
Return MathRound(total count 1)
End Function
Note The size of the
array arr is not
specified ndash it can be
of any sizeFor
loopArray
property
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2626
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Summary
bull An array is a collection of data with a single name
bull All the elements in an array are of the same type
bull The index of an array starts at 0
bull All arrays have access to methods and properties of SystemArray class eg
Length getUpperBound(0)
Sort
bull Referencing array elements outside the array boundscauses a runtime error
bull The size of an can be changed at run-time using ReDim
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2026
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2126
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 222622
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (5)
Private Sub cmdSort_Click(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles cmdSortClick
Dim i As Integer
clear the listbox
lstBoxItemsClear()
ArraySort(data)
display the sorted data in the listbox
For i = 0 To N - 1
lstBoxItemsAdd(CStr(data(i)))
NextEnd Sub
Private Sub cmdCalculate_Click(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles cmdCalculateClick
CalculateAndDisplayStats()
End Sub
Button
cmdSort
bull Program code
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 232623
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Private Sub CalculateAndDisplayStats()
Dim minD As Integer = data(0)Dim maxD As Integer = data(0)
Dim total As Integer = data(0)Dim mean As Double
Dim i As Integer
For i = 1 To N - 1
total += data(i)
If data(i) lt minD Then
minD = data(i)End If
If data(i) gt maxD Then
maxD = data(i)End If
Nextmean = MathRound(total N 2)
lblMinText = Min amp CStr(minD)
lblMaxText = Max amp CStr(maxD)
lblMeanText = Mean amp CStr(mean)
End Sub
End Class
Example (6)
Display
results
Calculate
the Min
Max mean
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 242624
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (1)
bull An array can be passed to a method byincluding the array name in the argument list
eg
bull For a method to receive an array through a callthe parameter list must indicate that an arraywill be received
eg
Function Average(ByVal arr() As Integer) As Double
mean = Average(courseMarks)
ArrayFunction call
Function Average
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 252625
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (2)
bull eg Calculate the average course mark
Dim courseMarks() As Integer = 85 70 90 65
Dim mean As Double
mean = Average(courseMarks)
MessageBoxShow( Mean amp CStr(mean) )
Private Function Average(ByVal arr() As Integer) As Double
Dim i As IntegerDim count As Integer = 0
Dim total As Integer = 0
For i = 0 To arrLength -1total += arr(i)count += 1
Next
Return MathRound(total count 1)
End Function
Note The size of the
array arr is not
specified ndash it can be
of any sizeFor
loopArray
property
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2626
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Summary
bull An array is a collection of data with a single name
bull All the elements in an array are of the same type
bull The index of an array starts at 0
bull All arrays have access to methods and properties of SystemArray class eg
Length getUpperBound(0)
Sort
bull Referencing array elements outside the array boundscauses a runtime error
bull The size of an can be changed at run-time using ReDim
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2126
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 222622
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (5)
Private Sub cmdSort_Click(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles cmdSortClick
Dim i As Integer
clear the listbox
lstBoxItemsClear()
ArraySort(data)
display the sorted data in the listbox
For i = 0 To N - 1
lstBoxItemsAdd(CStr(data(i)))
NextEnd Sub
Private Sub cmdCalculate_Click(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles cmdCalculateClick
CalculateAndDisplayStats()
End Sub
Button
cmdSort
bull Program code
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 232623
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Private Sub CalculateAndDisplayStats()
Dim minD As Integer = data(0)Dim maxD As Integer = data(0)
Dim total As Integer = data(0)Dim mean As Double
Dim i As Integer
For i = 1 To N - 1
total += data(i)
If data(i) lt minD Then
minD = data(i)End If
If data(i) gt maxD Then
maxD = data(i)End If
Nextmean = MathRound(total N 2)
lblMinText = Min amp CStr(minD)
lblMaxText = Max amp CStr(maxD)
lblMeanText = Mean amp CStr(mean)
End Sub
End Class
Example (6)
Display
results
Calculate
the Min
Max mean
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 242624
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (1)
bull An array can be passed to a method byincluding the array name in the argument list
eg
bull For a method to receive an array through a callthe parameter list must indicate that an arraywill be received
eg
Function Average(ByVal arr() As Integer) As Double
mean = Average(courseMarks)
ArrayFunction call
Function Average
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 252625
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (2)
bull eg Calculate the average course mark
Dim courseMarks() As Integer = 85 70 90 65
Dim mean As Double
mean = Average(courseMarks)
MessageBoxShow( Mean amp CStr(mean) )
Private Function Average(ByVal arr() As Integer) As Double
Dim i As IntegerDim count As Integer = 0
Dim total As Integer = 0
For i = 0 To arrLength -1total += arr(i)count += 1
Next
Return MathRound(total count 1)
End Function
Note The size of the
array arr is not
specified ndash it can be
of any sizeFor
loopArray
property
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2626
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Summary
bull An array is a collection of data with a single name
bull All the elements in an array are of the same type
bull The index of an array starts at 0
bull All arrays have access to methods and properties of SystemArray class eg
Length getUpperBound(0)
Sort
bull Referencing array elements outside the array boundscauses a runtime error
bull The size of an can be changed at run-time using ReDim
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 222622
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Example (5)
Private Sub cmdSort_Click(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles cmdSortClick
Dim i As Integer
clear the listbox
lstBoxItemsClear()
ArraySort(data)
display the sorted data in the listbox
For i = 0 To N - 1
lstBoxItemsAdd(CStr(data(i)))
NextEnd Sub
Private Sub cmdCalculate_Click(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles cmdCalculateClick
CalculateAndDisplayStats()
End Sub
Button
cmdSort
bull Program code
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 232623
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Private Sub CalculateAndDisplayStats()
Dim minD As Integer = data(0)Dim maxD As Integer = data(0)
Dim total As Integer = data(0)Dim mean As Double
Dim i As Integer
For i = 1 To N - 1
total += data(i)
If data(i) lt minD Then
minD = data(i)End If
If data(i) gt maxD Then
maxD = data(i)End If
Nextmean = MathRound(total N 2)
lblMinText = Min amp CStr(minD)
lblMaxText = Max amp CStr(maxD)
lblMeanText = Mean amp CStr(mean)
End Sub
End Class
Example (6)
Display
results
Calculate
the Min
Max mean
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 242624
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (1)
bull An array can be passed to a method byincluding the array name in the argument list
eg
bull For a method to receive an array through a callthe parameter list must indicate that an arraywill be received
eg
Function Average(ByVal arr() As Integer) As Double
mean = Average(courseMarks)
ArrayFunction call
Function Average
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 252625
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (2)
bull eg Calculate the average course mark
Dim courseMarks() As Integer = 85 70 90 65
Dim mean As Double
mean = Average(courseMarks)
MessageBoxShow( Mean amp CStr(mean) )
Private Function Average(ByVal arr() As Integer) As Double
Dim i As IntegerDim count As Integer = 0
Dim total As Integer = 0
For i = 0 To arrLength -1total += arr(i)count += 1
Next
Return MathRound(total count 1)
End Function
Note The size of the
array arr is not
specified ndash it can be
of any sizeFor
loopArray
property
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2626
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Summary
bull An array is a collection of data with a single name
bull All the elements in an array are of the same type
bull The index of an array starts at 0
bull All arrays have access to methods and properties of SystemArray class eg
Length getUpperBound(0)
Sort
bull Referencing array elements outside the array boundscauses a runtime error
bull The size of an can be changed at run-time using ReDim
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 232623
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Private Sub CalculateAndDisplayStats()
Dim minD As Integer = data(0)Dim maxD As Integer = data(0)
Dim total As Integer = data(0)Dim mean As Double
Dim i As Integer
For i = 1 To N - 1
total += data(i)
If data(i) lt minD Then
minD = data(i)End If
If data(i) gt maxD Then
maxD = data(i)End If
Nextmean = MathRound(total N 2)
lblMinText = Min amp CStr(minD)
lblMaxText = Max amp CStr(maxD)
lblMeanText = Mean amp CStr(mean)
End Sub
End Class
Example (6)
Display
results
Calculate
the Min
Max mean
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 242624
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (1)
bull An array can be passed to a method byincluding the array name in the argument list
eg
bull For a method to receive an array through a callthe parameter list must indicate that an arraywill be received
eg
Function Average(ByVal arr() As Integer) As Double
mean = Average(courseMarks)
ArrayFunction call
Function Average
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 252625
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (2)
bull eg Calculate the average course mark
Dim courseMarks() As Integer = 85 70 90 65
Dim mean As Double
mean = Average(courseMarks)
MessageBoxShow( Mean amp CStr(mean) )
Private Function Average(ByVal arr() As Integer) As Double
Dim i As IntegerDim count As Integer = 0
Dim total As Integer = 0
For i = 0 To arrLength -1total += arr(i)count += 1
Next
Return MathRound(total count 1)
End Function
Note The size of the
array arr is not
specified ndash it can be
of any sizeFor
loopArray
property
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2626
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Summary
bull An array is a collection of data with a single name
bull All the elements in an array are of the same type
bull The index of an array starts at 0
bull All arrays have access to methods and properties of SystemArray class eg
Length getUpperBound(0)
Sort
bull Referencing array elements outside the array boundscauses a runtime error
bull The size of an can be changed at run-time using ReDim
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 242624
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (1)
bull An array can be passed to a method byincluding the array name in the argument list
eg
bull For a method to receive an array through a callthe parameter list must indicate that an arraywill be received
eg
Function Average(ByVal arr() As Integer) As Double
mean = Average(courseMarks)
ArrayFunction call
Function Average
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 252625
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (2)
bull eg Calculate the average course mark
Dim courseMarks() As Integer = 85 70 90 65
Dim mean As Double
mean = Average(courseMarks)
MessageBoxShow( Mean amp CStr(mean) )
Private Function Average(ByVal arr() As Integer) As Double
Dim i As IntegerDim count As Integer = 0
Dim total As Integer = 0
For i = 0 To arrLength -1total += arr(i)count += 1
Next
Return MathRound(total count 1)
End Function
Note The size of the
array arr is not
specified ndash it can be
of any sizeFor
loopArray
property
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2626
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Summary
bull An array is a collection of data with a single name
bull All the elements in an array are of the same type
bull The index of an array starts at 0
bull All arrays have access to methods and properties of SystemArray class eg
Length getUpperBound(0)
Sort
bull Referencing array elements outside the array boundscauses a runtime error
bull The size of an can be changed at run-time using ReDim
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 252625
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Passing Arrays (2)
bull eg Calculate the average course mark
Dim courseMarks() As Integer = 85 70 90 65
Dim mean As Double
mean = Average(courseMarks)
MessageBoxShow( Mean amp CStr(mean) )
Private Function Average(ByVal arr() As Integer) As Double
Dim i As IntegerDim count As Integer = 0
Dim total As Integer = 0
For i = 0 To arrLength -1total += arr(i)count += 1
Next
Return MathRound(total count 1)
End Function
Note The size of the
array arr is not
specified ndash it can be
of any sizeFor
loopArray
property
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2626
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Summary
bull An array is a collection of data with a single name
bull All the elements in an array are of the same type
bull The index of an array starts at 0
bull All arrays have access to methods and properties of SystemArray class eg
Length getUpperBound(0)
Sort
bull Referencing array elements outside the array boundscauses a runtime error
bull The size of an can be changed at run-time using ReDim
8142019 Lecture (11)One-Dimensional Arrays
httpslidepdfcomreaderfulllecture-11one-dimensional-arrays 2626
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
FSB23103
Summary
bull An array is a collection of data with a single name
bull All the elements in an array are of the same type
bull The index of an array starts at 0
bull All arrays have access to methods and properties of SystemArray class eg
Length getUpperBound(0)
Sort
bull Referencing array elements outside the array boundscauses a runtime error
bull The size of an can be changed at run-time using ReDim
top related