1 CSE1301 Computer Programming Lecture 4: Components of an Algorithm.

Slides:



Advertisements
Similar presentations
CS101: Introduction to Computer programming
Advertisements

Chapter 1 - An Introduction to Computers and Problem Solving
Chapter 2 - Problem Solving
 Control structures  Algorithm & flowchart  If statements  While statements.
1 CSE1301 Computer Programming: Lecture 23 Algorithm Design (Part 1)
Computer Programming Lecture 23. Summary of Previous Lecture In the previous lecture, we have learnt  Intellectual Property Types of Intellectual Property.
ITEC113 Algorithms and Programming Techniques
Chapter 4 Repetitive Execution. 2 Types of Repetition There are two basic types of repetition: 1) Repetition controlled by a counter; The body of the.
1 CSE1301 Computer Programming: Lecture 15 Flowcharts and Debugging.
1 CSE1301 Computer Programming Lecture 10: Iteration (Part 1)
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Pseudocode and Algorithms
1 CSE1301 Computer Programming: Lecture 15 Flowcharts, Testing and Debugging.
Chapter 1 Program Design
The switch Statement, DecimalFormat, and Introduction to Looping
CSC103: Introduction to Computer and Programming
1 Introduction to Computing Lecture 02: Introduction to Algorithms Dr. Bekir KARLIK Yasar University Department of Computer Engineering
DCT 1123 PROBLEM SOLVING & ALGORITHMS INTRODUCTION TO PROGRAMMING.
Algorithms 1 (Lecture 2 of CSE1301)
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Invitation to Computer Science, Java Version, Second Edition.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
Chapter 12: How Long Can This Go On?
Jaeki Song ISQS6337 JAVA Lecture 04 Control Structure - Selection, and Repetition -
1 CSE Computer Programming “Using C-Language” Lectures 1.
1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI
1 CSE1301 Computer Programming Lecture 3: Components of an Algorithm.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Al-Khwarizmi 2 (Lecture 3 of CSE1301) Dr Damian Conway Room 132, Building 26 (Consult hours on the door)
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
1 CSE1301 Computer Programming Lecture 3: Components of an Algorithm.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Comments, Conditional Statements Continued, and Loops Engineering 1D04, Teaching Session 4.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
GCSE Computing#BristolMet Session Objectives #23 MUST understand what is meant by the programming term iteration SHOULD describe methods of looping used.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
1 CSE1301 Computer Programming: Lecture 16 Flow Diagrams and Debugging.
CONTROL FLOW. Introduction  In a program instructions are always executed in the sequential manner.  If programmer wishes to specify the order in which.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
REPETITION CONTROL STRUCTURE
Algorithm & Programming
The switch Statement, and Introduction to Looping
7 - Programming 7P, Q, R - Testing.
DCP2073 Asas Pengaturcaraan C Lecture 3: Components of an Algorithm
Chapter 5: Repetition Structures
( Iteration / Repetition / Looping )
Control Structure Senior Lecturer
Introduction to pseudocode
Loops A loop is a repetition control structure.
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Algorithm Discovery and Design
Iteration: Beyond the Basic PERFORM
Coding Concepts (Basics)
Algorithm Discovery and Design
Chapter 2.1 Repetition.
Introduction to Problem Solving and Control Statements
Faculty of Computer Science & Information System
Computer Science Core Concepts
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
CHAPTER 4 Iterative Structure.
The structure of programming
Thinking procedurally
REPETITION Why Repetition?
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

1 CSE1301 Computer Programming Lecture 4: Components of an Algorithm

2 Recall What is the problem solving process? What is an algorithm? What are some examples? What are values and variables? What are instructions or primitives?

3 Topics More components of an algorithm The software development process Top-down design

4 Components of an Algorithm Values and Variables Instruction (a.k.a. primitives) Sequence (of instructions) Procedure (involving instructions) Selection (between instructions) Repetition (of instructions) Documentation (beside instructions)

5 Sequence A series of instructions...to be carried out one after the other......without hesitation or question Example: –How to cook a Gourmet Meal TM

6 Sequence -- Example 1. Open freezer door 2. Take out Gourmet Meal™ 3. Close freezer door 4. Open microwave door 5. Put Gourmet Meal™ on carousel 6. Shut microwave door 7. Set microwave on high for 5 minutes 8. Start microwave 9. Wait 5 minutes 10. Open microwave door 11. Remove Gourmet Meal™ 12. Close microwave door

7 Components of an Algorithm Values and Variables Instruction (a.k.a. primitives) Sequence (of instructions) Procedure (involving instructions) Selection (between instructions) Repetition (of instructions) Documentation (beside instructions)

8 Procedure A named sequence of instructions So that you can –Refer to it collectively (by name) –...instead of individually (by each instruction in the sequence) Example: –Drive_To_Uni

