Unformatted text preview:

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 othersAnnoucement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DebuggingDebuggingCall 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)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 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 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.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-187What 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 valuevalueSecond Second valuevalueThird Third valuevalueFourth Fourth valuevalueFifth Fifth valuevalue22.4922.4913.4213.4222.1722.1764.1164.117.337.33Declaring 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 INDEXINDEXMatrices: 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 StringStringPassing 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)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 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()LoopLoopProblems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 ofPrevious remaindersSteps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 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 VBBreak pointsBreak pointsDebugging tools in VB(2)Immediate WindowImmediate WindowDebugging tools in VB(3)Watch window and local


View Full Document

MSU LBS 126 - Arrays

Course: Lbs 126-
Pages: 22
Download Arrays
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Arrays and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Arrays 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?