Category Archives: Teaching ideas

Using a FUNCTION in Scratch

A function is basically a series of steps that you teach the computer – it then learns the steps and can use them when you ‘call it’. The idea is you don’t need to continually tell the computer how to do something as it already knows.

In this case, we are going to teach the computer how to work out the percentage of a number.  The steps are:

  1. Take the number and divide it by 100
  2. Multiply that answer by the percentage you want
  3. Print out that answer

In Scratch, you can do this using the MAKE BLOCK function. You define the block and put in how many numbers you need. In the function, you define the steps and return the answer.

You then ‘call the function’ by adding the block to the code.

function

Using REPETITION in Scratch

REPETITION is a very useful function on coding and saves time rather than doing a long list of code.  The basic idea is that you can have a variable and then write code so the variable increases by 1 (or another number) until a certain number is reached.

Example: To get Scratch to work out the 5x table

Using pseudocode:

  1. Declare a variable called MULTIPLIER
  2. Make MULTIPLIER = 0
  3. Work out 5 x MULTIPLIER
  4. PRINT the answer
  5. Add 1 to MULTIPLIER
  6. Repeat steps 3 – 5
  7. STOP the code when MULTIPLIER = 12

This is how the code looks like in Scratch.

repetition

 

 

A search algorithm you can act out in primary school

Imagine you’ve got100 children all lined up. You want to know if someone is called Sarah and what position they are.

There’s a few ways you can do this – what you can’t do is ask anyone.

1)Get them in a line. Ask each child their name.

IF Name = Sarah, then check position and say that Sarah is at that position. Keep going until you reach the end.

OR

2) Sort the children into alphabetical order (IMPORTANT)

Find the middle child. IF their name is before Sarah, then get rid of the first half. Or the second half if after Sarah.

Then find the middle child again. Repeat the process as above.

By halving, you will find the Sarah.

This can be adapted into a guessing game.

Imagine a number between 0 – 100. Computer guess 50. If Higher, guesses 75 etc.

Worked example.

Number is 57

Guess 50 (HIGHER) Range left 51 – 100

Guess 75 (LOWER) Range left 51 – 74

Guess 63 (LOWER) Range left 51 – 62

Guess 56 (HIGHER) Range left 57 – 62

Guess 59 (LOWER) Range left  57 – 59

Guess 58 (LOWER) Range left 57 – 57

Guess 57. CORRECT

This is called a DIVIDE and CONQUER algorithm. It’s designed to cut down the work. An alternative could be to go through from 0 – 100 and get the answer HIGHER or LOWER. It would work but it would be slow.

You could do RANDOM guesses.