Morse code

Morse code

Translating dots and dashes.

Morse code is a method used in telecommunication to encode text characters as sequences of two different signal durations, called dots and dashes, or dits and dahs. In the 1890s, Morse code began to be used extensively for early radio communication before it was possible to transmit voice. In the late 19th and early 20th centuries, most high-speed international communication used Morse code on telegraph lines, undersea cables, and radio circuits. Today, Morse code has been replaced by the Global Maritime Distress and Safety System (GMDSS). This program translates Morse code in a file into plain text.

Make


Write a program that reads a file containing Morse code with dots represented with a full stop: ".", dashes represented with hyphens: "-" and words separated with a space and forward slash: " / ". The program should output the contents of the file in English.

Use this boilerplate code as a starting point:

Success Criteria

Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.

Complete the subprogram called `read_morse` that:

  1. Takes a parameter that is the filename containing the Morse code. Note the files message1.txt and message2.txt are included in the Trinket above for you to use as source data.
  2. Checks if the file exists. If not, it returns "error: file not found"
  3. Reads the data in the file.
  4. Returns the message contained in the file in plain text by calling `translate` for each character.

Complete the `main program` so that:

  1. The user can input the name of the file.
  2. The `read_morse` function is called.
  3. The plain text message is output.

Typical inputs and outputs from the program would be:

For a file containing:

••• •- — ••- • •-•• / — — •-• ••• • / •– •- ••• / •- -• / •- — • •-• •• -•-• •- -• / •–• •- •• -• – • •-• / •- -• -•• / •• -• •••- • -• – — •-• / •– •••• — / •• ••• / -••• • ••• – / •-• • — • — -••• • •-• • -•• / – — -•• •- -•– / ••-• — •-• / •••• •• ••• / •• -• •••- • -• – •• — -• / — ••-• / – •••• • / ••• •• -• –• •-•• • / •– •• •-• • / – • •-•• • –• •-• •- •–• •••• / ••• -•– ••• – • — / •- -• -•• / — — •-• ••• • / -•-• — -•• •

The output would be:

SAMUEL MORSE WAS AN AMERICAN PAINTER AND INVENTOR WHO IS BEST REMEMBERED TODAY FOR HIS INVENTION OF THE SINGLE WIRE TELEGRAPH SYSTEM AND MORSE CODE

Note: use the files: message1.txt and message2.txt in the Trinket, do not copy the morse code above into your program.

Knowledge Organiser

Use these resources as a reference to help you meet the success criteria.

Programming guide:

Evaluate


Run the unit tests below to check that your program has met the success criteria.

Enter the filename: message1.txt

THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG

Enter the filename: message2.txt

SAMUEL MORSE WAS AN AMERICAN PAINTER AND INVENTOR WHO IS BEST REMEMBERED TODAY FOR HIS INVENTION OF THE SINGLE WIRE TELEGRAPH SYSTEM AND MORSE CODE

Check that you have:

  • Used comments within the code to describe the purpose of subprograms, conditions and iterations.
  • Used meaningful identifier names. That means the names of subprograms and variables indicate what they are for.