Program design
GOOD DAY, students. This is lesson 22 in our series of lessons. In this week’s lesson, we will continue to look at problem-solving and program design.
Before we continue looking at program design, here are the solutions to the defining diagram questions I gave you in the previous lesson.
QUESTION 1 QUESTION 2 ALGORITHMS
An algorithm is a sequence of precise instructions or steps used for solving a problem in a finite amount of time.
The properties of an algorithm are:
1. It must be precise (exact).
2. It must be unambiguous (in other words, it must be clear).
3. It must give the correct solution in all cases.
4. An algorithm must terminate or eventually end.
THE ALGORITHMIC STRUCTURE
Every algorithm should have the following sections, in the stated order:
Header: Algorithm’s name or title
Declaration: A brief description of the algorithm and variables used. That is, a statement of the purpose as well as a declaration of the variables. Body: Sequence of steps
Terminator: An end statement
An algorithm can be represented in the following ways:
With the use of narrative Pseudocode format
With flow charts
Narrative: This gives a description of a sequence of instructions to solve a particular programming problem.
Flow chart: This is a graphical representation of an algorithm with the use of certain specified symbols.
Psuedocode: This is a design of a computer program or an imitation of a computer program using mathematical notations and English-like statements to describe the logics use to solve a problem or carry out a procedure. It consists of a set of executable steps/instructions of a solution to a programming problem.
THE ALGORITHMIC LANGUAGE NARRATIVE FORMAT
Example: Write an algorithm to accept two numbers, find the product of the two numbers and output the product of the two numbers.
Step 1: Start
Step 2: Get the two numbers
Step 3: Multiply the two numbers and store the result
Step 4: Display the result
Step 5: Stop
PSEUDOCODE FORMAT
When writing algorithms, the language gradually progresses from English towards a notation that resembles that of a programming language. An intermediate notation, called pseudocode, is commonly used to express algorithms.
PARTS OF A PSEUDOCODE ALGORITHM
A pseudocode algorithm contains one or more of the following statements:
Input statements
Assignment statements
Output statements
Control structures (sequence, selection, iteration/ repetition)
DOS AND DON’TS WHEN WRITING PSEUDOCODE
You should do the following:
1. Use the assignment symbol (–––) in assignment statements instead of the equal sign.
2. Use the equal sign for conditional statements (‘if’ statements).
3. Use meaningful variable names. 4. Use indentation to show the logic and scope of control structures.
You should not do the following:
1. Use language-specific constructs such as case, switch statements or ‘for’ loops. Constructs such as ‘while’, ‘repeat’ and ‘if-then-else’ are sufficiently general and can, therefore, be used in the pseudocode. Keywords such as readln, writeln, printf, scanf should not be used. These are used for high-level languages.
2. Attempt to write Pascal code before writing the algorithm. That is, do not execute the program first and then try to write the algorithm afterwards based on the program code. This is a very bad practice and is, essentially, a waste of time.
We have come to the end of this lesson. See you next week, when we will continue to look at psuedocode algorithms. Remember, if you fail to prepare, you prepare to fail.