Skip to content
Study Hall
Lesson 2 of 7Software Development & the CPT
Unit 7 · Lesson 2 of 7

Problem-Solving, Pseudocode and Algorithm Design

Strategies for breaking a problem apart, the notations that capture a solution before any code exists, and how to translate them into Java.

6 min read11 practice questionsAsk AI about this →

Key things to remember

  1. 1Decomposition splits a problem into independent sub-problems; pattern recognition spots the ones you have already solved.
  2. 2Abstraction means deliberately ignoring detail that does not affect the solution.
  3. 3An IPO chart lists inputs, processing and outputs, and exposes values that were never actually supplied.
  4. 4Pseudocode is structured English with no syntax rules; it exists so the logic can be checked cheaply.
  5. 5In a flowchart, a diamond is a decision and must have exactly two exits, both of which lead somewhere.
  6. 6Boundary words in a specification — inclusive, at least, up to — decide between < and <=.

A large problem is not solved by staring at it harder. It is solved by turning it into several small problems you already know how to do. That conversion is the real skill in programming, and it is entirely separate from knowing Java syntax — which is why a good designer can plan a program in a language they have never used. Five strategies do most of the work.

StrategyWhat it meansExample
DecompositionSplit the problem into independent sub-problems you can solve one at a timeA mark reporter becomes: read data, validate it, calculate statistics, format output
Pattern recognitionNotice that a sub-problem is one you have solved before"Find the top scorer" is the standard maximum search wearing a different hat
AbstractionIgnore the details that do not affect the solutionTo sort marks you do not need names, subjects or dates — only the numbers and their order
Working backwardsStart from the required output and ask what must exist just before itTo print an average you need a total and a count, so the loop must produce both
Solve a smaller caseDo the 3-element version by hand, then generalize itReverse a 4-element array on paper, spot the pairing, then write the loop
Do the small case by hand first
If you cannot solve the problem for three values with a pencil, no amount of code will solve it for three hundred. Working a tiny case by hand tells you what the steps actually are, and it hands you a test case with a known answer for free.

Every program is input, then processing, then output. An IPO chart writes those three columns down before anything else, and it is the fastest way to discover that a requirement is impossible — if a value appears in the processing column but was never an input, you have found a gap while it is still free to fix.

InputProcessingOutput
Number of marks, nValidate that n is between 1 and 30Error message if invalid
Each mark, as a whole numberValidate each mark is 0 to 100Error message and a re-prompt
Accumulate the total across all n marksThe total
Divide the total by n using real divisionThe average to one decimal place
Track the largest mark seen so farThe highest mark
An IPO chart for a mark statistics program. The blank input cells show which values are derived, not entered.

Pseudocode is the logic of a program written in structured English, with no language syntax to get wrong. It has no official rules, but useful pseudocode is consistent: capitalize the keywords, indent the bodies, and use one line per action. The point is that you can get the logic wrong ten times in two minutes, instead of once in two hours.

PSEUDOCODE - average of n values entered by the user

READ n
IF n <= 0 THEN
    DISPLAY "You must enter at least one value"
    STOP
END IF

SET total TO 0
FOR i FROM 1 TO n
    READ value
    SET total TO total + value
END FOR

SET average TO total / n
DISPLAY "The average is", average
Structured English, not Java. Notice there are no semicolons, no types and no brackets to get wrong.
Work out the steps in words first; turning them into Java is then mechanical.PSEUDOCODE — plain wordsJAVA — exact syntaxSET max TO the first numberint max = a[0];FOR each remaining numberfor (int i = 1; i < a.length; i++) { IF number > max THEN if (a[i] > max) { SET max TO number max = a[i]; END IF }END FOR}DISPLAY maxSystem.out.println(max);Pseudocode has no compiler, so there are no semicolons, no braces and no type names to get wrong.Notice the shape survives the translation: one setup line, one loop, one decision inside it, one output line.
Fig 1Each pseudocode line maps onto one or two lines of Java, which is exactly what makes the translation mechanical.
PseudocodeJava
READ nint n = input.nextInt();
SET total TO 0double total = 0;
FOR i FROM 1 TO nfor (int i = 1; i <= n; i++) {
IF n <= 0 THENif (n <= 0) {
DISPLAY xSystem.out.println(x);
END IF / END FORThe closing brace }
Because the pseudocode was written line by line, the translation is line by line too. Java only adds types, semicolons and care over integer division.
import java.util.Scanner;

public class AverageCalculator {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("How many values? ");
        int n = input.nextInt();

        if (n <= 0) {
            System.out.println("You must enter at least one value");
            return;
        }

        double total = 0;
        for (int i = 1; i <= n; i++) {
            System.out.print("Value " + i + ": ");
            total += input.nextDouble();
        }

        double average = total / n;
        System.out.println("The average is " + average);
    }
}
The same algorithm, now with types, semicolons and a Scanner. The structure is unchanged.
Pseudocode is not Java with the semicolons missing
Never write pseudocode as broken Java, and never paste pseudocode into a source file and hope. They are two different notations for two different stages. Pseudocode exists so that the logic can be checked without the compiler having an opinion about it.

