Arrays and others - PowerPoint PPT Presentation

About This Presentation
Title:

Arrays and others

Description:

Decimal to Binary calculator. Flow chart. Start. Get a. number. Divide ... Do the conversion part. END IF. Call OutputNumber(number) Code fragment for the loop ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 23
Provided by: geraldru
Category:

less

Transcript and Presenter's Notes

Title: Arrays and others


1
Arrays and others
2
Annoucement
  • Todays office hour move to Friday 100PM to
    300PM
  • 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
  • If inside is changed, outside will be changed
    too.
  • 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.
  • 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.

5
Scope 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.

6
Arrays
  • 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

7
What 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
8
Declaring 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

9
Matrices 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

10
Passing 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)

11
ReDim
  • 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

12
Decimal to Binary calculator
13
Flow 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
14
Convert the first decision to code
  • IF number ltgt 0 OR number ltgt 1 THEN
  • Do the conversion part
  • END IF
  • Call OutputNumber(number)

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

16
Problems
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?

17
Steps
  • 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

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

19
Debugging tools in VB
  • Break points

20
Debugging tools in VB(2)
  • Immediate Window

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

22
Friday
  • Do the program that convert 10 base value to
    binary.
Write a Comment
User Comments (0)
About PowerShow.com