Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE1301 Computer Programming: Lecture 16 Algorithm Design: Components.

Similar presentations


Presentation on theme: "CSE1301 Computer Programming: Lecture 16 Algorithm Design: Components."— Presentation transcript:

1 CSE1301 Computer Programming: Lecture 16 Algorithm Design: Components

2 Topics Functions –Function Call –Parameters –Algorithm Design Readings: –D&D: Section 5.1 and 5.2

3 pick up the phone dial 9876-5432 say “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

4 pick up the phone dial 9876-5432 say “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 phone dial 9905-5788 say “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 phone dial 9544-2382 say “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 phone dial 9455-2323 say “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

5 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

6 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, 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 go to next name in the list } Solution 1.

7 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 Call this block of sequence by a name. inviteToParty Solution 2.

8 pick up the phone dial 9876-5432 say “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 phone dial 9905-5788 say “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 phone dial 9544-2382 say “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 phone dial 9455-2323 say “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

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

11 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

12 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

13 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

14 inviteToParty ( Sam, 9876-5432 ) inviteToParty ( Jim, 9905-5788 ) inviteToParty ( Ann, 9544-2382 ) inviteToParty ( Dru, 9455-2323 ) Function Call parameters listed in order

15 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

16 inviteToParty ( name, phone ) { ringUp ( name, phone ) askToParty ( ) sayGoodbye ( name ) } Function Hierarchy ringUpaskToPartysayGoodbye

17 inviteToParty ( name, phone ) { ringUp ( name, phone ) askToParty ( ) sayGoodbye ( name ) } Function Hierarchy ringUpaskToPartysayGoodbye ringUp ( name, phone ) { pick up the phone dial phone say “Hello name, it’s Jim” }

18 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 ) } ringUpaskToPartysayGoodbye

19 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 ) } ringUpaskToPartysayGoodbye

20 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 ) } ringUpaskToPartysayGoodbye Version 2: More parameters.

21 Function Hierarchy ringUp (name, phone) askToParty ( ) sayGoodbye (name) inviteToParty (name, phone) ringUp ( name, phone ) { pick up the phone dial phone say “Hello name, it’s Jim” } Given the name, search for phone number here.

22 Function Hierarchy ringUp (name) askToParty ( ) sayGoodbye (name) ringUp ( name ) { set number to result of searchAddrBook ( name ) pick up the phone dial number say “Hello name, it’s Jim” } inviteToParty (name) Fewer parameters.

23 Function Hierarchy ringUp (name) askToParty ( ) sayGoodbye (name) inviteToParty (name) searchAddrBook ( name )

24 Function Hierarchy ringUp (name) askToParty ( ) sayGoodbye (name) inviteToParty (name) searchAddrBook ( name ) greetings ( name )

25 Top-Down Design Recall: 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

26 Bottom-Up Design Find phone number Dial number Introduce self Invite Say when Say where Say goodbye Hang up phone Invite to a party

27 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

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 inviteToParty (name) Example askToParty ( ) sayGoodbye (name) ringUp (name) searchAddrBook ( name ) greetings ( name )

31 Summary Functions –Function definition: name, body, parameters. –Function call. Bottom-Up Design Next Lecture Writing functions in C.


Download ppt "CSE1301 Computer Programming: Lecture 16 Algorithm Design: Components."

Similar presentations


Ads by Google