Algorithms Flowchart and Pseudocode Original Source http www

  • Slides: 23
Download presentation
Algorithms, Flowchart and Pseudocode Original Source : http: //www. ftsm. ukm. my/zma/TK 1914/05 -Algorithms

Algorithms, Flowchart and Pseudocode Original Source : http: //www. ftsm. ukm. my/zma/TK 1914/05 -Algorithms and Problem Solving. ppt

WHAT IS AN ALGORITHM? � An algorithm is a set of ordered steps for

WHAT IS AN ALGORITHM? � An algorithm is a set of ordered steps for solving a problem. � Examples: �An algorithm for preparing breakfast. �An algorithm for converting Gregorian dates to Islamic dates. �An algorithm for calculating moon phase. �An algorithm for drawing a curve. 2

ALGORITHM IN REAL LIFE � Consider the following … Problem: Baking a Cake How

ALGORITHM IN REAL LIFE � Consider the following … Problem: Baking a Cake How to solve: 1. Start 2. Preheat the oven at 180 o. C 3. Prepare a baking pan 4. Beat butter with sugar 5. Mix them with flour, eggs and essence vanilla 6. Pour the dough into the baking pan 7. Put the pan into the oven 8. End 3

‘DIVIDE AND CONQUER’ STRATEGY IN ALGORITHM Problem: Prepare a Breakfast 1. Start 2. Prepare

‘DIVIDE AND CONQUER’ STRATEGY IN ALGORITHM Problem: Prepare a Breakfast 1. Start 2. Prepare a Breakfast 3. End 4

‘DIVIDE AND CONQUER’ STRATEGY IN ALGORITHM 1. Start 2. Prepare a Breakfast 2. 1

‘DIVIDE AND CONQUER’ STRATEGY IN ALGORITHM 1. Start 2. Prepare a Breakfast 2. 1 Prepare a tuna sandwich 2. 2 Prepare some chips 2. 3 Make a cup of coffee 3. End 5

‘DIVIDE AND CONQUER’ STRATEGY IN ALGORITHM 1. Start 2. Prepare a Breakfast 2. 1

‘DIVIDE AND CONQUER’ STRATEGY IN ALGORITHM 1. Start 2. Prepare a Breakfast 2. 1 Prepare a tuna sandwich 2. 1. 1 Take 2 slices of bread 2. 1. 2 Prepare tuna paste 2. 2 Prepare some chips 2. 3 Make a cup of coffee 3. End 6

‘DIVIDE AND CONQUER’ STRATEGY IN ALGORITHM 1. Start 2. Prepare a Breakfast 2. 1

‘DIVIDE AND CONQUER’ STRATEGY IN ALGORITHM 1. Start 2. Prepare a Breakfast 2. 1 Prepare a tuna sandwich 2. 1. 1 Take 2 slices of bread 2. 1. 2 Prepare tuna paste 2. 2 Prepare some chips 2. 2. 1 Cut potatoes into slices 2. 2. 2 Fry the potatoes 2. 3 Make a cup of coffee 3. End 7

‘DIVIDE AND CONQUER’ STRATEGY IN ALGORITHM 1. Start 2. Prepare a Breakfast 2. 1.

‘DIVIDE AND CONQUER’ STRATEGY IN ALGORITHM 1. Start 2. Prepare a Breakfast 2. 1. Prepare a tuna sandwich 2. 1. 1 Take 2 slices of bread 2. 1. 2 Prepare tuna paste 2. 2. Prepare some chips 2. 2. 1 Cut potatoes into slices 2. 2. 2 Fry the potatoes 2. 3. Make a cup of coffee 2. 3. 1 Boil water 2. 3. 2 Add water with sugar and coffee 3. End 8

CLASS ACTIVITY 5. 1 � Write a simple algorithm for withdrawing a sum of

CLASS ACTIVITY 5. 1 � Write a simple algorithm for withdrawing a sum of money at an ATM. 9

WHY DO WE NEED TO BUILD ALGORITHMS? � If we wish to build a

