Task 1: Encode and Decode
(a) [20%] void encode(int key, char plain[], int length, char encoded[]);
This function implements the encoding scheme described earlier, using the key passed as the first
argument. It steps through the array plain[] from index 0 to index length–1, encodes each character
and places the resulting value in the corresponding element of encoded[]. The array plain[] is not
changed.
Examples:
(i)
Breaking the code
Tasks
char message[2] = {'H', 'i'};
char encoded_message[2];
encode(1, message, 2, encoded_message);
for (int i = 0; i < 2; ++i)
{
cout << encoded_message[i];
}
cout << endl;
(b) [20%] voiddecode(intkey,charencoded[],intlength,charplain[]);
This function decodes characters back to plaintext. The key passed as the first argument is the key
used to perform the original encoding. The function steps through array encoded[] from index 0 to
index length–1, decodes each character and places the resulting value in the corresponding element
of plain[]. The array encoded[] is not changed.
Examples:
(i)
Produces the output:
(ii)
char message[2] = {'H', 'i'};
char encoded_message[2];
encode(90, message, 2, encoded_message);
for (int i = 0; i < 2; ++i)
{
cout << encoded_message[i];
}
cout << endl;
Ij
"C
char message[2];
char encoded_message[2] = {'I', 'j'};
decode(1, encoded_message, 2, message);
for (int i = 0; i < 2; ++i)
{
cout << message[i];
}
cout << endl;
Hi
May I ask how i can design a function to encode and decode it? Thanks a lot!