Problem => Algorithm => Code
Examples of Problems:
- Cooking boiled eggs - Integer division of two numbers - Missile Pursuit (or animal chasing a prey) - Play a game like chess - Recover 3D information from stereoscopic images
It is important to realize the different nature of the two tasks:
finding an algorithm to solve a problem
translating an algorithm into a code (coding)
Because computers are stupid, that is, lack a theory of mind and cannot guess your desires and intentions, you must provide very detailed and exhaustive instructions.
Learning to code takes time because it requires to master not only the semantic but also the syntax (=grammar) of the language.
Just as learning to play an instrument, or speak a second language, programming is a skill that can only be acquire with lots of practice.
To begin our journey into programming, we will use Scratch, a system developed the MIT media lab to teach how to program to kids.
A great advantage of Scratch is that programs are created using a graphical interface, preventing syntactic errors. Thus, you can learn the language without having to learn its grammar!
One can either work online at https://scratch.mit.edu/projects/editor/?tip_bar=home or offline, by downloading Scratch at
https://scratch.mit.edu/scratch_1.4/ (version 1)
https://scratch.mit.edu/scratch2download/ (version 2)
It may be a good idea to run the tutorial “Getting Started with Scratch” on the website
motion group, take the instruction
turn 15 degrees and drag
it onto the
Double-Click repeatedly on the block
turn 15 degrees, you should see
the cat (
sprite 1) rotate.
In Scratch, when one double-clicks an instruction in the
panel, the computer executes it.
Drag the instruction
move 10 steps from the motion group, and add it
to the bottom of the instruction
turn 15 degrees. Change the value
You have just created a block of instructions, that is, your first program or script, Bravo!
Double-Click on the block and see the sprite moving.
Note that inside a block, instructions are exectuted sequentially, one after the other. Can you prove it?
Experiment with changing the argument of the instruction
move(Tip: to clear the drawing area, move the instruction
pen/clearto the script window and execute it)
Click on the
pen group, and add
pen down at the top of the block.
Construct the following scripts and play with them until you are sure to understand the behavior of the computer..
Argument of an instruction (change
move 10 steps)
Block of instructions and sequential execution
Computers are good at doing tasks repeatedly (as they do not get tired).
Click on the “Control” group, and try to construct the following script:
Clicking on the
greenflag will execute the block of instructions
Repeatinstruction executes the inner block of instruction a number of times specified as an argument. This is called a loop
Adjust the parameter of the Repeat instruction so that the sprite draws a full circle when you click once on the green flag.
Replace the repeat instruction by
Modify the script as follows:
Tip: the condition
key space pressed? is in the
This illustrates a repeat…until loop: the inner block is executed until the condition is satisfied.
Add a new sprite, and duplicate the script from sprite1. Click on the green flag. You should see the two sprites running in circles.
Remark that the scripts associated to the two sprites run in parallel (rather than sequentially).
Create a new scratch project, and change the costume of the sprite into a ball.
Then write and execute the following script.
You should see the ball bounce on the edges.
With Scratch, use the instructions “pen down” and “move” and “turn” to (a) make the cat draw a square (with sides measuring 100 steps) (b) draw an hexagon (c) draw a circle
Using the Control/Forever, make the cat turn continuously along a circle.
Delete the cat. Using new sprite/open, add a ball.
Make the ball move automatically horizontally from left to right and bounce when it touches an edge (tip: use Control/forever)
Make the ball follow the mouse.
Add a second ball that follows the first.
Create a script that asks for your name and then displays “Hello !”. Tip: use the instructions
operator/joinand the variable
Using the group
variable, we are going to create a variable
and make it display continuously the x-coordinate of the ball.
The concept of variable is very important. You can think of it as a name for a object that can change (here the object is a number).
Now study the following script:
The loop is executed 100 times. Each time, the value of the variable
a is incremented by 1, and is used to compute new
coordinates where to sprite is instructed to moved to.
“Multiply by adding”: Write a program that reads in two integer numbers and displays their sum.
“Guess a number”. Make Scratch pick up a random number in the interval [1,100],
and loop asking you for a guess and reply either
too high, or
you win! depending on your answer.
We are going to estimate the number PI by a Monte Carlo method.:
Repeatly (e.g. 2000 times) picks up two random numbers on the interval [-1, 1]. This corresponds to a dot inside a square of size 2x2.
Count how many times the dot falls within the circle of radius 1 centered on the origin (Pythagore helps you here: the dot is within the circle iff
(x * x + y * y) < 1.)
The proportion of dots falling within the circle, multiplied by four (the area of the square), is an estimate of teh area of the disk, that is, the number pi.