Title: CSE1301 Computer Programming: Lecture 16 Algorithm Design: Components
1CSE1301 Computer Programming Lecture
16Algorithm DesignComponents
2Topics
- Functions
- Function Call
- Parameters
- Algorithm Design
- Readings
- DD Section 5.1 and 5.2
3Example inviteToParty
pick up the phone dial 9876-5432 say Hello Sam,
its Jim say Would you like to come to my
party? say Its on 16 April. say Its at 1
Wellington Road. if reply is Yes then say
Great! See you then. else say Catch up
with you sometime. say Bye, Sam. hang up phone
4pick up the phone dial 9876-5432 say Hello Sam,
its Jim say Would you like to come to my
party? say Its on 16 April. say Its at 1
Wellington Road. if reply is Yes then say
Great! See you then. else say Catch up
with you sometime. say Bye, Sam. hang up phone
pick up the phone dial 9544-2382 say Hello Ann,
its Jim say Would you like to come to my
party? say Its on 16 April. say Its at 1
Wellington Road. if reply is Yes then say
Great! See you then. else say Catch up
with you sometime. say Bye, Ann. hang up phone
pick up the phone dial 9905-5788 say Hello Jian,
its Jim say Would you like to come to my
party? say Its on 16 April. say Its at 1
Wellington Road. if reply is Yes then say
Great! See you then. else say Catch up
with you sometime. say Bye, Jian. hang up phone
pick up the phone dial 9455-2323 say Hello Dru,
its Jim say Would you like to come to my
party? say Its on 16 April. say Its at 1
Wellington Road. if reply is Yes then say
Great! See you then. else say Catch up
with you sometime. say Bye, Dru. hang up phone
5pick up the phone dial phone say Hello name,
its Jim say Would you like to come to my
party? say Its on 16 April. say Its at 1
Wellington Road. if reply is Yes then say
Great! See you then. else say Catch up
with you sometime. say Bye, name. hang up phone
6Solution 1.
loop while there is a name in the list read the
name find the phone number pick up the phone dial
phone say Hello name, its Jim say Would you
like to come to my party? say Its on 16
April. say Its at 1 Wellington Road. if reply
is Yes then say Great! See you then.
else say Catch up with you sometime. say Bye,
name. hang up phone go to next name in the list
7Solution 2.
pick up the phone dial phone say Hello name,
its Jim say Would you like to come to my
party? say Its on 16 April. say Its at 1
Wellington Road. if reply is Yes then say
Great! See you then. else say Catch up
with you sometime. say Bye, name. hang up phone
8pick up the phone dial 9876-5432 say Hello Sam,
its Jim say Would you like to come to my
party? say Its on 16 April. say Its at 1
Wellington Road. if reply is Yes then say
Great! See you then. else say Catch up
with you sometime. say Bye, Sam. hang up phone
pick up the phone dial 9544-2382 say Hello Ann,
its Jim say Would you like to come to my
party? say Its on 16 April. say Its at 1
Wellington Road. if reply is Yes then say
Great! See you then. else say Catch up
with you sometime. say Bye, Ann. hang up phone
pick up the phone dial 9905-5788 say Hello Jian,
its Jim say Would you like to come to my
party? say Its on 16 April. say Its at 1
Wellington Road. if reply is Yes then say
Great! See you then. else say Catch up
with you sometime. say Bye, Jian. hang up phone
pick up the phone dial 9455-2323 say Hello Dru,
its Jim say Would you like to come to my
party? say Its on 16 April. say Its at 1
Wellington Road. if reply is Yes then say
Great! See you then. else say Catch up
with you sometime. say Bye, Dru. hang up phone
9 do inviteToParty with Sam, 9876-5432
do inviteToParty with Ann, 9544-2382
do inviteToParty with Jim, 9905-5788
do inviteToParty with Dru, 9455-2323
10Function Definition
inviteToParty ( name , phone ) pick up the
phone dial phone say Hello name, its
Jim say Would you like to come to my
party? say Its on 16 April. say Its
at 1 Wellington Road. if reply is Yes
then say Great! See you then. else
say Catch up with you sometime. say Bye,
name. hang up phone
11Function Definition
inviteToParty ( name , phone ) pick up the
phone dial phone say Hello name, its
Jim say Would you like to come to my
party? say Its on 16 April. say Its
at 1 Wellington Road. if reply is Yes
then say Great! See you then. else
say Catch up with you sometime. say Bye,
name. hang up phone
function name
12Function Definition
inviteToParty ( name , phone ) pick up the
phone dial phone say Hello name, its
Jim say Would you like to come to my
party? say Its on 16 April. say Its
at 1 Wellington Road. if reply is Yes
then say Great! See you then. else
say Catch up with you sometime. say Bye,
name. hang up phone
parameters (arguments)
13Function Definition
inviteToParty ( name , phone ) pick up the
phone dial phone say Hello name, its
Jim say Would you like to come to my
party? say Its on 16 April. say Its
at 1 Wellington Road. if reply is Yes
then say Great! See you then. else
say Catch up with you sometime. say Bye,
name. hang up phone
14Function Call
inviteToParty ( Sam, 9876-5432 ) inviteToParty (
Jim, 9905-5788 ) inviteToParty ( Ann, 9544-2382
) inviteToParty ( Dru, 9455-2323 )
parameters listed in order
15Function Hierarchy
inviteToParty ( name , phone ) pick up the
phone dial phone say Hello name, its
Jim say Would you like to come to my
party? say Its on 16 April. say Its
at 1 Wellington Road. if reply is Yes
then say Great! See you then. else
say Catch up with you sometime. say Bye,
name. hang up phone
16Function Hierarchy
inviteToParty ( name , phone ) ringUp ( name
, phone ) askToParty ( ) sayGoodbye ( name
)
ringUp
askToParty
sayGoodbye
17Function Hierarchy
inviteToParty ( name , phone ) ringUp ( name
, phone ) askToParty ( ) sayGoodbye ( name
)
ringUp
askToParty
sayGoodbye
ringUp ( name , phone ) pick up the phone
dial phone say Hello name, its Jim
18Function Hierarchy
inviteToParty ( name , phone ) ringUp ( name
, phone ) askToParty ( ) sayGoodbye ( name
)
ringUp
askToParty
sayGoodbye
askToParty ( ) say Would you like to come
to my party? say Its on 16 April. say
Its at 1 Wellington Road.
19Function Hierarchy
inviteToParty ( name , phone ) ringUp ( name
, phone ) askToParty ( ) sayGoodbye ( name
)
ringUp
askToParty
sayGoodbye
inviteToParty ( name ) request for reply
if reply is Yes then say Great! See
you then. else say Catch up with you
sometime. say Bye, name. hang up
phone
20Function Hierarchy
inviteToParty ( name , phone, date , venue )
ringUp ( name , phone ) askToParty (date ,
venue ) sayGoodbye ( name )
More parameters.
ringUp
askToParty
sayGoodbye
Version 2
askToParty ( date , venue ) say Would you
like to come to my party? say Its on
date. say Its at venue.
21Function Hierarchy
inviteToParty (name, phone)
ringUp (name, phone)
askToParty ( )
sayGoodbye (name)
ringUp ( name , phone ) pick up the phone
dial phone say Hello name, its Jim
Given the name, search for phone number here.
22Function Hierarchy
inviteToParty (name)
ringUp (name)
askToParty ( )
sayGoodbye (name)
ringUp ( name ) set number to result of
searchAddrBook ( name ) pick up the phone
dial number say Hello name, its Jim
Fewer parameters.
23Function Hierarchy
inviteToParty (name)
ringUp (name)
askToParty ( )
sayGoodbye (name)
searchAddrBook ( name )
24Function Hierarchy
inviteToParty (name)
ringUp (name)
askToParty ( )
sayGoodbye (name)
searchAddrBook ( name )
greetings ( name )
25Top-Down Design
Recall
Invite to a party
26Bottom-Up Design
- Find phone number
- Dial number
- Introduce self
- Invite
- Say when
- Say where
- Say goodbye
- Hang up phone
27Bottom-Up Design
- Find phone number
- Dial number
- Introduce self
- Invite
- Say when
- Say where
Invite to a party
- Say goodbye
- Hang up phone
28Bottom-Up Design
- To solve a problem
- Start with simple sequences (primitives).
- eg. press, say, listen, put, set, add, repeat,
.... - Build components with these sequences to
accomplish a simple task (pseudo-primitives). - eg. dial, greetings, sayGoodbye, ....
- Build more complex sequences using these
components. - eg. ringUp, inviteToParty, ....
- Repeat until problem is solved.
- eg. inviteAllFriends, organiseParty, ....
29Golden Rule
Design Top-Down, but always build Bottom-Up.
- Code and test the simplest functions first.
- Test each component before using them to build
more complex components.
30Example
31Summary
- Functions
- Function definition name, body, parameters.
- Function call.
- Bottom-Up Design
Next Lecture