9 Procedure -- Example procedure Drive_To_Uni { 1. find car keys 2. disable car alarm 3. open car door 4. get in car 5. shut car door 6. put keys in ignition 7. start car 8. back car out of driveway 9. drive to end of street 10. turn right 11. drive to end of street 12. turn left...etc...etc...etc...etc...etc...etc find parking space 53. pull into parking space 54. turn off engine 55. remove keys from ignition 56. open car door 57. get out 58. shut car door 59. lock car door 60. enable alarm }

10 Procedure – Example (cont) procedure Do_Wednesday { Wake_up Have_Shower Eat_Breakfast Drive_To_Uni Sit_1301_Lecture...etc...etc...etc... Drive_From_Uni...etc...etc...etc... } procedure Do_Week { Do_Monday Do_Tuesday Do_Wednesday Do_Thursday...etc...etc...etc... }

11 Procedure – Example (cont) procedure Do_Wednesday { Wake_up Have_Shower Eat_Breakfast Drive_To_Uni Sit_1301_Lecture...etc...etc...etc... Drive_From_Uni...etc...etc...etc... } In this subject, we also use the following words to refer to a “Procedure” : Sub-routine Module Function

12 Procedure – Example (cont) procedure Do_Wednesday { Wake_up Have_Shower Eat_Breakfast Drive_To_Uni Sit_1301_Lecture...etc...etc...etc... Drive_From_Uni...etc...etc...etc... } We use brackets to mark the beginning and end of a sequence.

13 Procedure – Example (cont) procedure Do_Wednesday { Wake_up Have_Shower Eat_Breakfast Drive_To_Uni Sit_1301_Lecture...etc...etc...etc... Drive_From_Uni...etc...etc...etc... } An instruction invoking a procedure is known as a “procedure call”

14 Procedure A procedure may have a set of parameters procedure customerService ( myName,timeOfDay ) { say “Good timeOfDay” say “My name is myName” say “How can I help you?” } customerService ( “Ann”, “Morning” ) customerService (“Ann”, “Afternoon” ) customerService ( “Jeff”, “Afternoon” )

15 Components of an Algorithm Values and Variables Instruction (a.k.a. primitives) Sequence (of instructions) Procedure (involving instructions) Selection (between instructions) Repetition (of instructions) Documentation (beside instructions)

16 Selection An instruction that decides which of two possible sequences is executed The decision is based on a single true/false condition Examples: –Car repair –Reciprocals

17 Selection Example -- Car Repair if (motor turns) then { CheckFuel CheckSparkPlugs CheckCarburettor } else { CheckDistributor CheckIgnitionCoil }

18 Selection Example – Car Repair (cont) if (motor turns) then { CheckFuel CheckSparkPlugs CheckCarburettor } else { CheckDistributor CheckIgnitionCoil } Should be a true or false condition.

19 Selection Example -- Car Repair (cont) if (motor turns) then { CheckFuel CheckSparkPlugs CheckCarburettor } else { CheckDistributor CheckIgnitionCoil } Sequence if the condition is true.

20 Selection Example -- Car Repair (cont) if (motor turns) then { CheckFuel CheckSparkPlugs CheckCarburettor } else { CheckDistributor CheckIgnitionCoil } Sequence if the condition is false.

21 Selection Example -- Reciprocals Q. Give an algorithm for computing the reciprocal of a number. Examples: Reciprocal of 2: 1/2 Reciprocal of -3/4: 1/(-3/4) = -4/3 Reciprocal of 0: “undefined”

22 Selection Example – Reciprocals (cont) Q. Give an algorithm for computing the reciprocal of a number. Algorithm: input Num if (Num is not equal 0) then { output 1/Num } else { output "infinity" }

23 Selection Example-- Reciprocals input Num if (Num is not equal 0) then { output 1/Num } else { output "infinity" } Algorithm: Num is a variable whose value depends on the actual number the user provides.

24 Selection Example – Reciprocals (cont) input Num if (Num is not equal 0) then { output 1/Num } else { output "infinity" } Algorithm: Condition depends on the value of Num

25 Selection Example – Reciprocals (cont) input Num if (Num is not equal 0) then { output 1/Num } else { output "infinity" } Algorithm: For a given value of Num, only one of these two sequences can be executed

26 Selection Example – Reciprocals (cont) input Num if (Num is not equal 0) then { output 1/Num } else { output "infinity" } Algorithm: Executed if Num is not equal to 0

27 Selection Example – Reciprocals (cont) input Num if (Num is not equal 0) then { output 1/Num } else { output "infinity" } Algorithm: Executed if Num is equal to 0

28 Selection -- Exercise input Num if (Num is not equal 0) then { output 1/Num } else { output "infinity" } Will the following algorithms produce the same output? Algorithm 1: input Num if (Num is not equal 0) then { output 1/Num } output "infinity" Algorithm 2:

