31
CSE1301 Computer Programming: Lecture 16 Algorithm Design: Components

CSE1301 Computer Programming: Lecture 16 Algorithm Design: Components

  • View
    224

  • Download
    0

Embed Size (px)

Citation preview

CSE1301 Computer Programming:

Lecture 16Algorithm Design:

Components

Topics

• Functions– Function Call– Parameters– Algorithm Design

• Readings: – D&D: Section 5.1 and 5.2

pick up the phonedial 9876-5432say “Hello Sam, it’s Jim”say “Would you like to come to my party?”say “It’s on 16 April.”say “It’s 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

Example: inviteToParty

pick up the phonedial 9876-5432say “Hello Sam, it’s Jim”say “Would you like to come to my party?”say “It’s on 16 April.”say “It’s 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 phonedial 9905-5788say “Hello Jian, it’s Jim”say “Would you like to come to my party?”say “It’s on 16 April.”say “It’s 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 phonedial 9544-2382say “Hello Ann, it’s Jim”say “Would you like to come to my party?”say “It’s on 16 April.”say “It’s 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 phonedial 9455-2323say “Hello Dru, it’s Jim”say “Would you like to come to my party?”say “It’s on 16 April.”say “It’s 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

pick up the phonedial phonesay “Hello name, it’s Jim”say “Would you like to come to my party?”say “It’s on 16 April.”say “It’s 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

loop while there is a name in the list{

read the namefind the phone numberpick up the phonedial phonesay “Hello name, it’s Jim”say “Would you like to come to my party?”say “It’s on 16 April.”say “It’s 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 phonego to next name in the list

}

Solution 1.

pick up the phonedial phonesay “Hello name, it’s Jim”say “Would you like to come to my party?”say “It’s on 16 April.”say “It’s 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

• Call this block of sequence by a name.

inviteToParty

Solution 2.

pick up the phonedial 9876-5432say “Hello Sam, it’s Jim”say “Would you like to come to my party?”say “It’s on 16 April.”say “It’s 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 phonedial 9905-5788say “Hello Jian, it’s Jim”say “Would you like to come to my party?”say “It’s on 16 April.”say “It’s 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 phonedial 9544-2382say “Hello Ann, it’s Jim”say “Would you like to come to my party?”say “It’s on 16 April.”say “It’s 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 phonedial 9455-2323say “Hello Dru, it’s Jim”say “Would you like to come to my party?”say “It’s on 16 April.”say “It’s 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

do inviteToParty with Sam, 9876-5432

do inviteToParty with Ann, 9544-2382

do inviteToParty with Jim, 9905-5788

do inviteToParty with Dru, 9455-2323

inviteToParty ( name , phone ){ pick up the phone dial phone say “Hello name, it’s Jim” say “Would you like to come to my party?” say “It’s on 16 April.” say “It’s 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 Definition

inviteToParty ( name , phone ){ pick up the phone dial phone say “Hello name, it’s Jim” say “Would you like to come to my party?” say “It’s on 16 April.” say “It’s 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

Function Definition

inviteToParty ( name , phone ){ pick up the phone dial phone say “Hello name, it’s Jim” say “Would you like to come to my party?” say “It’s on 16 April.” say “It’s 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)

Function Definition

inviteToParty ( name , phone ){ pick up the phone dial phone say “Hello name, it’s Jim” say “Would you like to come to my party?” say “It’s on 16 April.” say “It’s 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}

body

Function Definition

inviteToParty ( Sam, 9876-5432 )inviteToParty ( Jim, 9905-5788 )inviteToParty ( Ann, 9544-2382 )inviteToParty ( Dru, 9455-2323 )

Function Call

parameters listed in order

inviteToParty ( name , phone ){ pick up the phone dial phone say “Hello name, it’s Jim” say “Would you like to come to my party?” say “It’s on 16 April.” say “It’s 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 Hierarchy

ringUp

askToParty

sayGoodbye

inviteToParty ( name , phone ){ ringUp ( name , phone ) askToParty ( ) sayGoodbye ( name )}

Function Hierarchy

ringUp askToParty sayGoodbye

inviteToParty ( name , phone ){ ringUp ( name , phone ) askToParty ( ) sayGoodbye ( name )}

Function Hierarchy

ringUp askToParty sayGoodbye

ringUp ( name , phone ){ pick up the phone dial phone say “Hello name, it’s Jim”}

Function Hierarchy

askToParty ( ){ say “Would you like to come to my party?” say “It’s on 16 April.” say “It’s at 1 Wellington Road.”}

inviteToParty ( name , phone ){ ringUp ( name , phone ) askToParty ( ) sayGoodbye ( name )}

ringUp askToParty sayGoodbye

Function Hierarchy

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}

inviteToParty ( name , phone ){ ringUp ( name , phone ) askToParty ( ) sayGoodbye ( name )}

ringUp askToParty sayGoodbye

Function Hierarchy

askToParty ( date , venue ){ say “Would you like to come to my party?” say “It’s on date.” say “It’s at venue.”}

inviteToParty ( name , phone, date , venue ){ ringUp ( name , phone ) askToParty (date , venue ) sayGoodbye ( name )}

ringUp askToParty sayGoodbye

Version 2:

More parameters.

Function Hierarchy

ringUp (ringUp (namename, , phonephone)) askToParty ( )askToParty ( ) sayGoodbye (sayGoodbye (namename))

inviteToParty (inviteToParty (namename, , phonephone))

ringUp ( name , phone ){ pick up the phone dial phone say “Hello name, it’s Jim”}

Given the name, searchfor phone number here.

Function Hierarchy

ringUp (ringUp (namename)) askToParty ( )askToParty ( ) sayGoodbye (sayGoodbye (namename))

ringUp ( name ){ set number to result of searchAddrBook ( name ) pick up the phone dial number say “Hello name, it’s Jim”}

inviteToParty (inviteToParty (namename))

Fewer parameters.

Function Hierarchy

ringUp (ringUp (namename)) askToParty ( )askToParty ( ) sayGoodbye (sayGoodbye (namename))

inviteToParty (inviteToParty (namename))

searchAddrBook ( searchAddrBook ( namename ))

Function Hierarchy

ringUp (ringUp (namename)) askToParty ( )askToParty ( ) sayGoodbye (sayGoodbye (namename))

inviteToParty (inviteToParty (namename))

searchAddrBook ( searchAddrBook ( namename )) greetings ( greetings ( namename ))

Top-Down DesignRecall:

Invite to a party

Ring up

Ask to party

Say goodbye

•Find phone number•Dial number•Introduce self

•Invite•Say when•Say where

•Say goodbye•Hang up phone

Bottom-Up Design•Find phone number•Dial number•Introduce self

•Invite•Say when•Say where

•Say goodbye•Hang up phone

Invite to a party

Bottom-Up Design

Invite to a party

•Find phone number•Dial number•Introduce self

•Invite•Say when•Say where

•Say goodbye•Hang up phone

Ring up

Ask to party

Say goodbye

Bottom-Up DesignTo 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, ....

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.

inviteToParty (inviteToParty (namename))

Example

askToParty ( )askToParty ( ) sayGoodbye (sayGoodbye (namename))ringUp (ringUp (namename))

searchAddrBook ( searchAddrBook ( namename )) greetings ( greetings ( namename ))

Summary

• Functions– Function definition: name, body, parameters.– Function call.

• Bottom-Up Design

Next Lecture

• Writing functions in C.