0

I have a function that translates text entered by the user (it also translates numbers) to Morse code, however I do not understand why the array that contains the alphabet in morse code: m [37] [10] is a two-dimensional array? I understand that the 37 is by the amount of letters, numbers and white space, make a total of 37, but why the 10?

Here is my code:

void textoMorse(){
    int i,j;
    char texto[37] = {
        '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', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ' '
     };
    char m[37][10]={
        {".-,"}, {"-...,"}, {"-.-.,"}, {"-..,"}, {".,"}, {"..-.,"},
        {"--.,"}, {"....,"}, {"..,"}, {".---,"}, {"-.-,"}, {".-..,"},
        {"--,"}, {"-.,"}, {"---,"}, {".--.,"}, {"--.-,"}, {".-.,"}, 
        {"...,"}, {"-,"}, {"..-,"}, {"...-,"}, {".--,"}, {"-..-,"},
        {"-.--,"}, {"--..,"}, {".----,"}, {"..---,"}, {"...--,"}, 
        {"....-,"}, {".....,"}, {"-....,"}, {"--...,"}, {"---..,"}, 
        {"---.,"}, {"-----,"}, {"//"}
    };
    char frase[1000];
    gets(frase);
    fflush(stdin);
    for(i=0; frase!='\0'; i++){
        for(j=0; j<37; j++){
            if(frase[i] == texto[j]){
                printf("%s",m[j]);
            }
        }
    }
}
Eli Korvigo
  • 8,820
  • 6
  • 42
  • 67
Erick Pro
  • 31
  • 5
  • 9
    That is because the Morse code symbols are more than 1 character long. You need to make a space for them, and 10 is the worst case scenario for Morse code. – VladP Dec 27 '17 at 18:19
  • 4
    Please for the love of God format your code – ubadub Dec 27 '17 at 18:20
  • 2
    Aside: I wonder why each Morse code needs a terminating comma, when each contains a terminating nul `'\0'`. – Weather Vane Dec 27 '17 at 18:49
  • 2
    There are some problems with the code: the [`gets` function is so dangerous](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used]) that it is now obsolete, and `fflush(stdin)` is [non-standard](https://stackoverflow.com/questions/2979209/using-fflushstdin), and your loop control `for(i=0; frase!='\0'; i++)` should be `for(i=0; frase[i]!='\0'; i++)`. – Weather Vane Dec 27 '17 at 18:56
  • `char *m[37] = { ".-,", "-...,", ... };` would have been safer and less space expensive (and the trailing `,` in each code should not be there). – Breaking not so bad Dec 28 '17 at 03:57
  • `-... - .-- // - .... . // ..-. ..- -. -.-. - .. --- -. // -.. --- . ... // -. --- - // .-- --- .-. -.-` – Breaking not so bad Dec 28 '17 at 04:05

1 Answers1

2

m is a two-dimensional array. Think of it as having 37 rows, each with 10 characters. A C string is encoded in each row and implicitly terminated by a \0 character you don't see. For instance, the {".....,"} row contains 5 '.' characters, 1 '.' character, and a '\0' terminating character. The remaining 3 bytes in that row are filled in as '\0' characters by default.

NovaDenizen
  • 4,555
  • 12
  • 24