Plan the solution before writing C
Turn a problem statement into an algorithm, pseudocode, and a flowchart that can be tested by hand.
After this lesson
You should be able to
- Identify inputs, outputs, decisions, and repetition in a problem.
- Dry-run an algorithm with representative and boundary values.
A recipe with an end
An algorithm is a finite, ordered, unambiguous sequence of steps. Start by writing the input and required output. Then decompose the transformation into small steps. If a step depends on a condition, show a decision. If it repeats, state when repetition stops.
Pseudocode expresses this plan without worrying about C punctuation. A flowchart shows the same control flow visually: oval for start/end, parallelogram for input/output, rectangle for processing, and diamond for a decision.
Example: largest of three values
Initialize max with the first value. Compare the second value with max and update when larger. Repeat for the third. This structured solution avoids trying to list every ordering of three numbers.
Dry-run with 4, 9, 9. The result should remain 9. Equal values are useful because they reveal whether the comparison and update rule are sensible.
READ a, b, c
max <- a
IF b > max THEN
max <- b
END IF
IF c > max THEN
max <- c
END IF
PRINT maxTry it yourself
Write pseudocode that reads a positive integer n and prints the sum from 1 through n.
Need a hint?
Keep two changing values: a counter and an accumulated total.
Check the worked solution
Initialize sum to 0 and i to 1. While i is at most n, add i to sum and increment i. Print sum after the loop.
READ n
sum <- 0
i <- 1
WHILE i <= n
sum <- sum + i
i <- i + 1
END WHILE
PRINT sumQuick check
What makes an algorithm different from an open-ended set of suggestions?
Why this lesson exists
Syllabus mapping
Algorithms · Flowcharts · Pseudocode · Program design · Structured programming
Maps to course outcomes CO1, CO2.