Steps of Problem Solving and Algorithms
This is a free course. Log in to run the code exercises and track your progress.
Steps of Problem Solving and Algorithms
Before writing any program we first understand the problem. Every program follows the input → process → output cycle: it takes data in, works on it, and gives a result. Solving a problem on a computer has four steps:
- Analyse the problem: what is given (input) and what is asked (output)?
- Develop an algorithm: write the exact steps in plain language.
- Code: turn the algorithm into a program, here in Python.
- Test and debug: run the program on sample inputs and fix the mistakes.
An algorithm is a finite, ordered set of clear steps that solves a problem. A good algorithm has a definite start and end, takes an input, gives an output, and every step is unambiguous. For example, an algorithm to find the average of three numbers:
Step 1: Start
Step 2: Read three numbers A, B and C
Step 3: SUM = A + B + C
Step 4: AVG = SUM / 3
Step 5: Print SUM and AVG
Step 6: Stop
In Python each step becomes one line. Here is the same algorithm for two numbers; notice how each line of code matches one step.
a, b = map(int, input().split())
total = a + b
print("Sum =", total, "Average =", total / 2)
Read three whole numbers from one line and print their sum and average, exactly as Sum = 21 Average = 7.0.
a, b, c = map(int, input().split())
total = 0 # Step 3: add the three numbers
print("Sum =", total, "Average =", total / 3)Log in to edit and run this code.