Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12
ALGORITHM
A step by step problem-solving process in which a solution is arrived at in
a finite amount of time. Or A sequence of steps for solving a specific problem given its input data and the expected output data. Algorithms - developing a step-by-step solution to the problem, or the rules to follow to solve the problem •Decomposition - breaking down a complex problem or system into smaller, more manageable parts •Pattern recognition – looking for similarities among and within problems •Abstraction – focusing on the important information only, ignoring irrelevant detail DESIGNING ALGORITHM • When designing an algorithm, consider if there is more than one way of solving the problem. • When designing an algorithm there are two main areas to look at: • the big picture - What is the final goal? • the individual stages – What hurdles need to be overcome on the way to the goal? UNDERSTANDING THE PROBLEM
• Before an algorithm can be designed, it is important to check that
the problem is completely understood. There are number of basic things to know in order to really understand the problem: • What are the inputs into the problem? • What will be the outputs of the problem? • In what order do instructions need to be carried out? • What decisions need to be made in the problem? • Are any areas of the problem repeated? +Once these basic things are understood, it is time to design the algorithm. Example 1 Pseudocode for cooking rice might look as follows: 1) Start 2) Wash the rice 3) Put into boiling water in saucepan 4) Leave to boil for some minutes until thoroughly cooked 5) End Example 2 Pseudocode for adding two numbers might look as: 1) Start 2) Prompt for first number 3) Enter first number 4) Prompt for second number 5) Enter second number 6) Calculate the sum of the two numbers, i.e. num1 + num2 7) Display the results 8) End DESIGN AN ALGORITHM TO FIND THE PERIMETER AND AREA OF A RECTANGLE 1. Get the length of the rectangle 2. Get the width of the rectangle 3. Find the perimeter using the equation perimeter = 2 * (length + area ) 4. For the area, we use the formulae Area = length * width NB: there would be more examples on algorithm PSEUDOCODE Most programs are developed using programming languages. These languages have specific syntax that must be used so that the program will run properly. Syntax: it refers to rules that govern a particular programming language Pseudocode is not a programming language; it is a simple way of describing a set of instructions that does not have to use specific syntax There are two main ways that algorithms can be represented Representing an algorithm: Pseudocode – pseudocode and flowcharts.
Most programs are developed using programming languages.
These languages have specific syntax that must be used so that the program will run properly.
Pseudocode is not a programming language,
it is a simple way of describing a set of instructions that does not have to use specific syntax. Writing in pseudocode is like writing in a programming language. Each step of the algorithm is written on a line of its own in sequence. Usually, instructions are written in uppercase, variables in lowercase and messages in sentence case. In pseudocode, INPUT asks a question. PSEUDOCODE CONT’D
A simple program could be created
to ask someone their name and age, and to make a comment based on these. This program represented in pseudocode would look like this: EXAMPLE OF PSEUDOCODE +OUTPUT 'What is your name?’ +INPUT user inputs their name STORE the user's input in the name variable OUTPUT 'Hello' + name +OUTPUT 'How old are you?’ +INPUT user inputs their age STORE the user's input in the age variable +IF age >= 70 +THEN +OUTPUT 'You are aged to perfection!’ +ELSE OUTPUT 'You are a spring chicken!' +In programming, > means ‘greater than’, < means ‘less than’, ≥ means ‘greater than or equal to’ and ≤ means ‘less than or equal to’. EXAMPLE
REPEAT OUTPUT 'What is the best subject you take?’
INPUT user inputs the best subject they take STORE the user's input in the answer variable IF answer = 'Computer Science' THEN OUTPUT 'Of course it is!’ ELSE OUTPUT 'Try again!’ UNTIL answer = 'Computer Science'