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 -> Snowy
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 Snowy
The input sequence can be divided into 110, 0, 10, and 111, corresponding to Rainy, Fine, Cloudy, and Snowy.
Example 2: Incomplete Bit Pattern
Enter a sequence of bits: 11011
Invalid
The first three bits (110) represent Rainy, but the remaining 11 do not form a complete bit pattern. Therefore, the entire input is invalid. Your program must output only Invalid, not Rainy Invalid.
Example 3: Invalid Character
Enter a sequence of bits: 1102
Invalid
The input contains 2, which is not a valid binary digit. Therefore, the entire input is invalid.
Important: If an input is invalid, your program must output only Invalid, even if part of the input can be decoded successfully.
Requirements
More input-output samples.
Your solution should be based on the code template.
The input is a nonempty string without spaces. The input may be valid or invalid.
An input is considered invalid if:
It contains any character other than 0 or 1.
It cannot be completely decoded using the four bit patterns listed above.
If the input is valid, your program must output the corresponding weather messages in the correct order.
If the input is invalid, your program must output exactly Invalid. Do not output any partially decoded messages.
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, without leading or trailing spaces.
Print exactly Invalid (case-sensitive) for invalid inputs.
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.
Important: Your program will receive zero points if any runtime error occurs, including an out-of-range string index access.
Hints and Suggestions
Suggested Developement Strategy
We recommend solving this assignment in two stages:
Stage 1: Handle valid inputs first. Assume that the input is valid and focus on correctly decoding the bit sequence into weather messages. Pay particular attention to updating the index i.
Stage 2: Handle invalid inputs. Once your program works correctly for valid inputs, extend it to detect invalid characters and incomplete bit patterns.
Remember that if any part of the input is invalid, your program must output only Invalid, without any partially decoded messages. You may need to reconsider how your program stores and outputs decoded messages.
Accessign Individual Characters
To check whether an input bit is 0, you can use: code[i] == '0'
Remember that code[i] is a character, so you should compare it with '0' or '1', not the integers 0 or 1.
Updating the index
Different messages are arepresented by different numbers of bits. Make sure that you update the index i correctly after decoding each message.
Avoiding out-of-range string index access.
Before accessing a character in a string, make sure that its index is within the valid range.
The condition of the provided while loop ensures that code[i] is within the valid range, provided that i remains non-negative. However, it does not guarantee that code[i+1] or code[i+2] is valid.
For example, you can safely check code[i+1] using:
if (i+1 < static_cast<int>(code.length()) && code[i+1] == '0')
{ // Your code here
}
In C++, the && operator uses short-circuit evaluation. If the first condition is false, the second condition will not be evaluated. This prevents an invalid character access in the example above. Similarly, before accessing code[i+2], make sure that the corresponding index is valid.
Concatenating Strings:
In C++, you can use + or += to combine strings:
string result = "Hello";
result += " ";
result += "World";
cout << result; // Hello World
Hint: You can use string concatenation to store decoded messages before printing them, so that invalid inputs do not produce partially decoded output.
Additional Reading (Optional)
If you are interested in the ideas behind this assignment, including how information can be encoded efficiently, you can read Demystify Entropy.