CSE1301 Computer Programming: Lecture 16 Algorithm Design: Components - PowerPoint PPT Presentation

1 / 31
About This Presentation
Title:

CSE1301 Computer Programming: Lecture 16 Algorithm Design: Components

Description:

hang up phone. pick up the phone. dial 9905-5788. say 'Hello Jian, it's Jim' ... hang up phone. loop while there is a name in the list. read the name. find the ... – PowerPoint PPT presentation

Number of Views:54
Avg rating:3.0/5.0
Slides: 32
Provided by: AnnNichols
Category:

less

Transcript and Presenter's Notes

Title: CSE1301 Computer Programming: Lecture 16 Algorithm Design: Components


1
CSE1301 Computer Programming Lecture
16Algorithm DesignComponents
2
Topics
  • Functions
  • Function Call
  • Parameters
  • Algorithm Design
  • Readings
  • DD Section 5.1 and 5.2

3
Example 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
4
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
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
5
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
6
Solution 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
7
Solution 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
8
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
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
10
Function 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
11
Function 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
12
Function 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)
13
Function 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
14
Function Call
inviteToParty ( Sam, 9876-5432 ) inviteToParty (
Jim, 9905-5788 ) inviteToParty ( Ann, 9544-2382
) inviteToParty ( Dru, 9455-2323 )
parameters listed in order
15
Function 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
16
Function Hierarchy
inviteToParty ( name , phone ) ringUp ( name
, phone ) askToParty ( ) sayGoodbye ( name
)
ringUp
askToParty
sayGoodbye
17
Function 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
18
Function 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.
19
Function 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
20
Function 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.
21
Function 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.
22
Function 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.
23
Function Hierarchy
inviteToParty (name)
ringUp (name)
askToParty ( )
sayGoodbye (name)
searchAddrBook ( name )
24
Function Hierarchy
inviteToParty (name)
ringUp (name)
askToParty ( )
sayGoodbye (name)
searchAddrBook ( name )
greetings ( name )
25
Top-Down Design
Recall
Invite to a party
26
Bottom-Up Design
  • Find phone number
  • Dial number
  • Introduce self
  • Invite
  • Say when
  • Say where
  • Say goodbye
  • Hang up phone

27
Bottom-Up Design
  • Find phone number
  • Dial number
  • Introduce self
  • Invite
  • Say when
  • Say where

Invite to a party
  • Say goodbye
  • Hang up phone

28
Bottom-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, ....

29
Golden 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.

30
Example
31
Summary
  • Functions
  • Function definition name, body, parameters.
  • Function call.
  • Bottom-Up Design

Next Lecture
  • Writing functions in C.
Write a Comment
User Comments (0)
About PowerShow.com