File

advertisement

STEP 3- DEVELOP AN

ALGORITHM

At this stage we break down the problem into simple manageable steps so that they can be handled easily.

WHAT IS AN ALGORITHM?

An algorithm is a sequence of precise instructions for solving a problem in a finite number of steps.

Properties of well-written algorithms:

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

Algorithm design generally has three structural components:

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.

Conditional

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

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

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

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.

PSEUDOCODE

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

Problem

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

Algorithm Student_data {Header}

This algorithm displays a student’s name and age on the screen. {declaration}

START

PRINT “Enter your name:”

READ Name

PRINT “Enter your age:”

READ Age

PRINT “Hello”, Name

PRINT “You are”, Age, “years old”

STOP {Terminator} body

FLOWCHARTS

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

Flowchart symbols

Problem

• Create a flowchart to solve the above problem

FLOWCHARTS VS. PSEUDOCODE

• 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

Download