Decomposition CS Thinking Example 2
Follow the full solution, then compare it with the other examples linked below.
Example 2
mediumDecompose the task of building a simple calculator program.
Solution
- 1 Step 1: Input sub-problem: get two numbers and an operation from the user.
- 2 Step 2: Processing sub-problem: perform the correct operation (add, subtract, multiply, divide).
- 3 Step 3: Output sub-problem: display the result. Error handling: check for division by zero, invalid input.
Answer
Sub-problems: input handling, operation selection, calculation, output display, error handling.
Decomposing a programming task helps us plan the structure of our code. Each sub-problem can become a function or module.
About Decomposition
Breaking a complex problem into smaller, independently-solvable parts that combine into a complete solution.
Learn more about Decomposition โMore Decomposition Examples
Example 1 easy
You need to plan a school sports day. Decompose this problem into smaller sub-problems.
Example 3 easyDecompose the problem of creating a sandwich into at least 4 sub-tasks.
Example 4 mediumDecompose building a class voting app into four modules, and identify one dependency between them.