Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays and others. Annoucement Today’s office hour move to Friday 1:00PM to 3:00PM Today’s office hour move to Friday 1:00PM to 3:00PM Today Today  Call.

Similar presentations


Presentation on theme: "Arrays and others. Annoucement Today’s office hour move to Friday 1:00PM to 3:00PM Today’s office hour move to Friday 1:00PM to 3:00PM Today Today  Call."— Presentation transcript:

1 Arrays and others

2 Annoucement Today’s office hour move to Friday 1:00PM to 3:00PM Today’s office hour move to Friday 1:00PM to 3:00PM Today Today  Call by reference and call by value  Variable scopes  Arrays  Debugging

3 Call by reference Passing the whole variable memory block to the sub procedure Passing the whole variable memory block to the sub procedure If inside is changed, outside will be changed too. If inside is changed, outside will be changed too. How: How:  Private Sub Add(num1 as integer, num2 as integer)  Call Add(x,y)

4 Call by value Only copy the value of the variable to the sub procedure. Only copy the value of the variable to the sub procedure. Inside changes will not affect the outside variables. Inside changes will not affect the outside variables. How: 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. Call by reference and call by value also apply to the function procedure. When to use. When to use.

5 Scope of a variable Local scope. Local scope.  Variable declared in a sub procedure  It only exists in this sub procedure. Form-level scope. Form-level scope.  Variable declared out of any sub procedure.  Any sub procedure can read it.

6 Arrays Array is a special type of variable Array is a special type of variable Regular Variables hold one value Regular Variables hold one value Arrays hold may values - subscripts Arrays hold may values - subscripts X(1), X(2), X(3), X(4), X(5) X(1), X(2), X(3), X(4), X(5) Arrays are like subscripts – refer to different values stored in the same variable Arrays are like subscripts – refer to different values stored in the same variable Pp. 175-187 Pp. 175-187

7 What Arrays Do Suppose Data is an array. 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.4913.4222.1764.117.33

8 Declaring Arrays Use Dim key word to declare arrays, just as what we do for variables. 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. Accessing values in an array with subscript.  Data(44) = 22.5;  Species(1) = “ACTGACTCGTAACGT” Red Number is INDEX Red Number is INDEX

9 Matrices: 2-dimensional Arrays Declaration Declaration  Dim Data(1 to 50, 1 to 20) As Single Accessing Accessing  Data(2,4)=100 The range of the subscript can be between any integers. The range of the subscript can be between any integers.  Dim WeekendMorning(6 to 7, 7 to 12) As String

10 Passing array to a sub procedure Array can be passed as an argument to sub procedures of function procedures. 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. Array can only be called by reference. Function Ubound(ArrayName,2) Function Ubound(ArrayName,2)

11 ReDim Use ReDim when declaring an array size based on a variable Use ReDim when declaring an array size based on a variable Also called dynamic array Also called dynamic array User tells you there are 50 values (count = 50) User tells you there are 50 values (count = 50) ReDim Values(1 to count) as Single ReDim Values(1 to count) as Single

12 Decimal to Binary calculator

13 Flow chart Start Get a number Divide the Number By 2 Is the quotient Equal to 1? Print The Remainder to the left of previous remains No Print 1 To the left of Previous remainders End Yes Output number If number Equal to 1 or 0 Yes No

14 Convert the first decision to code IF number <> 0 OR number <> 1 THEN Do the conversion part END IF Call OutputNumber(number)

15 Code fragment for the loop Do While quotient <> 1 quotient = number \ 2 reminder = number mod 2 number = quotient call PrintReminder() Loop

16 Problems So far we have already solved the Decision structure and loop structure. So far we have already solved the Decision structure and loop structure. Problems not solved. Problems not solved.  All previous reminders need to be stored.  How many? Print 1 To the left of Previous remainders

17 Steps First we need determine how many places in the converted binary number 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. 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 In the do while loop, put reminders of each step in the Bin array. Beware of the order

18 Output function Private Sub Output(rems() As String) ‘Print the binary array in the picOutput picture box End Sub Output number

19 Debugging tools in VB Break points Break points

20 Debugging tools in VB(2) Immediate Window Immediate Window

21 Debugging tools in VB(3) Watch window and local window Watch window and local window

22 Friday  Do the program that convert 10 base value to binary.


Download ppt "Arrays and others. Annoucement Today’s office hour move to Friday 1:00PM to 3:00PM Today’s office hour move to Friday 1:00PM to 3:00PM Today Today  Call."

Similar presentations


Ads by Google