Presentation is loading. Please wait.

Presentation is loading. Please wait.

STEP 3- DEVELOP AN ALGORITHM At this stage we break down the problem into simple manageable steps so that they can be handled easily.

Similar presentations


Presentation on theme: "STEP 3- DEVELOP AN ALGORITHM At this stage we break down the problem into simple manageable steps so that they can be handled easily."— Presentation transcript:

1 STEP 3- DEVELOP AN ALGORITHM At this stage we break down the problem into simple manageable steps so that they can be handled easily.

2 WHAT IS AN ALGORITHM? An algorithm is a sequence of precise instructions for solving a problem in a finite number of steps.

3 Algorithms must: be precise (clearly defined), be unambiguous, be logically sequenced (flow of control from one process to another), give the correct solution in all cases, and eventually end ( a general solution to the problem in a finite number of steps). Algorithms must: be precise (clearly defined), be unambiguous, be logically sequenced (flow of control from one process to another), give the correct solution in all cases, and eventually end ( a general solution to the problem in a finite number of steps). Properties of well-written algorithms:

4 Sequence - the ability of the program to execute instructions in a step-by-step manner. Selection - the ability of the computer to make decisions and alter the course of the algorithm. Example: IF statement Iteration (repetition or looping) - the ability of the computer to repeat a block of instructions until a condition is met. Example: WHILE, FOR and DO-WHILE loops. Sequence - the ability of the program to execute instructions in a step-by-step manner. Selection - the ability of the computer to make decisions and alter the course of the algorithm. Example: IF statement Iteration (repetition or looping) - the ability of the computer to repeat a block of instructions until a condition is met. Example: WHILE, FOR and DO-WHILE loops. Algorithm design generally has three structural components: Conditional

5 Sequence The sequence control structure is used when you have instructions to be carried out in a particular order. Example: » Start » Get 2 numbers » Add the number and store it in Answer » Display Answer » Stop

6 Selection The Selection control structure is used in problems with instructions to be carried out if a certain condition is met. The choice of option will be dependent on if the condition is true or false. Example » Start » Accept score » If score is more than 59 then display ‘the student has passed’ else display ‘the student has failed’ » stop

7 Iteration/ repetition/ loop Iteration or Repetition control structures are used to repeat a certain process a number of times. Suppose you want to accept 100 numbers from the user and find their sum, it will be wise to repeat the process of getting the numbers 100 times rather than writing 100 instructions to accept the numbers. Commonly used iteration statements are: » For………….end for » While………end while » Repeat…….until

8 Algorithms are normally written in (i)pseudocode or pseudo-English or (ii)represented by a flowchart. A pseudocode is an algorithm that resembles the programming language. It however cannot be executed by the computer. A flowchart is a pictorial representation of an algorithm. These are diagrams that help us to visualize the sequence of algorithms.

9 Algorithmic structure Header: Algorithm’s name or title Declaration: Brief description of algorithm and variable used. i.e. A statement of purpose as well as the initialization of variables Body: Sequence of steps Terminator: An end statement PSEUDOCODE

10 Write an algorithm that prompts a student to enter his/her name and age, accepts the name and age and then display a welcoming message on the screen such as “Hello Michael! You are 16 years old!” Identify the header, declaration, body and terminator. Problem

11 Algorithm Student_data {Header} This algorithm displays a student’s name and age on the screen. {declaration} START PRINT “Enter your name:” READ Namebody PRINT “Enter your age:” READ Age PRINT “Hello”, Name PRINT “You are”, Age, “years old” STOP{Terminator} Algorithm Student_data {Header} This algorithm displays a student’s name and age on the screen. {declaration} START PRINT “Enter your name:” READ Namebody PRINT “Enter your age:” READ Age PRINT “Hello”, Name PRINT “You are”, Age, “years old” STOP{Terminator}

12 Flowcharts are diagrams that arrange the components of a problem in a logical sequence. Flowcharts must use special geometrical objects that designate the basic steps of a program. The objects are linked using arrowed lines that point to the next step in the sequence. Flowcharts are diagrams that arrange the components of a problem in a logical sequence. Flowcharts must use special geometrical objects that designate the basic steps of a program. The objects are linked using arrowed lines that point to the next step in the sequence. FLOWCHARTS

13 Flowchart symbols

14 Create a flowchart to solve the above problem Problem

15

16 Pseudocode is more concise, closely resembles programming language and there are no symbols to learn Flowchart gives good view of the structure and flow of the logic Beginners tend to follow the logic easier when using flowcharts rather than pseudocode Longer, more complex solutions are better represented using pseudocode. Pseudocode uses less paper to document FLOWCHARTS VS. PSEUDOCODE


Download ppt "STEP 3- DEVELOP AN ALGORITHM At this stage we break down the problem into simple manageable steps so that they can be handled easily."

Similar presentations


Ads by Google