At this code, why is used char MORSE[40][7], \"two dimensional array\"? and How
ID: 3635711 • Letter: A
Question
At this code, why is used char MORSE[40][7], "two dimensional array"?and How is it doing or work for the conversion to morse code at this part of code, I need more explanation....
while(in[i]!='')
//repeats the "for loop" 40 times because the are 40 characters in EnglishAlphabet array
{for(j=0;j<40;j++)
//compares the each character with "in" array to "EnglishAlphabet" array
{if(toupper(in[i])==EnglishAlphabet[j])
//if true enter to the loop
{for(k=0;k<strlen(MORSE[j]);k++)
//display the Morse code for the given string
cout<<MORSE[j][k];
cout<<" "; //space
}
}
i++; //update i, adding 1
}
-------------------------------------------------------------------------------------------------------
THIS IS THE CODE
#include <iostream>
#include <string> //needs to use the string functions like toUpper, gets
#include<cctype> //use for character arrays
using namespace std;
//FUNCTION: main
int main()
{
//create an array MORSE with 40 rows and 7 columns
//it contains the morse codes for alphabets and numbers
char MORSE[40][7]={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---",
"-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-",
"..-","...-",".--","-..-","-.--","--..",".----","..---","...--",
"....-",".....","-....","--...","---..","----.","-----"," ","--..--",".-.-.-","..--.."};
////declare a charcter array "in" with 100 size
char in[100];
//character array, stores all the alphabets and numbers run 0 to 9, and symbols
char EnglishAlphabet[40]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X','Y','Z','1','2','3','4','5','6',
'7','8','9','0',' ',',','.','?'};
//variables for the iterators
int i=0,j,k;
cout<<"Enter a string: ";
//read the string into array "in"
gets(in);
cout<<endl;
//loop repeats till the last character in the string "in"
while(in[i]!='')
//repeats the "for loop" 40 times because the are 40 characters in EnglishAlphabet array
{for(j=0;j<40;j++)
//compares the each character with "in" array to "EnglishAlphabet" array
{if(toupper(in[i])==EnglishAlphabet[j])
//if true enter to the loop
{for(k=0;k<strlen(MORSE[j]);k++)
//display the Morse code for the given string
cout<<MORSE[j][k];
cout<<" "; //space
}
}
i++; //updare i, adding 1
}
cout<<endl<<endl;
system("pause");
return 0;
}
At this code, why is used char MORSE[40][7], "two dimensional array"?
and How is it doing or work, i need more explanation....
while(in[i]!='')
//repeats the "for loop" 40 times because the are 40 characters in EnglishAlphabet array
{for(j=0;j<40;j++)
//compares the each character with "in" array to "EnglishAlphabet" array
{if(toupper(in[i])==EnglishAlphabet[j])
//if true enter to the loop
{for(k=0;k<strlen(MORSE[j]);k++)
//display the Morse code for the given string
cout<<MORSE[j][k];
cout<<" "; //space
}
}
i++; //update i, adding 1
}
Explanation / Answer
As per your comments this is my answer Since there are total 40 characters size is taken as 40 and length of each character is atmost 7 therefore morse code array is declared as MORSE[40][7]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.