7.2 Designs of Algorithm
7.2 Designs of Algorithm
1. Modules must be linked and testing must be carried out to make sure that the links work correctly.
2. Programmers must ensure that the cross-referencing is done.
3. Interfaces between modules must be planned.
Q3. Any problem that uses a computer system for its solution needs to be decomposed into its
component parts.
1. Inputs : the data used by the system that needs to be entered while the system is active.
2. Process : the tasks that need to be performed using the input data and any other previously stored
data.
3. Outputs : information that needs to be displayed or printed for the users of the system.
4. Storage : data that needs to be stored in files on an appropriate medium for use in the future.
b) Identify three of the different items, other than software, that make up a computer system.
1. Hardware
2. Data
3. Communication and people
An algorithm is a set of instructions, telling computer what to do step by step to solve a problem.
Each steps are uniquely defined, either to take input, execute some process or output data.
The steps are normally in "sequence", "selection", "iteration" and a "case-type" statement.
The algorithm stops after execution of finite number of instructions.
1. Analyze the problem and make sure that you understood it properly.
2. Determine what would be the -
Inputs,
Processes and
Outputs.
3. Break down the problem into sub-problems if it is complex.
4. Write down all the steps needed to solve the problem sequentially from start to end.
5. Determine what variables and constants need to be declared and initialized to setup the program.
6. Determine the sequential statements and compound statements (like, selection and looping) need
to be used.
7. Construction your algorithm using either flowchart or pseudocode designing tools.
8. Making sure that it can be easily read and understood by others. Use meaningful names for
variables and constants.
9. Use several sets of test data (normal, abnormal and boundary) and trace tables to find any errors in
your algorithm.
10. Debug the errors if found, and test your algorithm until it works perfectly to produce the desired
output.
1) Structure diagram:
A diagram to show the hierarchical or ordered way of designing a system, by breaking down a
system into its sub-systems to its lowest manageable levels in a tree like structure.
It is a top-down modular design tool, constructed using squares to represent different modules in
the system, and lines that connect them. The lines represent the connection between activities and
sub- activities as they are used in organization charts.
2) Flowchart:
3) Pseudocode:
Pseudocode is a method to describe the steps of an algorithm, using English words with common
programming terms and mathematical operators that are set out to look like a program.
It is not bound by the strict syntax rules of any programming language to solve a problem.
While developing a real program to execute, pseudocode can then be coded into any programming
language of choice.
Structure diagram
Q6. A Satellite Navigation System allows the user to enter the destination details, either a new
destination or chosen from previously saved destinations. The satellite navigation system will then
output directions to the destination in the form of either a visual map or a list of directions.
Flow-chart
Q8. a) Draw and describe the use of four flowchart symbols.
Input /
A parallelogram represents input or output.
Output
Flow lines uses arrows to show the direction of flow of process and data.
The algorithm makes use of the MOD function which divides one integer by another and gives the
remainder, like MOD (5, 2) = 1 (i.e. 5 divided by 2 gives 2 remainder 1).
Q10. There are two different ways of drawing a flowchart to input ten numbers and output the total.
Draw flowcharts for pre-condition loop and the post-condition loop.
a) Pre-condition loop:
It is quite complex to understand and design. Flowchart is easier to construct and understand.
1) Assignment statement:
The variable on the left of the ← operator is assigned a value or an expression using mathematical
operators.
Example: Total ← 0
Variable 'Total' stores the value '0'.
Total ← Total + Num
Variable 'Total' stores the value 'Total + Num'.
2) Conditional statement:
It is used, when different actions need to be performed by an algorithm according to the values of the
variables.
(i) "IF ... THEN ... ELSE ... ENDIF" Conditional Selection statement:
Allows to test the given condition and execute the instructions if it is true or false, based on “relational
operator” (like, <, >, =, AND, OR, NOT).
(ii) "CASE ... OF ... OTHERWISE ... ENDCASE" Conditional Switching statement:
Allows to make choice and execute the instructions if it is true, it is based on “equality operator ( = )”.
Iteration is the term given to the repetition of a block of instructions (code) within a computer
program for a given number of repetitions or continuously either for as long as a condition
continues to be true or until a condition becomes true.
When a block of code is executed out again, it is called an iteration.
When a cycle of instructions is carried out in a repeated manner, it is called a loop.
Iteration allows programmers to simplify a program and make it more efficient. Instead of writing
out the same lines of code again and again, a programmer can write a section of code once, and
ask the program to execute the same block of code repeatedly until it is no longer needed.
The FOR ... NEXT loop is used to execute the block of instructions repeatedly for a fixed number of
times.
In the following example, the counter variable "X" starts at 1 and its value is incremented by 1 every
time the code repeats until it reaches a value of 10, to terminate the loop.
The WHILE ... DO loop. executes the block of code and continue to loop or repeat only if the condition is
True.
It checks for the condition before executing the block of code.
The loop may not execute the block of code at least once, if the condition is False.
The following example will continue to take user input till the value of Count is less than 10 (ten times:
for Count = 0 to 9).
Example: Count ← 0
WHILE Count < 10 DO
INPUT Num
Count = Count + 1
ENDWHILE
The REPEAT... UNTIL loop, executes the block of code and continue to repeat until the condition is
True (repeats only if the condition is False). It checks for the condition after executing the block of
code. It needs to be executed at least once.
The following example will continue to take user input until the user inputs a value between 0 and 50
inclusive.
This loop structure is used for Range checks validation.
Example: REPEAT
INPUT Num
IF (Num < 0 OR Num < 50 THEN
PRINT "Invalid data, please re-enter the number."
ENDIF
UNTIL (Num >= 0 AND Num <= 50)
Q13. a) Write an algorithm using pseudocode to input ten numbers. Calculate and output the
average.
Total = 0
FOR Count = 1 TO 10
INPUT "Enter the number :", Num
Total = Total + Num
NEXT Count
Avg = Total / 10
OUTPUT "The sum of the input numbers is ", Avg
b) Write an algorithm using flowchart to input ten numbers. Calculate and output the average.
Flowchart and Pseudocode are two different tools to represent an Algorithm that illustrates a solution to
a given problem and helps to develop a software.
c) How a programmer could decide whether to use flowchart or pseudocode algorithm? Give its
advantages and limitation.
The type of algorithm to use is chosen after analyzing the time complexity and space complexity of an
algorithm.
Flowcharts has the advantage of being easy to understand the logic of given problem compared to
Pseudocode. It is used in programming to show the steps to write a program.
But has a limitation of time and space complexity because it is a diagrammatic representation with
various symbols to illustrate a solution to a problem.
Evaluation is the process that allows us to make sure our algorithm does the job it has been designed
to do and to think about how it could be improved.
If an algorithm meets these four criteria it is likely to work well. The algorithm can then be
programmed.
Efficiency.
Correctness.
Appropriateness.
Although an algorithm is expected to produce the correct outputs, correctness can still be
measured in terms of:
Accuracy: How many decimal places produce output with greater accuracy (e.g. more
decimal places).
Length: If the problem is simple then a short algorithm would normally be expected.
Speed: If the output needs to be generated quickly, then the algorithm must be able to generate
output quick enough.
Memory requirements: An algorithm should use a minimum possible memory.