Binary representation of any decimal number 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 any decimal number as the input and plays the musical notes representing that number in binary (high notes for dots showing and low notes for dots not showing).

Testing examples:

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

Input Output
255 high
high
high
high
high
high
high
high
100 high
high
low
low
high
low
low
1 high

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:] and wait
play note (72 v) for (0.5) beats

play note (48 v) for (0.5) beats
repeat until <(decimal number) < (bit value)>
end

repeat until <(bit value) = [1]>
end

if <<(decimal number) > (bit value)> or <(decimal number) = (bit value)>> then
else
end
set [decimal number v] to (answer)

set [bit value v] to [1]

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

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 ‘1’. Find the smallest bit value which is larger than the “decimal number” by doubling value of “bit value” until it is bigger than the “decimal number”.
  • Set the variable “bit value” to 1 and find the smallest “bit value” which is larger than “decimal number” by multiplying “bit value” by 2 (Use the () * () block under “Operators” to multiply the “bit value” by 2) until it is larger than “decimal number”. You can do this by using a repeat until <> loop.

  • 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.

Show Scratch solution

Python

Extra Challenge

Modify your program so it would play a low note when the user enters the number zero as the input.