How to solve a task
Problem
The blank screen
You have a task: on the first day you save €1, on the second €2, on the third €3, and so on. How many euros will you have after N days?
You open the editor and… you do not know which line to start with. This happens to everyone, and the reason is rarely that you do not know the language. Usually it is not yet clear what exactly the program has to do. That is why the solution starts before the code: on paper or in your head.
The idea
Read the task
The tasks in this course look like this:
Savings. On the first day you save €1, on the second €2, on the third €3, and so on. The input is the number of days N. Print how many euros you have saved in total.
Input:
5Output:15
Before you think about a solution, answer three questions:
- What do I get? One whole number N, the number of days.
- What do I have to print, and in what form? One number, with no other text.
- Do I understand the example? Work it out by hand: 1 + 2 + 3 + 4 + 5 = 15. It matches the output.
If your calculation by hand does not give the output from the example, you have not understood the task. Read it again before you write anything.
The idea
Split the task
A big task is hard to solve all at once. Split it into parts, each of which you know how to do:
1. read the number of days N2. add up the numbers from 1 to N3. print the totalThe first and third parts are clear. The second one is still big, so split it too. How do you add things up by hand? You keep the total in your head and add the next number to it, one at a time. That is what a cashier does when ringing up your shopping.
Keep splitting until each part is so small that it is clear how to do it.
Next to the animation is the program that follows these parts. You will learn the words in it in the next module. For now, watch how each part turns into a line or two of code.
Scanner in = new Scanner(System.in);int n = Integer.parseInt(in.nextLine());int sum = 0;for (int day = 1; day <= n; day++) { sum += day;}System.out.println(sum);Step by step, in words
- All at once, the task is big: how many euros do you save in N days?
- You split it into three parts. Each one is clear on its own.
- Part 1: read N. In the example, N = 5.
- Part 2: the total starts at 0, and each number from 1 to N is added to it.
- Dry run: 0 + 1 + 2 + 3 + 4 + 5 = 15.
- Part 3: print the total. The output is 15, just like in the example.
The idea
Write the plan in words
Now put the steps in order, in plain words:
read Nsum = 0for each number from 1 to N: add the number to the sumprint the sumThis is called pseudocode: a description of the solution that looks like a program but is not in any language. There are no strict rules. What matters is that each step is clear and the steps are in the right order.
The indent on the fourth line shows that this step repeats for each number. Soon you will see how to write that in a real language.
See it
Dry run
Does the plan work? Check it before there is any code. Take the value from the example (N = 5) and go through the steps by hand, writing down the sum after each one:
| Step | Number | Sum before | Sum after |
|---|---|---|---|
| start | 0 | ||
| 1 | 1 | 0 | 1 |
| 2 | 2 | 1 | 3 |
| 3 | 3 | 3 | 6 |
| 4 | 4 | 6 | 10 |
| 5 | 5 | 10 | 15 |
The last step prints 15, exactly as in the example. The plan is right, at least for this case.
This is called a dry run: you go through the solution with sample values without running it. It takes a few minutes, and it catches logic errors before you have written a single line of code.
More precisely, if you are curious
- There is another way to get the same sum: N × (N + 1) / 2. For N = 5 that is 5 × 6 / 2 = 15. Two different plans give the same result. The second one is faster, but to come up with it you have to know the formula. The first one follows exactly how you would work it out by hand.
- You will follow values line by line like this with real code too. Editors have a tool for it, the debugger: it stops the program on a line you choose and shows the values of everything in it.
Try it
Find the bug
For N = 5 this plan gives 16 instead of 15. Do a dry run in your head. Which line is the bug on?
Pick the line with the bug.
The sum starts at 1, not at 0. So one extra unit, which was never saved, gets added before the first number: 1 + 1 + 2 + 3 + 4 + 5 = 16. Before you have added anything, the total is 0.
The idea
Check the solution against the example
Once you have a program, the check is the same, except that you run it instead of working it out by hand:
- Enter the input from the example and compare your output with the one in the task. Compare character by character: "The sum is 15" is not "15". Solutions are often checked automatically, and extra text makes the answer wrong.
- Try your own values too, especially edge cases: the smallest allowed value, zero, a very large number.
- If the output does not match, do a dry run of your code with the sample value. The place where the values stop matching what you expect is the bug.
Try it with the savings plan: what does it give for N = 1? And for N = 0?
The answer
- For N = 1 the step repeats once: the sum becomes 0 + 1 = 1. That is right: after one day you have €1.
- For N = 0 there are no numbers from 1 to 0, so the step does not run even once. The sum stays 0. That is right too: in zero days you have saved nothing.
Test