Algorithm Design and Problem Solving
Key Learning Objectives:
1. Computational thinking skills (abstraction and decomposition).
2. Writing algorithms that solve problems using structured English, flowcharts, and
pseudocode.
3. Stepwise refinement for systematic problem solving.
Overview
To design a computer system that performs specific tasks or solves problems, the task
must be clearly defined with a plan of what will be computed and how it will be
computed.
Techniques Covered
- Tools and techniques to create software solutions that work with hardware.
- The importance of practice in developing computational thinking skills.
- Using flowcharts and pseudocode to plan and check solutions before actual
programming.
- Coding to test the viability of a solution.
Programming Languages
- Java
- Python
- VB.NET
Pre-existing Knowledge
Students should understand the following concepts:
1. Procedure – A block of code designed to perform a specific task.
2. Function – A type of procedure that returns a value.
3. Algorithm – A step-by-step process or set of rules to solve a problem.
4. Structured English – A plain language approach to describe algorithms.
5. Flowchart – A visual representation of an algorithm.
6. Pseudocode – Writing algorithms in plain text before implementing them in a
programming language.
Computational Thinking Skills
Key Terms:
- Abstraction – Focusing on important details while ignoring irrelevant ones.
- Decomposition – Breaking down complex problems into smaller, manageable parts.
- Pattern recognition – Identifying similarities in problems to use similar solutions.
Using Abstraction
- Abstraction helps to focus only on the important details necessary for solving a
problem.
- Examples include simplified maps (Figure 9.1), which only display essential information
like roads and town names.
Benefits of abstraction
- Reduces unnecessary details, making solutions easier to develop and understand.
- Results in smaller, faster programs that are easier to download and execute.
- Enhances customer satisfaction by delivering more targeted solutions.
Using Decomposition
- Decomposition breaks complex problems into smaller parts that are easier to solve
individually.
- Once each part is manageable, it can be developed into a solution and integrated
into the final program.
- Pattern recognition is used in decomposition to reuse parts of solutions that are similar
to other problems.
- Modular Programming (Figure below) demonstrates decomposition by dividing the
program into smaller modules, each performing a specific function. This saves
development time and allows code reuse.
Points to Note:
- Computational thinking involves breaking down problems using abstraction and
decomposition.
- Abstraction focuses on essential details, simplifying complex problems.
- Decomposition divides problems into smaller, solvable parts, allowing easier
integration of solutions.
- These skills are critical in programming and software design, leading to efficient,
scalable, and reusable code solutions.
Writing Algorithms that Provide Solutions
Key Concepts:
- Algorithm: A step-by-step process to solve a problem.
- Flowchart: A diagrammatic representation of an algorithm.
- Pseudocode: Plain language representation of an algorithm to show logic before
coding.
Steps for Designing Algorithms:
1. Understand the problem.
2. Identify inputs and expected outputs.
3. Plan the steps to solve the problem.
4. Write the algorithm using flowcharts and pseudocode.
5. Test the algorithm to ensure it works correctly.
---
Example 1: Write an Algorithm to Find the Average of Three Numbers
Inputs:
- Three numbers: `n1`, `n2`, `n3`.
Outputs:
- The average of the numbers.
Pseudocode:
1. Start
2. Input three numbers: `n1`, `n2`, `n3`
3. Calculate the sum: `sum = n1 + n2 + n3`
4. Calculate the average: `average = sum / 3`
5. Output the average
6. End
Flowchart:
- A simple visual showing the steps with input, calculation, and output boxes.
---
Example 2: Algorithm for Finding the Largest of Two Numbers
Inputs:
- Two numbers: `a`, `b`.
Outputs:
- The larger of the two numbers.
Pseudocode:
1. Start
2. Input two numbers: `a`, `b`
3. If `a > b`, then print `a`
4. Else print `b`
5. End
Key Terms
- IF-ELSE: A decision-making structure used to perform different actions based on
conditions.
Writing Algorithms with Flowcharts and Pseudocode
1. Flowchart Components:
- Start/End symbols (ovals).
- Input/Output symbols (parallelograms).
- Process symbols (rectangles).
- Decision symbols (diamonds).
2. Pseudocode:
- Structured like actual code, but written in plain language.
- Easier for non-programmers to understand the flow of the program.
Example 3: Algorithm for Counting from 1 to 10
Pseudocode:
1. Start
2. Set `count = 1`
3. While `count <= 10`
- Print `count`
- Increment `count` by 1
4. End
Input, Process, Output (IPO) Model:
- Input: Data entered into the system (e.g., user inputs numbers).
- Process: Operations performed on the input (e.g., calculating average).
- Output: Result from the process (e.g., displaying average).
Writing Modular Programs
1. Modular Design:
- Break the program into smaller subroutines or functions.
- Each subroutine performs a specific task and can be reused.
2. Example of Modular Code:
```python
def get_input():
return int(input("Enter a number: "))
def find_largest(a, b):
if a > b:
return a
else:
return b
num1 = get_input()
num2 = get_input()
largest = find_largest(num1, num2)
print(f"The largest number is {largest}")
```
---
Example 4: Using Loops and Conditions in Algorithms
Pseudocode for a Loop:
1. Start
2. Set `i = 1`
3. While `i <= 5`
- Print `i`
- Increment `i` by 1
4. End
Key Tips for Algorithm Design:
- Clarity: Ensure each step of the algorithm is clear.
- Stepwise Refinement: Break complex problems into simpler parts.
- Modularity: Divide the algorithm into reusable functions or modules.