WHY DO WE NEED TO BUILD ALGORITHMS? � If we wish to build a house, we need to design it first. ◦ Can you think of some possible consequences of not designing a house before building it? � Similarly, computer programs (especially large and complex ones) need to be designed before they are written. ◦ Can you think of some possible consequences of not designing a program before building it? � One of the things considered when designing a computer program is the algorithm which it will be based on. 10

ALGORITHMS IN PROGRAM DESIGN �A computer program is built to solve a certain problem.

ALGORITHMS IN PROGRAM DESIGN �A computer program is built to solve a certain problem. Examples: 1. A program to calculate the grade obtained given a mark. 2. A program to convert a Gregorian date to an Islamic date. 3. A program to produce a document. 11

� Below are steps (in fact, an algorithm) for building a program to solve

� Below are steps (in fact, an algorithm) for building a program to solve a particular problem: ◦ Analyse the problem ◦ Design a computer solution to the problem by developing an algorithm. ◦ Write a computer program based on the algorithm. ◦ Test the program. 12

HOW TO SPECIFY AN ALGORITHM? � An algorithm must be specific enough so that

HOW TO SPECIFY AN ALGORITHM? � An algorithm must be specific enough so that it can be conveniently translated into a computer program (using C++, for example). � An algorithm can be specified: ◦ Textually For example, using pseudo code (see later) ◦ Graphically For example, using flowcharts or UML activity charts 13

FLOWCHARTS �A flowchart is a graphical representation of the sequence of operations in a

FLOWCHARTS �A flowchart is a graphical representation of the sequence of operations in a program. � An algorithm can be represented graphically using a flowchart. 14

FLOWCHART NOTATIONS Symbol Semantic Start/End Process Input/Output Test Connector Flow of activities 15

FLOWCHART NOTATIONS Symbol Semantic Start/End Process Input/Output Test Connector Flow of activities 15

FLOWCHART: EXAMPLE 1 Start Algorithm starts here Input Gregorian date Input data from user

FLOWCHART: EXAMPLE 1 Start Algorithm starts here Input Gregorian date Input data from user Convert Gregorian date to Islamic date Display Islamic date End Perform the date conversion Display the result Algorithm ends here 16

PSEUDOCODE � � An outline of a program, written in a form that can

PSEUDOCODE � � An outline of a program, written in a form that can easily be converted into real programming statements. It resembles the actual program that will be implemented later. However, it cannot be compiled nor executed. Pseudocode normally codes the following actions: ◦ Initialisation of variables ◦ Assignment of values to the variables ◦ Arithmetic operations ◦ Relational operations 17

EXAMPLE OF PSEUDOCODE 1. 2. 3. 4. 5. 6. Start Read quantity Read price_per_kg

EXAMPLE OF PSEUDOCODE 1. 2. 3. 4. 5. 6. Start Read quantity Read price_per_kg price quantity * price_per_kg Print price End 18

CLASS ACTIVITY 5. 2 � Draw a flowchart which represents the algorithm built in

CLASS ACTIVITY 5. 2 � Draw a flowchart which represents the algorithm built in CA[5. 1]. 19

FLOWCHART: EXAMPLE 2 Start Input length, width area ← length X width • length,

FLOWCHART: EXAMPLE 2 Start Input length, width area ← length X width • length, width and area are referred to as variables. • A variable is like a box in which a value can be stored Output area End 20

FLOWCHART: EXAMPLE 3 � Selection Start Input height false height > 1. 6? Output

FLOWCHART: EXAMPLE 3 � Selection Start Input height false height > 1. 6? Output “You are short!” true Output “You are tall!” End 21

FLOWCHART: EXAMPLE 4 � Repetition (looping) Start Output “Thank you!” Input stop = 1?

FLOWCHART: EXAMPLE 4 � Repetition (looping) Start Output “Thank you!” Input stop = 1? false true End 22

YOU SHOULD NOW KNOW… � what an algorithm is. � when an algorithm should

YOU SHOULD NOW KNOW… � what an algorithm is. � when an algorithm should be developed when building a computer program. � the basic steps in building a computer program to solve a problem. � what flowcharts are. � how to represent algorithms graphically using flowcharts. 23