29 Selection – Several Conditions What if several conditions need to be satisfied? if ( today is Wednesday and the time is 10.00am ) then { Go to CSE1301 Lecture } else { Go to Library } Solution 1

30 Selection – Several Conditions (cont) Solution 2 Often called a “nested selection” if ( today is Wednesday ) then { if ( the time is 10.00am ) then { Go to CSE1301 Lecture } else...etc...etc...etc...

31 Selection – At Least One of Several Conditions What if at least one of several conditions needs to be satisfied? if ( I feel hungry or the time is 1.00pm or my mate has his eye on my lunch ) then { Eat my lunch now }

32 Selection – Several Courses of Action What if there are several courses of action? if ( button pressed is 1 ) then { CheckAccountBalance } else { if ( button pressed is 2 ) then { TransferFunds } else { if ( button pressed is 3 ) then { PayBills } else { if ( button pressed is 4 ) then { ExitPhoneBanking } else { say “Invalid option” } Form 1

33 Selection – Several Courses of Action (cont) if ( button pressed is 1 ) then { CheckAccountBalance } else { if ( button pressed is 2 ) then { TransferFunds } else { if ( button pressed is 3 ) then { PayBills } else { if ( button pressed is 4 ) then { ExitPhoneBanking } else { say “Invalid option” } Form 1

34 Selection – Several Courses of Action (cont) if ( button pressed is 1 ) then { CheckAccountBalance } else { if ( button pressed is 2 ) then { TransferFunds } else { if ( button pressed is 3 ) then { PayBills } else { if ( button pressed is 4 ) then { ExitPhoneBanking } else { say “Invalid option” } Form 1 One condition per course of action

35 Selection – Several Courses of Action (cont) if ( button pressed is 1 ) then { CheckAccountBalance } else { if ( button pressed is 2 ) then { TransferFunds } else { if ( button pressed is 3 ) then { PayBills } else { if ( button pressed is 4 ) then { ExitPhoneBanking } else { say “Invalid option” } Form 1

36 Selection – Several Courses of Action (cont) if ( button pressed is 1 ) then { CheckAccountBalance } else { if ( button pressed is 2 ) then { TransferFunds } else { if ( button pressed is 3 ) then { PayBills } else { if ( button pressed is 4 ) then { ExitPhoneBanking } else { say “Invalid option” } Form 1

37 Selection – Several Courses of Action (cont) if ( button pressed is 1 ) then { CheckAccountBalance } else { if ( button pressed is 2 ) then { TransferFunds } else { if ( button pressed is 3 ) then { PayBills } else { if ( button pressed is 4 ) then { ExitPhoneBanking } else { say “Invalid option” } Form 1

38 Selection – Several Courses of Action (cont) if ( button pressed is 1 ) then { CheckAccountBalance } else { if ( button pressed is 2 ) then { TransferFunds } else { if ( button pressed is 3 ) then { PayBills } else { if ( button pressed is 4 ) then { ExitPhoneBanking } else { say “Invalid option” } Form 1 “Default” sequence (may be optional)

39 Selection – Several Courses of Action (cont) if ( button pressed is 1 ) then { CheckAccountBalance } else if ( button pressed is 2 ) then { TransferFunds } else if ( button pressed is 3 ) then { PayBills } else if ( button pressed is 4 ) then { ExitPhoneBanking } else { say “Invalid option” } Form 2 “Cascaded” selection.

40 Selection – Several Courses of Action (cont) if ( button pressed is 1 ) then { CheckAccountBalance } else if ( button pressed is 2 ) then { TransferFunds } else if ( button pressed is 3 ) then { PayBills } else if ( button pressed is 4 ) then { ExitPhoneBanking } else { say “Invalid option” } Form 2 Optional default sequence.

41 Components of an Algorithm Values and Variables Instruction (a.k.a. primitives) Sequence (of instructions) Procedure (involving instructions) Selection (between instructions) Repetition (of instructions) Documentation (beside instructions)

42 Repetition Repeat an instruction... –...while (or maybe until) some true or false condition occurs –Test the condition each time before repeating the instruction Also known as iteration or loop Example: –Algorithm for getting a date

43 Repetition -- Example procedure AskOnDate ( name, time, location ) { Phone(name) Say("Hey", name, "it's your lucky day!") Say("Wanna come to", location, "at", time, "?") ListenToReply ( ) start begging count at zero while (reply is "No" and begging count < 100) { Say("Oh please!") add 1 to begging count ListenToReply ( ) } }

44 Repetition – Example (cont) procedure AskOnDate ( name, time, location ) { Phone(name) Say("Hey", name, "it's your lucky day!") Say("Wanna come to", location, "at", time, "?") ListenToReply ( ) start begging count at zero while ( reply is "No" and begging count < 100 ) { Say("Oh please!") add 1 to begging count ListenToReply ( ) } Condition is tested before sequence

45 Repetition – Example (cont) procedure AskOnDate ( name, time, location ) { Phone(name) Say("Hey", name, "it's your lucky day!") Say("Wanna come to", location, "at", time, "?") ListenToReply ( ) start begging count at zero while (reply is "No" and begging count < 100) { Say("Oh please!") add 1 to begging count ListenToReply ( ) } } Sequence may not get executed at all

46 Repetition – Example (cont) procedure AskOnDate ( name, time, location ) { Phone(name) Say("Hey", name, "it's your lucky day!") Say("Wanna come to", location, "at", time, "?") ListenToReply ( ) start begging count at zero while (reply is "No" and begging count < 100) { Say("Oh please!") add 1 to begging count ListenToReply ( ) } Ensure initial values of variables used in the conditions are set correctly

47 Repetition – Example (cont) procedure AskOnDate ( name, time, location ) { Phone(name) Say("Hey", name, "it's your lucky day!") Say("Wanna come to", location, "at", time, "?") ListenToReply ( ) start begging count at zero while (reply is "No" and begging count < 100) { Say("Oh please!") add 1 to begging count ListenToReply ( ) } Ensure the variables used in the conditions are updated in each iteration

48 Repetition – Example (cont) procedure AskOnDate ( name, time, location ) { Phone(name) Say("Hey", name, "it's your lucky day!") Say("Wanna come to", location, "at", time, "?") ListenToReply ( ) start begging count at zero while (reply is "No" and begging count < 100) { Say("Oh please!") } Infinite loop What if we don’t increment the begging count?

49 Repetition – Variation decide on Time and Location initialise booking to “unsuccessful” while ( not successfully booked ) { get next Name in little black book AskOnDate(Name, Time, Location) DetermineBookingSuccess } SighWithRelief

50 Repetition– Pre-tested Loop decide on Time and Location initialise booking to “unsuccessful” while ( not successfully booked ) { get next Name in little black book AskOnDate(Name, Time, Location) DetermineBookingSuccess } SighWithRelief pre-tested loop

51 Repetition – Pre-tested Loop decide on Time and Location initialise booking to “unsuccessful” until ( successfully booked ) { get next Name in little black book AskOnDate(Name, Time, Location) DetermineBookingSuccess } SighWithRelief pre-tested loop

52 Repetition – Post-tested Loop decide on Time and Location initialise booking to “unsuccessful” do { get next Name in little black book AskOnDate(Name, Time, Location) DetermineBookingSuccess } while ( not successfully booked ) SighWithRelief post-tested loop Sequence is executed at least once

53 Repetition – Post-tested Loop decide on Time and Location initialise booking to “unsuccessful” repeat { get next Name in little black book AskOnDate(Name, Time, Location) DetermineBookingSuccess } until ( successfully booked ) SighWithRelief

54 Repetition -- Variations decide on Time and Location initialise booking to “unsuccessful” loop { get next Name in little black book AskOnDate(Name, Time, Location) DetermineBookingSuccess if ( successfully booked ) then { break loop } SighWithRelief

55 Components of an Algorithm Values and Variables Instruction (a.k.a. primitives) Sequence (of instructions) Procedure (involving instructions) Selection (between instructions) Repetition (of instructions) Documentation (beside instructions)

56 Documentation Records what the algorithm does Describes how it does it Explains the purpose of each component of the algorithm Notes restrictions or expectations Example: –Getting a date (again)

57 Documentation -- Example Think of something romantic to do decide on time and location Work through address book to look for a person initialise booking to “unsuccessful” until (successfully booked) { get next Name in little black book AskOnDate(Name, Time, Location) DetermineBookingSuccess } Assumes that I will find someone in the book before it runs out SighWithRelief

58 Components of an Algorithm Values and Variables Instruction (a.k.a. primitives) Sequence (of instructions) Procedure (involving instructions) Selection (between instructions) Repetition (of instructions) Documentation (beside instructions)

59 The Software Development Process Define the problem clearly Analyse the problem thoroughly Design an algorithm carefully Code the algorithm efficiently Test the code thoroughly Document the system lucidly

60 Top-down Algorithm Design Write down what you have to do Break that into 3-7 smaller steps Break each step into 3-7 smaller steps Keeping subdividing until each individual step is easy enough to do – i.e., until it is a single instruction Example: –Learning

61 Top-down Design -- Example Learn Prepare Study Reinforce Read Make notes Prepare questions Attend lecture Listen and think Complete prac Attend tute Record answers to questions Revise notes Read lecture notes Read textbook Read exercise Design algorithm Code solution Test and document Record insights

62 Reading Deitel & Deitel, C: How to program –Chapter 3, Sections 3.8 to 3.13