0% found this document useful (0 votes)
36 views

7.2 Designs of Algorithm

The document discusses computer systems and subsystems, problem decomposition, and methods used to design algorithms including structure diagrams, flowcharts, and pseudocode. It provides examples and questions to describe these concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

7.2 Designs of Algorithm

The document discusses computer systems and subsystems, problem decomposition, and methods used to design algorithms including structure diagrams, flowcharts, and pseudocode. It provides examples and questions to describe these concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Designs of Algorithm

(Computer systems, sub-systems and decomposition)


Structure, Flowchart & Pseudocode
Objectives: Student should be able to -
 describe computer systems and sub-systems.
 problem decomposition into component parts.
 methods used to design and construct solutions to problems (Structure diagram, Flowchart &
Pseudocode algorithm).

Q1. a) Describe Computer System.

 A computer system is made up of software, data, hardware, communications and people.


 A computer system is a set of integrated devices that input, process, output, communicate and store
data and information.
 A computer system can be divided into a set of sub-systems.
 Each sub-system can be further divided into sub-systems and so on until each sub-system just
performs a single action.

b) Describe what is meant by Subsystem.

 A Sub-system is derived from a system and it is an integral part of a large system.


 A Sub-system is a system in its own, but it normally cannot provide a useful function by itself.
 It must be integrated with other subsystems to make a system.

Q2. a) Describe Top-down design, a modular approach to provide a solution to a problem.

 Top-down design is the decomposition of a computer system into sub-systems.


 It is the process of breaking down a system into a set of subsystems, then further breaking each sub-
system down into a set of smaller sub-systems, until each sub-system just performs a single action.

b) Give three advantages of ‘Top-down design’.

1. Smaller sub-systems are easy to design and manage.


2. Easy to test and debug errors.
3. Several programmers can work independently to develop and test each subsystem of a large
system at the same time.

c) Give three drawbacks of ‘Top-down design’.

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.

CS 2210/ 0478 Page 1 of 12


a) Name and describe four component parts of any computer system.

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

Methods used to design and construct a solution to a problem

Q4. a) Describe what is meant by Algorithm.

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

b) Describe how to design and construct an algorithm.

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.

CS 2210/ 0478 Page 2 of 12


Q5. Describe the following three methods of designing an algorithm to solve a problem.
 Structure diagram.
 Flowchart.
 Pseudocode.

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:

 A Flowchart is a diagram that represents an algorithm, the work flow or process.


 It shows the steps to be carried out to solve a problem.
 It uses variety of boxes and arrows to show the process to be done and the direction of flow of data.

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.

Draw a structured diagram for the navigation system.

CS 2210/ 0478 Page 3 of 12


Q7. An Alarm app for a smart phone allows to the set the alarm time, checks and sounds the alarm
for 2 minutes if the current time is equal to the set time.

Draw a structured diagram for the Alarm app computer system.

Flow-chart
Q8. a) Draw and describe the use of four flowchart symbols.

Name Symbol Function


Terminator
An oval represents a Start or End of an algorithm.
( Start / End)

Input /
A parallelogram represents input or output.
Output

A rectangle represents a process. It is used for


Process
arithmetical operation and data-manipulations.

A diamond represents a decision to take for the


Decision
direction of the flow of data.

b) Describe the purpose of flow lines in a flowchart.

 Flow lines uses arrows to show the direction of flow of process and data.

CS 2210/ 0478 Page 4 of 12


Q9. Draw a flowchart algorithm which takes a positive number as input, determines whether the
number is even or odd and output the result.

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:

CS 2210/ 0478 Page 5 of 12


Post-condition loop:

Q11. Give difference between structure diagram and flowchart.

Structure diagram Flowchart


It represents the software architecture by breaking It represents the flow of control in an algorithm
down a problem into more details with the help of with graphical design using variety of boxes
modules. and arrows.

Easier to identify modules or sub-modules in a Difficult to recognize and identify different


program. modules.

Represents the flow of controls in sequential


Represents the structure in hierarchical method.
structure.

It is quite complex to understand and design. Flowchart is easier to construct and understand.

CS 2210/ 0478 Page 6 of 12


Pseudocode
Q12. Describe the function of each of the following types of pseudocode statement and give an example
in pseudocode for the use of each one.

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.

There are two types of conditional statements:

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

Example: IF (Age < 18) THEN


OUTPUT "Child"
ELSE
OUTPUT "Adult"
ENDIF

(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 ( = )”.

Example: CASE Grade OF


"A" : OUTPUT "Excellent"
"B" : OUTPUT "Good"
"C" : OUTPUT "Average"
OTHERWISE
OUTPUT "Improvement is needed"
ENDCASE

CS 2210/ 0478 Page 7 of 12


3) Iterative or Looping statement:

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

There are three types of Looping structures:

(i) "FOR ... TO ... NEXT" is called Counting loop:

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.

Example: FOR Count = 1 TO 10


INPUT Num
Total = Total + Num
NEXT Count

(ii) "WHILE ... DO ... ENDWHILE" is a pre-conditional 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

CS 2210/ 0478 Page 8 of 12


(iii) "REPEAT ... UNTIL" is a post-conditional loop:

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.

CS 2210/ 0478 Page 9 of 12


Q14. a) What is the similarity between Flowchart and Pseudocode.

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.

b) What are the differences between Flowchart and Pseudocode.

Flowchart is a pictorial representation of an algorithm, while pseudocode is an informal high-level


description of an algorithm.

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.

 Pseudocode has the advantage of being independent of a specific language.

CS 2210/ 0478 Page 10 of 12


 Pseudocode also has the advantage of being easier to write because you don't have to satisfy the
restrictions of the syntax of a language, so it is used when you want to express an idea quickly.

Q15. Describe how to check the Effectiveness of an algorithm or solution.

In order to consider the effectiveness of a given solution as the following questions -

1. Does the solution work for all sets of test data?


2. Does the solution have any unnecessary process that are never used?
3. Are any actions repeated unnecessarily?
4. Can the solution be simplified and still work as well?

Q16. What is meant by Evaluation of an algorithm?

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.

Once algorithm is written, it should be checked to make sure:

 it is easily understood – is it fully decomposed?


 it is complete – does it solve every aspect of the problem?
 it is efficient – does it solve the problem, making best use of the available resources (e.g. as quickly
as possible/using least space)?
 it meets any design criteria we have been given.

If an algorithm meets these four criteria it is likely to work well. The algorithm can then be
programmed.

Q17. Algorithms should be evaluated using the following criteria:

 Efficiency.
 Correctness.
 Appropriateness.

a) How do you analyze the efficiency of an algorithm.

An algorithm's efficiency can be judged in terms of:

 Speed: How quick the algorithm produces the required output.


 Complexity of algorithm: How many lines of code the algorithm contains.

b) How do you check the correctness of an algorithm.

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

CS 2210/ 0478 Page 11 of 12


 Range: Will the algorithm work with the complete range of inputs? Or can it only deal with
positive numbers, whole numbers, numbers below 1 million, etc.
 Reliability: Will the algorithm always produce correct output within the range that it is designed to
work? Or are there values which it will not accept (e.g. zero).

c) How do you check the appropriateness of an algorithm.

Appropriateness can be measured in terms of:

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

CS 2210/ 0478 Page 12 of 12

You might also like