chapter-9 Algorithm Design and Problem-solving.docx
chapter-9 Algorithm Design and Problem-solving.docx
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.
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
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
8|Page
CS-11(2023 Intake) Topic 9- Algorithm Design and Problem-solving
• 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
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
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