COM148X1 Interactive Programming

Post on 31-Dec-2015

37 views 1 download

Tags:

description

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 - PowerPoint PPT Presentation

transcript

COM148X1Interactive 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

There are commonly 5 symbols used in flowchart, symbols in flowchart are connected by arrows terminal input/ output process decision connector

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 Symbols

Flowchart Example

salary =salary + 300

Start

working year > 3?

Enter working year

End

Enter salary

Output salary

yes

no

Output“bonus avail.”

A

B

BA

Pseudo-Code

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

BeginRead working year from userRead salary from userIf working year > 3 then

salary ← salary + 300Output “Bonus Available”

End ifOutput salary

End

Design Approach

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

Calculate the Net-Profit

Calculate the Cost Calculate the Gain

Calculate Workers Salary

Calculate Tax

Calculate Insurance

direction of breakdown

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

Calculate the Net-Profit

Calculate the Cost Calculate the Gain

Calculate Workers Salary

Calculate Tax

Calculate Insurance

direction of compose

Array

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

0

a

0

0

1

0

2

0

3

0

4

0

5

Access Array Data

array_name( index ) Example

a(3) = 10 ‘write array Dim b As Integer = a(3) ‘read array

a

10

b

0

0

0

1

0

2

10

3

0

4

0

5

UBound Function

Obtain the largest index of an array Example

For j = 0 To UBound(a)a(i) = 20

Next j

20

a

0

20

1

20

2

20

3

20

4

20

5

Resize an Array

Redim array_name(max_index) Redim Preserve array_name(max_index) If keyword Preserve is used, the data in origi

nal array will be copied to the new array as long as the new array can hold

Redim Example 1

Redim a(3)

60

a

0

44

1

81

2

72

3

90

4

9

5

Before Redim

0

a

0

0

1

0

2

0

3

After Redim

Redim Example 2

Redim Preserve a(3)

60

a

0

44

1

81

2

72

3

90

4

9

5

Before Redim

60

a

0

44

1

81

2

72

3

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

0

b

0

0

1

0

2

13

3

0

4

0

5

0 0 0 0 0 270

1

Collection

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()

Access Data in Collection

Add data to Collection collection.Add(data, key) marks.Add(10,”Jackson”)

Retrieve data from Collection

collection(key)collection(index)

x = marks(“Jackson”)x = marks(0)

Remove data from Collection

collection.Remove(key)collection.Remove(index)

marks.Remove(“Jackson”)marks.Remove(0)

Count number of data in Collection

collection.Count() cnt = marks.Count()