CS-11(2023 Intake) Topic 9- Algorithm Design and Problem-solving
Chapter-9 Algorithm Design
and
Problem-solving
9.1 Computational Thinking Skills
9.2 Algorithms
1|Page
CS-11(2023 Intake) Topic 9- Algorithm Design and Problem-solving
Abstraction
• Abstraction is a fundamental concept in computational thinking that
involves simplifying complex systems by focusing only on the essential
details while hiding unnecessary complexities.
• Abstraction encourages the development of simplified models that are
suited to a specific purpose by eliminating any unnecessary
characteristics from that model.
• Maps use abstraction to show what is required for a specific purpose, for
example, a road map should only show the necessary detail required to
drive from one place to another.
The benefits of eliminating any unnecessary characteristics from
the model include :
• the time required to develop the program is reduced so the program can
be delivered to the customer more quickly.
• the program is smaller in size so takes up less space in memory and
download times are shortened.
• customer satisfaction is greater as their requirements are met without
any extraneous features.
Using decomposition
Decomposition is a problem-solving technique that involves breaking down
complex problems or tasks into smaller, more manageable sub-problems or
components.
By decomposing a problem, you can better understand its structure, identify
dependencies, and tackle each component individually, often leading to more
efficient and organized solutions.
2|Page
CS-11(2023 Intake) Topic 9- Algorithm Design and Problem-solving
Algorithms
• An algorithm is a step-by-step procedure or set of instructions designed
to solve a specific problem or perform a particular task.
• It represents a systematic approach to problem-solving, where each step
in the algorithm is well-defined and leads to the desired outcome.
• There are several methods of writing algorithms before attempting to
program a solution.
• Here are three frequently used methods.
Structured English
Flowchart
Pseudocode
3|Page
CS-11(2023 Intake) Topic 9- Algorithm Design and Problem-solving
Writing simple algorithms using pseudocode
Identifier
An identifier is a name used to represent a variable, constant, function, or
other elements in the algorithm.
4|Page
CS-11(2023 Intake) Topic 9- Algorithm Design and Problem-solving
Programming Constructs
1. Sequential: In a sequential construct, statements are executed one after
the other, in the order in which they appear in the program.
2. Selective (Conditional): Selective or conditional constructs allow the
program to execute different blocks of code based on certain conditions.
This is typically achieved using if, if-else or case statements.
3. Iterative (Repetitive or Looping): Iterative or looping constructs allow
the program to execute a block of code repeatedly as long as a certain
condition is true, or for a fixed number of iterations. This is achieved
using loop statements such as for loops, while loops, etc..
5|Page
CS-11(2023 Intake) Topic 9- Algorithm Design and Problem-solving
IF statements
IF statements may or may not have an ELSE clause.
6|Page
CS-11(2023 Intake) Topic 9- Algorithm Design and Problem-solving
CASE statements
CASE statements allow one out of several branches of code to be executed,
depending on the value of a variable.
7|Page
CS-11(2023 Intake) Topic 9- Algorithm Design and Problem-solving
Iteration (repetition)
1. Count-controlled (FOR) loops
Post-condition (REPEAT) loops
• The statements in the loop will be executed at least once.
• The condition is tested after the statements are executed and if it
evaluates to TRUE the loop terminates, otherwise the statements are
executed again.
8|Page
CS-11(2023 Intake) Topic 9- Algorithm Design and Problem-solving
Pre-condition (WHILE) loops
• The condition is tested before the statements, and the statements will
only be executed if the condition evaluates to TRUE.
• After the statements have been executed the condition is tested again.
The loop terminates when the condition evaluates to FALSE.
9|Page
CS-11(2023 Intake) Topic 9- Algorithm Design and Problem-solving
Writing pseudocode from a structured English description
Algorithm: Find Greatest of Two Numbers
1. Start
2. Read the values of num1 and num2
3. If num1 is greater than num2,
Set max_num to num1
Else,
Set max_num to num2
4. Display max_num as the greatest of the two numbers
5. Stop
Pseudocode
INPUT num1
INPUT num2
IF num1>num2 THEN
max_num<-num1
ELSE
max_num<-num2
ENDIF
OUTPUT max_num,”is the greatest of the two numbers”
Writing pseudocode from a flowchart
10 | P a g e
CS-11(2023 Intake) Topic 9- Algorithm Design and Problem-solving
11 | P a g e
CS-11(2023 Intake) Topic 9- Algorithm Design and Problem-solving
Stepwise Refinement
• Stepwise refinement is a process used to break down a complex
algorithm or task into smaller, more manageable steps or subtasks.
• Each step or subtask is then refined further until it becomes simple
enough to be implemented directly in code.
• Decomposition is used to break the problem down into smaller and more
manageable parts.
• These parts then need to be written as a series of steps where each step
can be written as a statement in a high-level programming language
12 | P a g e
CS-11(2023 Intake) Topic 9- Algorithm Design and Problem-solving
Qn: An algorithm is expressed as follows:
• input 100 numbers, one at a time
• keep a total of all numbers input that have a value between 30 and
70 inclusive and output this total after the last number has been
input.
• Outline, using stepwise refinement, the five steps for this algorithm
which could be used to produce pseudocode.
• Do not use pseudocode statements in your answer.
ANS:
Step-1 Set total to zero
Step-2 Input a number
Step-3 Check if number greater than 29 and less than 71
Step-4 if check is true - add number to total
Step-5 Repeat from step 2 for a total of 100 iterations
Step-6 Output the total
13 | P a g e