12 SCRATCH corrected exercises

The following corrected exercises concern the creation of algorithms using the SCRATCH language.

 corrected exercises SCRATCH language algorithm.

Exercise 1

Use the path_1.sb2 project

Complete the script for the 'ball' sprite to enable him to hit the green target.

If blocked, the elf asks if he should turn right (answer 'D') or left (answer 'G'). The elf then turns in the direction indicated and starts moving forward again if possible.

You will use the following command blocks:

scratch maze

Exercise 2

What does the program below do?

absolute scratch

If the value is positive, it displays the number, otherwise it displays the opposite. The algorithm therefore displays the absolute value of a number as input.

Exercise 3

Which program displays a pentagon?

scratch pentagon

The first and fourth programs. The second is missing a line and the third turns 100° instead of 72°.

Exercise 4

Associate the program with the corresponding result:

scratch

Algorithm 1 –> 2

Algorithm 2 –> 3

Algorithm 3 –> 1

Exercise 5

The elf asks for a number then announces this number and 3 times this number (“3 x 6 = 18”) and starts again until the SPACE key is pressed

You will use the following command blocks:

scratch

Exercise 6

The elf asks for a number then announces the multiplication table by this number (3×1 = 3; 3×2 = 6; etc.)

You will use the following command blocks:

scratch

Exercise 7

The elf asks for a whole number then announces its opposite and starts again until 0 is answered.

You will use the following command blocks:

scratch

Exercise 8

The objective is to draw the computer's attention to the incremental construction of the requested algorithm.

How when faced with a problem considered 'too' complicated... it is possible to reduce the problem to a simpler and more accessible version, then through successive versions to achieve a resolution of the initial problem.

Drawing inspiration from the previous examples, build, test and validate the algorithms meeting the following need:

  • The elf asks for a number between 1 and 6
  • The elf counts from 1 to 5 if the requested number is between 1 and 5 and says an error message otherwise
  • Then the elf asks if we want to start again and if so, the sequence starts again forever until we say no.
  • The sprite asks for a number, counts or displays the message “invalid number”. Then he asks if he wants to start again and depending on the answer Y or N the sequence of operations repeats or the program stops.

For the first question:

scratch

Fourth question:

scratch

For the second question:

scratch

For the third question:

scratch

Exercise 9

To draw the square you will use the following command blocks:

square scratch

In another version, we want to draw a square at a position designated by the mouse (when the mouse is pressed...) and each trace will be done with a random color.

  • Define the draw square action as an independent 'draw a square' block with the following command blocks.

You will recognize the initialization part of the plot parameters as well as the plot part itself.

square scratch

  • Then create the plot script with the following blocks

square scratch

For the first version:

square scratch

For the second version:

square scratch

Exercise 10

Use the following command blocks to draw a circle:

scratch circle

Using this code as inspiration, draw a spiral.

Here is the code for the spiral, for the circle you should not increment the distance.

spiral scratch

spiral scratch

Exercise 11

Write the algorithm that will allow the Scratch elf to calculate the factorial of a number n that it will have
previously requested.

Then try your algorithm and check how it works.

Your test program must cover all possible cases: any number n as well as the
numbers n particular (it's up to you to look for them).

factorial scratch

For additional tests: negative number, zero number, non-integer number

Exercise 12

Write the algorithm that will allow Scratch to:
1. Ask for the two numbers a and b
2. Perform the Euclidean division of a by b; we note r the remainder of this division
3. a takes the value of b and b takes the value of r
4. we start again at line 2
5. when division gives a remainder equal to 0 the GCD is the last non-zero remainder
6. Then display the PGCD
Write your algorithm and test it for various pairs of values (a,b)