Write a program that converts a sequence of bits into weather messages. This process is called decoding. There are four possible bit patterns, each representing a different weather condition:
0 -> Fine
10 -> Cloudy
110 -> Rainy
111 -> Snow
Your program should decode the input bit sequence into the corresponding messages, in the same order.
Example 1 (click to see the figure):
Enter a sequence of bits: 110010111
Rainy Fine Cloudy Snow
The input sequence can be divided into 110, 0, 10, and 111, corresponding to Rainy, Fine, Cloudy, and Snowy.
Requirements
Your solution should be based on the code template.
You may assume that every input is a valid, nonempty sequence formed by concatenating the four bit patterns listed above.
Your program must follow the output format shown in Example 1.
Messages are case-sensitive. Use exactly Fine, Cloudy, Rainy, and Snowy.
Separate consecutive messages with a single space.
Do not print any unnecessary characters or debugging information.
Do not add \n or endl statements inside the decoding loop. Keep the existing cout << endl; at the end of the template.
Do not modify the existing cout statements in the code template.
Use cin >> code; as provided in the template. Do not replace it with getline(cin, code);, since the current grading script explicitly rejects submissions containing getline.
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'
Hints and 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.