arrays and others. annoucement today’s office hour move to friday 1:00pm to 3:00pm today’s...

22
Arrays and others

Upload: owen-holmes

Post on 18-Jan-2016

215 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 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

Arrays and others

Page 2: 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

Annoucement

Today’s office hour move to Friday 1:00PM to Today’s office hour move to Friday 1:00PM to 3:00PM3:00PM

TodayToday Call by reference and call by valueCall by reference and call by value Variable scopesVariable scopes ArraysArrays DebuggingDebugging

Page 3: 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

Call by reference

Passing the whole variable memory block to the Passing the whole variable memory block to the sub proceduresub 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 Private Sub Add(num1 as integer, num2 as integer)as integer)

Call Add(x,y)Call Add(x,y)

Page 4: 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

Call by value

Only copy the value of the variable to the sub Only copy the value of the variable to the sub procedure.procedure.

Inside changes will not affect the outside Inside changes will not affect the outside variables.variables.

How:How: Private Sub Add(num1 as integer, num2 Private Sub Add(num1 as integer, num2 as integer)as integer)

Call Add((x),y)Call Add((x),y) Call by reference and call by value also apply to Call by reference and call by value also apply to

the function procedure.the function procedure. When to use.When to use.

Page 5: 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

Scope of a variable

Local scope. Local scope. Variable declared in a sub procedure Variable declared in a sub procedure It only exists in this sub procedure.It only exists in this sub procedure.

Form-level scope. Form-level scope. Variable declared out of any sub Variable declared out of any sub

procedure. procedure. Any sub procedure can read it.Any sub procedure can read it.

Page 6: 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

Arrays

Array is a special type of variableArray is a special type of variable Regular Variables hold one valueRegular Variables hold one value Arrays hold may values - subscriptsArrays hold may values - subscripts

XX(1)(1), X, X(2)(2), X, X(3)(3), X, X(4)(4), X, X(5)(5) Arrays are like subscripts – refer to different Arrays are like subscripts – refer to different

values stored in the same variablevalues stored in the same variable Pp. 175-187Pp. 175-187

Page 7: 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

What Arrays Do

Suppose Data is an array.Suppose Data is an array.

Data(1)Data(1) Data(2)Data(2) Data(3)Data(3) Data(4)Data(4) Data(5)Data(5)

First First valuevalue

Second Second valuevalue

Third Third valuevalue

Fourth Fourth valuevalue

Fifth Fifth valuevalue

22.4922.49 13.4213.42 22.1722.17 64.1164.11 7.337.33

Page 8: 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

Declaring Arrays

Use Dim key word to declare arrays, just as Use Dim key word to declare arrays, just as what we do for variables.what we do for variables. Dim Data(1 to 50) As SingleDim Data(1 to 50) As Single Dim Species(1 to 4) As StringDim Species(1 to 4) As String

Accessing values in an array with subscript.Accessing values in an array with subscript. Data(Data(4444) = 22.5;) = 22.5; Species(Species(11) = “ACTGACTCGTAACGT”) = “ACTGACTCGTAACGT”

Red NumberRed Number is is INDEXINDEX

Page 9: 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

Matrices: 2-dimensional Arrays

DeclarationDeclaration Dim Data(1 to 50Dim Data(1 to 50, 1 to 20, 1 to 20) As ) As SingleSingle

AccessingAccessing Data(2,4)=100Data(2,4)=100

The range of the subscript can be between any The range of the subscript can be between any integers.integers. Dim Dim WeekendMorningWeekendMorning((66 to to 7, 7 to 7, 7 to 1212) As ) As StringString

Page 10: 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

Passing array to a sub procedure

Array can be passed as an argument to sub Array can be passed as an argument to sub procedures of function procedures.procedures of function procedures. Private Sub ProcName(ArrayName() as Private Sub ProcName(ArrayName() as Integer)Integer)

Call ProcName(Array1)Call ProcName(Array1) Array can only be called by reference.Array can only be called by reference. Function Function Ubound(ArrayName,2)Ubound(ArrayName,2)

Page 11: 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

ReDim

Use ReDim when declaring an Use ReDim when declaring an array sizearray size based on a based on a variablevariable

Also called dynamic arrayAlso called dynamic array User tells you there are 50 values (count = User tells you there are 50 values (count =

50)50) ReDim Values(1 to count) as SingleReDim Values(1 to count) as Single

Page 12: 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

Decimal to Binary calculator

Page 13: 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

Flow chartStart

Get a number

Divide the Number

By 2

Is the quotient Equal to 1?

PrintThe Remainder

to the left of previous remains

No

Print 1 To the left of

Previous remaindersEnd

YesOutput number

If numberEqual to 1 or

0

Yes

No

Page 14: 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

Convert the first decision to code

IF number <> 0 OR number <> 1 THENIF number <> 0 OR number <> 1 THEN

Do the conversion partDo the conversion partEND IFEND IF

Call OutputNumber(number)Call OutputNumber(number)

Page 15: 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

Code fragment for the loop

Do While quotient <> 1Do While quotient <> 1

quotient = number \ 2quotient = number \ 2

reminder = number mod 2reminder = number mod 2

number = quotientnumber = quotient

call PrintReminder()call PrintReminder()

LoopLoop

Page 16: 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

Problems

So far we have already solved the So far we have already solved the Decision structure and loop Decision structure and loop structure.structure.

Problems not solved.Problems not solved. All previous reminders need to be All previous reminders need to be

stored.stored. How many?How many?

Print 1 To the left of

Previous remainders

Page 17: 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

Steps

First we need determine how many places in the First we need determine how many places in the converted binary numberconverted binary number Int(Log(Dec) / Log(2)) + 1Int(Log(Dec) / Log(2)) + 1

Second, declare a dynamic array to store all the Second, declare a dynamic array to store all the output numbers.output numbers. ReDim Bin(1 to binplaces) as ReDim Bin(1 to binplaces) as StringString

In the do while loop, put reminders of each step in In the do while loop, put reminders of each step in the Bin array. the Bin array. Beware of the orderBeware of the order

Page 18: 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

Output function

Private Sub Output(rems() As Private Sub Output(rems() As String)String)

‘‘Print the binary array in the Print the binary array in the picOutput picture boxpicOutput picture box

End SubEnd Sub

Output number

Page 19: 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

Debugging tools in VB

Break pointsBreak points

Page 20: 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

Debugging tools in VB(2)

Immediate WindowImmediate Window

Page 21: 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

Debugging tools in VB(3)

Watch window and local windowWatch window and local window

Page 22: 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

Friday

Do the program that convert 10 base Do the program that convert 10 base value to binary.value to binary.