Decomposition CS Thinking Example 2

Follow the full solution, then compare it with the other examples linked below.

Example 2

medium
Decompose the task of building a simple calculator program.

Solution

  1. 1
    Step 1: Input sub-problem: get two numbers and an operation from the user.
  2. 2
    Step 2: Processing sub-problem: perform the correct operation (add, subtract, multiply, divide).
  3. 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