A flowchart draws the same logic as a path rather than a list. It is better than pseudocode at one specific thing: showing every route through a decision at a glance, including the route you forgot. A diamond asks a yes or no question, so exactly two arrows must leave it and both must lead somewhere. If a diamond has an arrow leaving only one side, you have just found a missing case before writing a line of code — and a dangling exit in a flowchart becomes a missing else in the program, where the code silently does nothing at all.

The shape carries meaning — never draw everything as a box.START / ENDterminator — one entry, one exitINPUT / OUTPUTread a value, print a resultPROCESSa calculation or assignmentDECISIONa yes/no test — exactly two exitsa loop is a decision whose “yes” goes back upPseudocode does the same job:READ markIF mark >= 50 THEN PRINT "Pass"ELSE PRINT "Fail"ENDIF
Fig 2The standard shapes: oval for start and end, parallelogram for input and output, rectangle for a process, diamond for a decision.
The first true condition wins — everything below it is skipped.mark >= 80trueLevel 4falsemark >= 70trueLevel 3falsemark >= 60trueLevel 2falseelse: Level 1all paths rejoin hereOrder the tests from the highest threshold down, or an 85 matches "mark >= 60" first.
Fig 3A decision has exactly two exits, and every exit must eventually rejoin the main path or reach an end.

When someone hands you a specification, the algorithm is not yours to invent freely — it must meet every stated condition. Read it with a highlighter and mark three things: the exact inputs and their valid ranges, the exact outputs and their formatting, and every boundary word. Words such as inclusive, at least, up to and no more than each decide between < and <=, and each one of them is a bug waiting to happen.

  1. Underline every noun in the specification — these become your variables and arrays.
  2. Underline every verb — these become your methods and processing steps.
  3. Circle every number, limit and boundary word, and write down which comparison operator it implies.
  4. Write the IPO chart, and check that every processing value traces back to a real input.
  5. Write the pseudocode, then hand-trace it on the smallest case you can think of.
  6. Only now open the editor.

Worked examples

Reveal one step at a time and try to beat it to the next line.

Example 1
Design an algorithm to a specification: read a whole number of items sold and a unit price, apply a 10 percent discount if 20 or more items are sold, then display the total with 13 percent HST added. Produce an IPO chart and pseudocode.
Example 2
Use decomposition and pattern recognition to plan a program that reports the highest, lowest and average of a set of temperatures that may be negative.
Example 3
Convert this pseudocode to Java and state the printed result: SET total TO 0, FOR i FROM 1 TO 5, SET total TO total + i * 2, END FOR, DISPLAY total.

Where marks get lost

  • Writing pseudocode as Java with the semicolons removed, which defeats the point of a notation the compiler cannot object to.
  • Translating FROM 1 TO n as i < n instead of i <= n, losing the final pass.
  • Ignoring boundary words such as "20 or more", producing a > where the specification required >=.
  • Drawing a flowchart decision with only one exit, which becomes a missing case in the finished program.
  • Designing the whole program at once instead of decomposing it, so no part can be tested until everything is written.

Flashcards 9

What is decomposition?
What is abstraction in problem solving?
What does an IPO chart list?
What is pseudocode?
+ 5 more

Practice

1
Match each problem-solving strategy to its description.
Decomposition
Pattern recognition
Abstraction
Working backwards
2
Which Java loop correctly translates the pseudocode FOR i FROM 1 TO 10?
3
Trace: SET total TO 0, FOR i FROM 1 TO 5, SET total TO total + i * 2, END FOR. What is total?
4
A specification says "apply the discount when 20 or more items are sold". Which condition is correct?
5
Order the design steps that should happen before any code is written.
  1. 1Build the IPO chart
  2. 2Hand-trace the design on a small case with a known answer
  3. 3Write the pseudocode or draw the flowchart
  4. 4Identify the inputs, outputs and boundary conditions in the specification
6
What does it mean that a flowchart decision must have exactly two exits?
7
What are the three columns of an IPO chart?
8
Recognizing that "find the student with the highest mark" is the same algorithm as "find the warmest day" is an example of what?
9
Why write pseudocode at all, when you could write the Java directly?
10
Name the strategy of deliberately ignoring details that do not affect the solution.
11
A program must calculate a subtotal, apply a discount and then add 13 percent HST. In which order must the steps run?

Spot a mistake on this page? Let us know