Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can you please verify its correct when you finish because I\'ve posted this mult

ID: 3788850 • Letter: C

Question

Can you please verify its correct when you finish because I've posted this multiple times but I've been getting wrong answers from other people who try it. This is one question. (PLEASE USE C++ CODE) Thank You

In this assignment, you are going to write a program to encode messages using codes such as Morse code. This programs will use the class Code. The program will use the class to encode a message. The Morse code table is given below :

Our software will support codes that consist of symbols followed by a blank space. For example, in a message if an A appears, it will be replace by ".- ". The trailing space indicates the end of the codeword for that character. (NOTE: We will only be using the characters A-Z. Morse code words are separated by a space, so the message "cat" would be -.-. .- - x where x indicates the end of message. Note that spaces between words are denoted by 7 dots ".......". You can assume that encoded messages end with a lowercase x (which means STOP). When converting a message from text to code, a period "." should be converted to an x.)

Requirements

Your program should include the following class :

class Code

{

public:

Code(); // Default constructor - loads and uses morse code

Code(vector codewords); // constructor loading customized code

string encode(vector message); // encodes a message consisting of A-Z

string decode(vector message); // decodes a message

private:

vector codewords; // this is a codeword vector parallel to A-Z

vector alpha; // this is the vector A-Z

vector alphacode(); // function builds the vector alpha - A B C etc.

vector morsecode(); // function the vector codewords containing morse code

string encode(char x); //returns the codeword for the character x

char decode(string c); //returns the character for the codeword c.

};

The encode will input an alphabetic message from a file, and use the class to build the morse code string, and then output the string. You can download the functions morsecode and alphacode.

International Morse Code 1. A dash is equal to three dots 2. The space between parts of the same letter is equal to one dot. 3. The space between two letters is equal to three dots 4. The space between two words is equal to seven dots.

Explanation / Answer

Given below is the c++ code for the question. A main() function is written to demonstrate the implementation. The output of main is in the end. Please do rate the answer it it helped. Thank you.

#include <vector>
#include <iostream>
using namespace std;
class Code

{
public:
Code(); // Default constructor - loads and uses morse code
Code(vector<string> codewords); // constructor loading customized code
string encode(vector<string> message); // encodes a message consisting of A-Z
string decode(vector<string> message); // decodes a message
  
private:
  
vector<string> codewords; // this is a codeword vector parallel to A-Z
vector<char> alpha; // this is the vector A-Z
vector<char> alphacode(); // function builds the vector alpha - A B C etc.
vector<string> morsecode(); // function the vector codewords containing morse code
string encode(char x); //returns the codeword for the character x
char decode(string c); //returns the character for the codeword c.
  
};


Code::Code() // Default constructor - loads and uses morse code
{
this->alpha = alphacode();
this->codewords = morsecode();
}

Code::Code(vector<string> codewords) // constructor loading customized code
{
this->alpha = alphacode();
this->codewords = codewords;
}
string Code::encode(vector<string> message) // encodes a message consisting of A-Z
{
string encodedStr = "";
for(int i = 0; i < message.size(); i++)
{
string word = message[i];
  
if(i != 0)
encodedStr = encodedStr + "....... ";
  
for(int j = 0; j < word.size(); j++)
{
if(word[j] == '.')
encodedStr = encodedStr + "x";
else if(word[j] == ' ')
encodedStr = encodedStr + ".......";
else if(word[j] >= 'a' && word[j] <= 'z')
{
encodedStr = encodedStr + encode( word[j] - 'a' + 'A');
}
else if(word[j] >= 'A' && word[j] <= 'Z')
{
encodedStr = encodedStr + encode( word[j]);
}

encodedStr = encodedStr + " ";
}
  
  
}
return encodedStr;
}
string Code::decode(vector<string> message) // decodes a message
{
string decodedStr = "";
for(int i = 0; i < message.size(); i++)
{
string word = message[i];
if(word == "x")
decodedStr = decodedStr + ".";
else if(word == ".......")
decodedStr = decodedStr + " ";
else
decodedStr = decodedStr + decode(word);
}
return decodedStr;
}
vector<char> Code::alphacode() // function builds the vector alpha - A B C etc.
{
vector<char> alphabet;
for(int letter = 'A'; letter <= 'Z' ; letter++)
alphabet.push_back(letter);
return alphabet;
}
vector<string> Code::morsecode() // function the vector codewords containing morse code
{
vector<string> morsecodes;
morsecodes.push_back(".-"); //A
morsecodes.push_back("-..."); //B
morsecodes.push_back("-.-.");//C
morsecodes.push_back("-..");//D
morsecodes.push_back(".");//E
morsecodes.push_back("..-.");//F
morsecodes.push_back("--.");//G
morsecodes.push_back("....");//H
morsecodes.push_back("..");//I
morsecodes.push_back(".---");//J
morsecodes.push_back("-.-");//K
morsecodes.push_back(".-..");//L
morsecodes.push_back("--");//M
morsecodes.push_back("-.");//N
morsecodes.push_back("---");//O
morsecodes.push_back(".--.");//P
morsecodes.push_back("--.-");//Q
morsecodes.push_back(".-.");//R
morsecodes.push_back("...");//S
morsecodes.push_back("-");//T
morsecodes.push_back("..-");//U
morsecodes.push_back("...-");//V
morsecodes.push_back(".--");//W
morsecodes.push_back("-..-");//X
morsecodes.push_back("-.--");//Y
morsecodes.push_back("--..");//Z
return morsecodes;
  
}
string Code::encode(char x) //returns the codeword for the character x
{
int idx = x - 'A';
return codewords[idx];
}
char Code::decode(string c) //returns the character for the codeword c.
{
for(int i = 0; i < codewords.size(); i++)
{
if(codewords[i] == c)
return alpha[i];
}
return '0'; //error
}


template <typename T>
void print(vector<T> v)
{
for(int i = 0; i < v.size(); i++)
cout << v[i] << " ";
  
}

int main()
{
Code code;
string w[] = {"cat", "ate", "rat"};
vector<string> message;
for(int i = 0; i < 3; i++)
message.push_back(w[i]);
  
cout << "encoding:[" ;
print(message);
cout << "]" << endl;
  
cout << "encoded string is:[";
string enc = code.encode(message);
cout << enc << "]" << endl;
  
//-.-. .- - ....... .- - . ....... .-. .- -
message.clear();
message.push_back("-.-.");
message.push_back(".-");
message.push_back("-");
message.push_back(".......");
message.push_back(".-");
message.push_back("-");
message.push_back(".");
message.push_back(".......");
message.push_back(".-.");
message.push_back(".-");
message.push_back("-");
  
cout << "decoding:[" ;
print(message);
cout << "]" << endl;
  
cout << "decoded string is:[" << code.decode(message) << "]" << endl;
return 0;
}

output

encoding:[cat ate rat ]

encoded string is:[-.-. .- - ....... .- - . ....... .-. .- - ]

decoding:[-.-. .- - ....... .- - . ....... .-. .- - ]

decoded string is:[CAT ATE RAT]

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote