280 likes | 526 Views
COM148X1 Interactive Programming. Lecture 5. Topics Today. Flowchart Pseudo-Code Design Approach Array Collection. Flowchart. What is Flowchart. Flowchart is used to represent the design of the program flow
E N D
COM148X1Interactive Programming Lecture 5
Topics Today • Flowchart • Pseudo-Code • Design Approach • Array • Collection
What is Flowchart • Flowchart is used to represent the design of the program flow • There are commonly 5 symbols used in flowchart, symbols in flowchart are connected by arrows • terminal • input/ output • process • decision • connector
Flowchart Symbols • Terminal • used to represent the start/ end of the program • Input/ Output • Process • Decision • Connector • used to connect different parts of flowchart when the direct connection among these parts are messy
Flowchart Example A Start Output “bonus avail.” Enter working year Enter salary salary = salary + 300 working year > 3? B yes no Output salary A End B
Pseudo-Code • Pseudo-Code is tools other than flowchart to represent the design of program flow • Pseudo-Code looks like mixture of program code and human language (mostly English)
Pseudo-Code Example Begin Read working year from user Read salary from user If working year > 3 then salary ← salary + 300 Output “Bonus Available” End if Output salary End
Limitation of Flowchart and Pseudo-Code • Both flowchart and pseudo-code become less usable when the program logic becomes too complicated • Large program project required programmers to break down the tasks into small pieces until the tasks is simple enough to handle • There are two approaches • Top-down approach • Bottom-up approach
Top-down Approach • In top down approach, a task will be divided into different smaller tasks. If the smaller tasks’ complexity is still high, the smaller tasks will be further divided until all tasks are manageable
Top-down Approach direction of breakdown Calculate the Net-Profit Calculate the Cost Calculate the Gain Calculate Workers Salary Calculate Insurance Calculate Tax
Bottom-up Approach • Unlike top-down approach, bottom-up approach try to search any manageable tasks which can be use to accomplished the main tasks. Then compose all of them to become the main task
Bottom-up Approach direction of compose Calculate the Net-Profit Calculate the Cost Calculate the Gain Calculate Workers Salary Calculate Insurance Calculate Tax
What is Array • Array is a collection of data of same type • Size of array is fixed unless it is being asked to change • Array is random access • Index of array start from 0
Declare Array • Dim array_name(max_index) As data_type • Example • Dim a(5) As Integer a 0 0 0 0 0 0 0 1 2 3 4 5
Access Array Data • array_name( index ) • Example • a(3) = 10 ‘write array • Dim b As Integer = a(3) ‘read array a 0 0 0 10 0 0 0 1 2 3 4 5 b 10
UBound Function • Obtain the largest index of an array • Example • For j = 0 To UBound(a) a(i) = 20Next j a 20 20 20 20 20 20 0 1 2 3 4 5
Resize an Array • Redim array_name(max_index) • RedimPreserve array_name(max_index) • If keyword Preserve is used, the data in original array will be copied to the new array as long as the new array can hold
Redim Example 1 • Redim a(3) a a 60 44 81 72 90 9 0 0 0 0 0 1 2 3 4 5 0 1 2 3 Before Redim After Redim
Redim Example 2 • Redim Preserve a(3) a a 60 44 81 72 90 9 60 44 81 72 0 1 2 3 4 5 0 1 2 3 Before Redim After Redim
Multi-Dimension Array • Multi-Dimension Array is the array using multiple index for data access • Dim array_name(max_index1, max_index2, …) As data_type
Multi-Dimension Array Example • Dim b(1, 5) As Integerb(1, 3) = 13b(0, 5) = 27Dim i As Integer = UBound(b, 1) ‘i = 1Dim j As Integer = UBound(b, 2) ‘j = 5 b 0 0 0 0 0 27 0 0 0 0 13 0 0 1 0 1 2 3 4 5
What is Collection • Collection is a group of data which can be retrieved either by index or string • Collection index started from 1 • Dim collection_name As New Collection() • Example • Dim marks As New Collection()