Binary representation of a decimal number between 0 and 31 by playing musical notes

Challenge Level: Ready to expand

Learning outcomes

Students will be able to:

Requirement:

Write a program that asks the user to enter a decimal number between 0 and 31 as the input and plays the 5 musical notes representing that number in binary (a high note for dots showing and a low note for dots not showing).

Testing examples:

Your program should display the outputs shown in this table for the given inputs provided:

Input Output
11 low
high
low
high
high
31 high
high
high
high
high
0 low
low
low
low
low

Languages

Scratch

What it should look like

Click on the green flag, enter the inputs provided in the “testing examples” to see the expected output of your program.

Recommended blocks
when green flag clicked

ask [Please enter a decimal number between 0 and 31:] and wait
repeat until <(bit value) = [1]>
end

if <<(decimal number) > (bit value)> or <(decimal number) = (bit value)>> then
else
end
play note (72 v) for (0.5) beats

play note (48 v) for (0.5) beats
set [decimal number v] to (answer)

set [bit value v] to [32]

set [bit value v] to ((bit value) / (2))

set [decimal number v] to ((decimal number) - (bit value))
Hints
  • Make variables called:

    • “decimal number” and set its value to the input number given by the end user.
    • “bit value” and set its value to ‘32’.
  • Now divide the “bit value” by 2 and check if “decimal number” is greater than or equal to “bit value”. If it is, play a high note and subtract “bit value” from the “decimal number”. If not, play a low note. Repeat until “bit value” is equal to 1.

  • To play a musical note use the block play note (60 v) for (0.5) beats beats under the “Sound”. Type in the number “72” for a high note and “48” for a low note.

  • Test your program with some values on the boundaries (for example test it with numbers 0 and 31).

Show Scratch solution

Python