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

I need to improve this morse code enocder code I made. The program is only requi

ID: 3766579 • Letter: I

Question

I need to improve this morse code enocder code I made. The program is only required to correctly translate a text string consisting of ONLY letters ('a'~'z') and digits ('0'~'9'). But, the program needs to be robust --- if the program is provided with a text string and there are punctuation marks or spaces in the text string, the program must be able to print out an error message.

Thus, if the program is provided with a text string 'MATLAB', it will print out the Morse code of the text on ONE LINE (-- .- - .-.. .- -... ).  if the program is provided with a text string 'programming', it will print out the Morse code of the text on ONE LINE (.--. .-. --- --. .-. .- -- -- .. -. --.). If the program is provided with 'MATLAB!', your program prints out an error message (e.g. 'Input has invalid characters!'). It is OK if your program also print out the Morse code of the valid part (as shown below).

>> step2

The word to be encoded: MATLAB!

The Morse code is -- .- - .-.. .- -...

Input has invalid characters!

Please note that, your program must put the Morse code on one line and use a space to separate the Morse codes of two consecutive characters. For example, for a word 'AB', its Morse code is '.- -...' , since the Morse code of 'A' is '.-' and the Morse code of 'B' is '-...' . It is not correct to put '.-' and '-...' together without the space in between (i.e. '.--...') or to put them on different lines, i.e

the basic idea in this step is to put this part into a loop, so that this part of the code can be repeatedly executed to translate the characters one by one. You may use either a for loop or a while loop. The number of iterations is determined by the length of the text string.

If you want to use disp(), you may combine those strings into a bigger string, and use disp() to print out the new string.

str4=[ ]

str4=[str1 ' '];

str4=[str4 ' ' str2];

str4=[str4 ' ' str3];

disp(str4);

Here is my code:

MC_1='.----'; MC_2='..---'; MC_3='...--';

MC_4='....-'; MC_5='.....'; MC_6='-....';

MC_7='--...'; MC_8='---..'; MC_9='----.';

MC_0='-----'; MC_A='.-'; MC_B='-...';

MC_C='-.-.'; MC_D='-..'; MC_E='.';

MC_F='..-.'; MC_G='--.'; MC_H='....';

MC_I='..'; MC_J='.---'; MC_K='-.-';

MC_L='.-..'; MC_M='--'; MC_N='-.';

MC_O='---'; MC_P='.--.'; MC_Q='--.-';

MC_R='.-.'; MC_S='...'; MC_T='-';

MC_U='..-'; MC_V='...-'; MC_W='.--';

MC_X='-..-'; MC_Y='-.--'; MC_Z='--..';

Char=input('Enter character to encode here: ','s');
  
Char=upper(Char);

Valid = 1;

switch(Char)

case '1'

Code=MC_1;

case '2'

Code=MC_2;

case '3'

Code=MC_3;
  
case '4'

Code=MC_4;
  
case '5'

Code=MC_5;
  
case '6'

Code=MC_6;
  
case '7'

Code=MC_7;
  
case '8'
  
Code=MC_8;
  
case '9'

Code=MC_9;
  
case '0'

Code=MC_0;
  
  
case 'A'
  
Code=MC_A;
  
case 'B'
  
Code=MC_B;
  
case 'C'
  
Code=MC_C;
  
case 'D'
  
Code=MC_D;
  
case 'E'
  
Code=MC_E;
  
case 'F'
  
Code=MC_F;
  
case 'G'
  
Code=MC_G;
  
case 'H'
  
Code=MC_H;
  
case 'I'
  
Code=MC_I;
  
case 'J'
  
Code=MC_J;
  
case 'K'
  
Code=MC_K;
  
case 'L'
  
Code=MC_L;
  
case 'M'
  
Code=MC_M;
  
case 'N'
  
Code=MC_N;
  
case 'O'
  
Code=MC_O;
  
case 'P'
  
Code=MC_P;
  
case 'Q'
  
Code=MC_Q;
  
case 'R'
  
Code=MC_R;
  
case 'S'
  
Code=MC_S;
  
case 'T'
  
Code=MC_T;
  
case 'U'
  
Code=MC_U;
  
case 'V'
  
Code=MC_V;
  
case 'W'
  
Code=MC_W;
  
case 'X'
  
Code=MC_X;
  
case 'Y'
  
Code=MC_Y;
  
case 'Z'
  
Code=MC_Z;

otherwise

Valid =0;
end

if Valid

disp(Code);
else

disp('Error: Invalid Character(s)');

end

Please help me, thank you SO much!!

Explanation / Answer

<code>

#include <bits/stdc++.h>
using namespace std;

int main() {
   map<char,string>mint;
   mint.insert(pair<char,string>('0',"-----"));
   mint.insert(pair<char,string>('1',".----"));
   mint.insert(pair<char,string>('2',"..---"));
   mint.insert(pair<char,string>('3',"...--"));
   mint.insert(pair<char,string>('4',"....-"));
   mint.insert(pair<char,string>('5',"....."));
   mint.insert(pair<char,string>('6',"-...."));
   mint.insert(pair<char,string>('7',"--..."));
   mint.insert(pair<char,string>('8',"---.."));
   mint.insert(pair<char,string>('9',"----."));
   mint.insert(pair<char,string>('A',".-"));
   mint.insert(pair<char,string>('B',"-..."));
   mint.insert(pair<char,string>('C',"-.-."));
   mint.insert(pair<char,string>('D',"-.."));
   mint.insert(pair<char,string>('E',"."));
   mint.insert(pair<char,string>('F',"..-."));
   mint.insert(pair<char,string>('G',"--."));
   mint.insert(pair<char,string>('H',"...."));
   mint.insert(pair<char,string>('I',".."));
   mint.insert(pair<char,string>('J',".---"));
   mint.insert(pair<char,string>('K',"-.-"));
   mint.insert(pair<char,string>('L',".-.."));
   mint.insert(pair<char,string>('M',"--"));
   mint.insert(pair<char,string>('N',"-."));
   mint.insert(pair<char,string>('O',"---"));
   mint.insert(pair<char,string>('P',".--."));
   mint.insert(pair<char,string>('Q',"--.-"));
   mint.insert(pair<char,string>('R',".-."));
   mint.insert(pair<char,string>('S',"..."));
   mint.insert(pair<char,string>('T',"-"));
   mint.insert(pair<char,string>('U',"..-"));
   mint.insert(pair<char,string>('V',"...-"));
   mint.insert(pair<char,string>('W',"..--"));
   mint.insert(pair<char,string>('X',"-..-"));
   mint.insert(pair<char,string>('Y',"-.--"));
   mint.insert(pair<char,string>('Z',"--.."));
   cout<<"Enter the string:";
   string str;
   cin >> str;
   string code = "";
   bool valid = true;
   for(int i = 0;i < str.size();i++){
if (isalpha(str[i])){
str[i] = toupper(str[i]);
}else if(isdigit(str[i])){

}else{
valid = false;
break;
}
   }
   cout<<"The word to be encoded:"<<str<<endl;
   if (valid){
for(int i = 0;i < str.size();i++){
if (i == 0){
code += mint[str[i]];
}else{
code = code + " "+ mint[str[i]];
}
}
cout<<"The Morse code is:"<<code<<endl;
   }else{
cout<<"Input has invalid characters! ";
   }
   return 0;
}

</code>