Arrays and othersAnnoucementCall by referenceCall by valueScope of a variableArraysWhat Arrays DoDeclaring ArraysMatrices: 2-dimensional ArraysPassing array to a sub procedureReDimDecimal to Binary calculatorFlow chartConvert the first decision to codeCode fragment for the loopProblemsStepsOutput functionDebugging tools in VBDebugging tools in VB(2)Debugging tools in VB(3)FridayArrays and othersAnnoucementToday’s office hour move to Friday 1:00PM to Today’s office hour move to Friday 1:00PM to 3:00PM3:00PMTodayTodayCall by reference and call by valueCall by reference and call by valueVariable scopesVariable scopesArraysArraysDebuggingDebuggingCall by referencePassing the whole variable memory block to the Passing the whole variable memory block to the sub proceduresub procedureIf 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)Call by valueOnly 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 variables.Inside changes will not affect the outside 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.Scope of a variableLocal 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.ArraysArray is a special type of variableArray is a special type of variableRegular Variables hold one valueRegular Variables hold one valueArrays hold may values - subscriptsArrays hold may values - subscriptsXX(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 variablePp. 175-187Pp. 175-187What Arrays DoSuppose 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 valuevalueSecond Second valuevalueThird Third valuevalueFourth Fourth valuevalueFifth Fifth valuevalue22.4922.4913.4213.4222.1722.1764.1164.117.337.33Declaring ArraysUse 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 SingleDim Species(1 to 4) As StringDim Species(1 to 4) As StringAccessing 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 INDEXINDEXMatrices: 2-dimensional ArraysDeclarationDeclarationDim Data(1 to 50Dim Data(1 to 50, 1 to 20, 1 to 20) As ) As SingleSingleAccessingAccessingData(2,4)=100Data(2,4)=100The 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 StringStringPassing array to a sub procedureArray 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)ReDimUse ReDim when declaring an Use ReDim when declaring an array sizearray size based on a based on a variablevariableAlso called dynamic arrayAlso called dynamic arrayUser 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 SingleDecimal to Binary calculatorFlow chartStartGet a numberDivide the NumberBy 2Is the quotient Equal to 1?PrintThe Remainderto the left of previous remains NoPrint 1 To the left ofPrevious remaindersEndYesOutput numberIf numberEqual to 1 or0YesNoConvert the first decision to codeIF number <> 0 OR number <> 1 THENIF number <> 0 OR number <> 1 THENDo the conversion partDo the conversion partEND IFEND IFCall OutputNumber(number)Call OutputNumber(number)Code fragment for the loop Do While quotient <> 1Do While quotient <> 1quotient = number \ 2quotient = number \ 2reminder = number mod 2reminder = number mod 2number = quotientnumber = quotientcall PrintReminder()call PrintReminder()LoopLoopProblemsSo 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 ofPrevious remaindersStepsFirst we need determine how many places in the First we need determine how many places in the converted binary numberconverted binary numberInt(Log(Dec) / Log(2)) + 1Int(Log(Dec) / Log(2)) + 1Second, 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 StringStringIn 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 orderOutput functionPrivate 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 boxEnd SubEnd SubOutput numberDebugging tools in VBBreak pointsBreak pointsDebugging tools in VB(2)Immediate WindowImmediate WindowDebugging tools in VB(3)Watch window and local
View Full Document