Title: Arrays and others
1Arrays and others
2Annoucement
- Todays office hour move to Friday 100PM to
300PM - Today
- Call by reference and call by value
- Variable scopes
- Arrays
- Debugging
3Call by reference
- Passing the whole variable memory block to the
sub procedure - If inside is changed, outside will be changed
too. - How
- Private Sub Add(num1 as integer, num2 as integer)
- Call Add(x,y)
4Call by value
- Only copy the value of the variable to the sub
procedure. - Inside changes will not affect the outside
variables. - How
- Private Sub Add(num1 as integer, num2 as integer)
- Call Add((x),y)
- Call by reference and call by value also apply to
the function procedure. - When to use.
5Scope of a variable
- Local scope.
- Variable declared in a sub procedure
- It only exists in this sub procedure.
- Form-level scope.
- Variable declared out of any sub procedure.
- Any sub procedure can read it.
6Arrays
- Array is a special type of variable
- Regular Variables hold one value
- Arrays hold may values - subscripts
- X(1), X(2), X(3), X(4), X(5)
- Arrays are like subscripts refer to different
values stored in the same variable - Pp. 175-187
7What Arrays Do
- Suppose Data is an array.
Data(1) Data(2) Data(3) Data(4) Data(5)
First value Second value Third value Fourth value Fifth value
22.49 13.42 22.17 64.11 7.33
8Declaring Arrays
- Use Dim key word to declare arrays, just as what
we do for variables. - Dim Data(1 to 50) As Single
- Dim Species(1 to 4) As String
- Accessing values in an array with subscript.
- Data(44) 22.5
- Species(1) ACTGACTCGTAACGT
- Red Number is INDEX
9Matrices 2-dimensional Arrays
- Declaration
- Dim Data(1 to 50, 1 to 20) As Single
- Accessing
- Data(2,4)100
- The range of the subscript can be between any
integers. - Dim WeekendMorning(6 to 7, 7 to 12) As String
10Passing array to a sub procedure
- Array can be passed as an argument to sub
procedures of function procedures. - Private Sub ProcName(ArrayName() as Integer)
- Call ProcName(Array1)
- Array can only be called by reference.
- Function Ubound(ArrayName,2)
11ReDim
- Use ReDim when declaring an array size based on a
variable - Also called dynamic array
- User tells you there are 50 values (count 50)
- ReDim Values(1 to count) as Single
12Decimal to Binary calculator
13Flow chart
Start
If number Equal to 1 or 0
Print The Remainder to the left of previous
remains
No
Divide the Number By 2
Get a number
Yes
Is the quotient Equal to 1?
No
Output number
Print 1 To the left of Previous remainders
Yes
End
14Convert the first decision to code
- IF number ltgt 0 OR number ltgt 1 THEN
- Do the conversion part
- END IF
- Call OutputNumber(number)
15Code fragment for the loop
- Do While quotient ltgt 1
- quotient number \ 2
- reminder number mod 2
- number quotient
- call PrintReminder()
- Loop
-
16Problems
Print 1 To the left of Previous remainders
- So far we have already solved the Decision
structure and loop structure. - Problems not solved.
- All previous reminders need to be stored.
- How many?
17Steps
- First we need determine how many places in the
converted binary number - Int(Log(Dec) / Log(2)) 1
- Second, declare a dynamic array to store all the
output numbers. - ReDim Bin(1 to binplaces) as String
- In the do while loop, put reminders of each step
in the Bin array. Beware of the order
18Output function
Output number
- Private Sub Output(rems() As String)
- Print the binary array in the picOutput picture
box - End Sub
19Debugging tools in VB
20Debugging tools in VB(2)
21Debugging tools in VB(3)
- Watch window and local window
22Friday
- Do the program that convert 10 base value to
binary.