Assignment 3: Decoder
Write a program that converts a sequence of bits into messages (this process is known as decoding). There are four types of bit encoding: 0, 10, 110, 111. The corresponding message for each bit encoding is shown below.
0 -> Fine
10 -> Cloudy
110 -> Rainy
111 -> Snow
Example 1 (click to see the figure):
Enter a sequence of bits: 110010111
Rainy Fine Cloudy Snow
Your solution should be based on the code template.
You can assume that all the input sequences are legal.
Your program must conform the following requirements:
The outputs of your program should behave as in Example 1.
program input: 110010111
program output: Rainy Fine Cloudy Snow
Each message (Fine, Rainy, Cloudy, Snow) is case sensitive and is separated by one space.
Do not output any \n or endl.
Do not modify the cout statements in the code template.
Use cin >> code;
Do not use getline(cin, code); otherwise your assignment will not be processed.
This is due to the limitation of the evaluating program.
More input-output samples.
If you are interested in the story behind this assignment, you can read Demystify Entropy.
To check whether an input bit is 0, you can use code[i] == '0'
Suggestions
When you wan to access code[i], you can add the following to two assertions before your statement to ensure that index i is within the valid range.
assert(i >= 0);
assert(i < code.length());
cout << code[i] << endl;
Similarly, if you want to access code[i+1], you can add the following two assertions before your statement to ensure that index i+1 is within the valid range.
assert(i+1 >= 0);
assert(i+1 < code.length());
cout << code[i+1] << endl;
If the array index is not within the valid range, your program will cause runtime error or unknown results.
The accuracy of your program is ZERO if any runtime error occurs.