Thread: Replacing Morse to English

  1. #16
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    My eyes are bleeding from structure's drain-bamage.

    Global variables, a string compare worse than O(N), and an array overrun.


    > Comparing strings can be difficult...
    No it isn't. It's first chapter stuff in pretty much any C introduction.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  2. #17
    Registered User
    Join Date
    Sep 2019
    Posts
    11
    Ok, im looking at alot of this and honestly, im thoroughly confused, i think this is beyond the point i'm at right now, im grateful for all of you trying to help though

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Temphost View Post
    Ok, im looking at alot of this and honestly, im thoroughly confused, i think this is beyond the point i'm at right now, im grateful for all of you trying to help though
    Which part do you find confusing?

    The idea is to have two parallel arrays of strings such that the element at index i of one array corresponds to the element at index i of the other array, for all valid indices i. So one array stores the letters in Morse code form, the other array stores the letters in English form. Hence, given a letter in Morse code form, you search for it in the first array, thereby finding the index, and then use this index to access the letter in English form in the second array.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Registered User
    Join Date
    Sep 2019
    Posts
    11
    Quote Originally Posted by laserlight View Post
    Which part do you find confusing?
    Right now, c in general, i just started it and i don't know all the basics yet, the morse thing i'm asking about was an exam which my class had in may which we all struggled with, which im going over now again to try and use to help me learn c more

  5. #20
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Well, it's a pretty silly question for an exam. Here's an attempt written in a way I'd never write it in real life. You really wouldn't write it this way in real life and I find it kind of a dumb question for an exam.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    /* Bunch of morse code strings/sequences.
     */
    const char *morse[] = {
            ".-",        //morse code of letter A
            "-...",        //morse code of letter B
            "-.-." ,    //morse code of letter C
            "-.." ,        //morse code of letter D
            "." ,        //morse code of letter E
            "..-." ,    //morse code of letter F
            "--." ,        //morse code of letter G
            "...." ,    //morse code of letter H
            ".." ,        //morse code of letter I
            ".---" ,    //morse code of letter J
            "-.-" ,        //morse code of letter K
            ".-.." ,    //morse code of letter L
            "--" ,        //morse code of letter M
            "-." ,        //morse code of letter N
            "---" ,        //morse code of letter O
            ".--." ,    //morse code of letter P
            "--.-" ,    //morse code of letter Q
            ".-." ,        //morse code of letter R
            "..." ,        //morse code of letter S
            "-" ,        //morse code of letter T
            "..-" ,        //morse code of letter U
            "...-" ,    //morse code of letter V
            ".--" ,        //morse code of letter W
            "-..-" ,    //morse code of letter X
            "-.--" ,    //morse code of letter Y
            "--.."        //morse code of letter Z
    };
    
    /* We need this because the letters A through to Z are not guarenteed to be
     * continguious or ascending by the C standard. We'll use this string (array)
     * to find, or not, the character/letter that corresponds to the morse string in
     * the morse[] array. Note that the order of this array (english_chars) must be
     * the same order as for morse[]; i.e. an index of 1 in bothe morse[] and
     * english_chars[] corresponds to the index for 'B'
     */
    const char *english_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    
    /* Given the input string 'morse_str' try and find an index into the 'english_chars'
     * array that matches the input morse code. If there is no match found, -1 is
     * returned. If a match is found the index for the english character in
     * 'english_chars' is returned.
     *
     * Basically, search the morse[] array for the string 'str' and return an index
     * if there was a match and -1 if no match found. If anything other than -1 is
     * returned then the returned value can be used to look up the english letter in
     * the "parallel array" called (in this example) 'english_chars'
     */
    int morse_string_to_index(const char *morse_str)
    {
        const size_t number_of_letters = sizeof morse / sizeof morse[0];
        size_t i;
        int result;
        
        for (i = 0; i < number_of_letters ; i++) {
            if (strcmp(morse[i], morse_str) == 0) {    /* if there is a match */
                break;
            }
        }
        /* Test to see if we found a match. If we got to the end of the morse[]
         * array without any of the strings matching with morse_str then no match
         * was found and 'i' will be == number_of_letters. If a match was found then
         * 'i' will be < number_of_letters. If a match was found then return the
         * index ('i'), otherwise return -1 to indicate nothing found.
         */
        if (i < number_of_letters)
            result = i;        /* Found a match, we want to return that array index */
        else
            result = -1;    /* No match, return -1 */
        
        return result;
    }
    
    /* Match up 'index' to the character/letter in the english_chars string (array).
     * If there is no match then the '\0' (nul) character is returned 
     *
     * This is "mapping" the index of the first "parallel array" to the value in the
     * second (the English letter).
     */
    char morse_index_to_char(int index)
    {
        const size_t number_of_letters = sizeof morse / sizeof morse[0];
        char result;
        
        if (index >= 0 && index < number_of_letters)
            result = english_chars[index];
        else
            result = '\0';    /* to indicate an error, basically */
        
        return result;
    }
    
    /* Given a morse code string (sequence), try and find the corresponding English letter
     */
    char morse_to_english_letter(const char *morse)
    {
        int morse_string_index;
        
        morse_string_index = morse_string_to_index(morse);
        
        return morse_index_to_char(morse_string_index);
    }
    
    void print_morse_to_eng(const char *morse)
    {
        char ch;
            
        ch = morse_to_english_letter(morse);
        
        if (ch == '\0')
            printf("No morse sequence matched\n");
        else
            printf("Morse sequenece \"%s\" is the letter %c\n", morse, ch);
    }
    
    int main(void)
    {
        size_t i;
        
        /* Silly test... */
        for (i = 0; i < 26; i++) {
            print_morse_to_eng(morse[i]);
        }
        print_morse_to_eng(".-.-.-.-.-.-.....");
        
        return 0;
    }
    Edit: It's quite possible that if this was really an exam question that the professor doesn't realise that 'A' to 'Z' are not guaranteed to be contiguous or sequential and probably just wanted, basically, the code in morse_string_to_index()
    Last edited by Hodor; 09-17-2019 at 05:02 AM.

  6. #21
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338
    Try adding a break.
    Wow, an actual productive idea. I like it.
    "without goto we would be wtf'd"

  7. #22
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by Structure View Post
    Wow, an actual productive idea. I like it.
    Yeah, it's a crazy thing to do I know

  8. #23
    Registered User
    Join Date
    Sep 2019
    Posts
    11
    I can post the actual question given if you guys want it?

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Temphost View Post
    I can post the actual question given if you guys want it?
    Sure, but in the end it's up to you to program a solution if you want to learn.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #25
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308
    First, just because you know how to do something doesn't mean you just provide everyone else (who don't know how to do it) with an entire source code. If you ever have the time, which I believe you'll never have, go through the forum guidelines. You don't provide source codes. That's that. Doing so hinders the growth of people who come here to learn as by providing a solution you're basically inhibiting the thought process of the person who needs help. Instead try and help them build up on their logic not provide a darn code which is alien nature to a person new to C++ or any programming language in general. Read the messages sent by Salem, laserlight, john etc on the forum and try and learn how they help others if you really wanna help people here. Not everyone would know whatever you sent in the post as a solution. The guy/girl may not even know what a #define is and it's not his/her fault. If this isn't simple enough to understand then let the others help out. Trying to increase your post counts by posting gibberish isn't going to be of any help to anyone!

    Secondly, I don't think you might ever read anything written above cuz you'd probably be partying.

  11. #26
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by Zeus_ View Post
    Trying to increase your post counts by posting gibberish isn't going to be of any help to anyone!
    Oh, I dunno. Sometimes gibberish is good. E.g. if someone can be bothered understanding the code I wrote below -- i.e. how it works -- then maybe they'll learn something about data structures. Who knows though

    Code:
    #include <stdio.h>
    
    int morse_seq_to_char(const char *mseq)
    {
        static const char morsetree[] = " ETIANMSURWDKGOHVF L PJBXCYZQ  ";
        unsigned idx = 0;
            
        while (*mseq) {
            idx = 2 * idx + ((unsigned char)*mseq & 1) + 1;
            mseq++;
        }
        
        if (idx >= sizeof morsetree)
            return -1;
        
        return morsetree[idx] == ' ' ? -1 : morsetree[idx];
    }
    
    int main(void)
    {
        char buff[255];
        
        while (scanf("%s", buff) == 1) {    // Not a great way to read input... someone else can fix
            int ch = morse_seq_to_char(buff);
            if (ch != -1)
                putchar(ch);
            else
                puts("\n --- Unknown morse code sequence");
        }
        puts("");
        
        return 0;
    }
    Code:
    $ echo ".... --- -.. --- .-." | ./a.out
    HODOR
    Last edited by Hodor; 09-17-2019 at 09:35 PM. Reason: removed unused variable

  12. #27
    Registered User
    Join Date
    Sep 2019
    Posts
    11
    I am here to learn it, the question is in the past and even if i got a full working code reply it wouldnt get me anywhere with it, i just know i didnt do it well the first time and wanted to go back to it to try and understand how i should have

  13. #28
    Registered User
    Join Date
    Sep 2019
    Posts
    11
    Heres the actual question minus the image with the morse and english counterparts, if it helps make it any clearer or i missed details originally.

    C101 Home Assignment 2019


    Due Date: 2nd May 2019


    Description

    You are required to write a program that can convert Morse Code into English text. The program should take a string of ’-’ and ’.’ with a single space in between each full English letter and three spaces between each English word.
    For example
    ‘‘−− −.−− −. .− −− . .. … … . .− −.”
    Would decode as the English sentence “MY NAME IS SEAN”. Here is the Morse Code scheme for the alphabet.



    Instructions


    • Insert a string of Morse code directly into the program, i.e. do not use a scanf (as this can be very laborious).
    • The string can be up to 200 Morse characters long. e.g. the example above is 43 Morse characters long.
    • Output the corresponding English text.
    • Note that while you should leave your own test string in the program, I will change it to my own test strings to fully test the program.
    • If the text is too long you should only use the first 200 Morse characters.
    • If there is a mistake in the input text you should inform the user where the error is and what it is.

    It is up to you to consider what sort of mistakes could be made.
    Allocation of Marks


    • 20 marks for adherence to the (deleted college name) C Coding Standard, including commenting headers, naming, indentation.
    • 20 marks for being able to decode individual letters.
    • 20 marks for correctly separating full letters in a line of text.
    • 20 marks for correctly separating full words.
    • 20 marks for reporting errors.

    Note: This should be your own work and it should be treated similar to an exam. Your lecturer will clarify any questions you have about what you are being asked, but will not assist you with writing or fixing the code. While you can discuss with your class mates, what you submit must be your own work. Before you start you should draw on paper a flow chart of the system. This will assist you in getting the design of your program correct.
    Last edited by Temphost; 09-18-2019 at 03:41 AM. Reason: Accidental Thing added

  14. #29
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by Temphost View Post
    You are required to write a program that can convert Morse Code into English text. The program should take a string of ’-’ and ’.’ with a single space in between each full English letter and three spaces between each English word.
    For example
    ‘‘−− −.−− −. .− −− . .. … … . .− −.”
    Would decode as the English sentence “MY NAME IS SEAN”. Here is the Morse Code scheme for the alphabet.
    My 2nd program doesn't actually do exactly that. Assuming that the input text should be "-- -.-- -. .- -- . .. ... ... . .- -."" and not what's given, my 2nd program decodes it as:

    Code:
    $ echo "-- -.--  -. .- -- .  .. ...  ... . .- -." | ./a.out
    MYNAMEISSEAN

    Should be easy enough to adapt (to add the spaces) though

    Edit: Although, given the example input it seems kind of difficult to imagine how words can be differentiated (i.e. how does that input signify it should decode as "MY NAME" and not "MYNAME"?) That's probably the hardest part of the assignment, as given, and pretty much impossible if you ask me.
    Last edited by Hodor; 09-18-2019 at 03:53 AM.

  15. #30
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Maybe you find this more useful:

    Replacing Morse to English-morse1min-gif

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-04-2018, 08:16 AM
  2. Code Morse ,help...
    By Raskolnikov LM in forum C Programming
    Replies: 1
    Last Post: 01-05-2018, 05:24 PM
  3. Morse Code(Going the other way)
    By lilbo4231 in forum C Programming
    Replies: 21
    Last Post: 06-16-2011, 03:25 AM
  4. Implementing a English-Spanish/Spanish-English Dictionary
    By invertedMirrors in forum C Programming
    Replies: 4
    Last Post: 02-23-2008, 03:48 PM
  5. how convert english to morse code
    By uler in forum C++ Programming
    Replies: 2
    Last Post: 09-12-2001, 07:56 PM

Tags for this Thread