Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Array Location? (C Programming)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 NUCLEAR RABBIT   Members   -  Reputation: 254

Like
0Likes
Like

Posted 23 April 2014 - 01:22 PM

Hello, I am writing a program that takes a sentence and tranforms that sentence into the morse code equivalent. Problem is, when I print the text after the convertion, all the letters after the 1st letter are the same morse code symbol, instead of the correct letter equivalent :/ I been looking over my code trying to see why this is happening, but I cannot see what the problem can be! Can anyone please help me see what I am doing wrong? I'd surely appreciate any help! ohmy.png

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define LETTERS 26
#define NUMBERS 10

void convertToLower(char * sPtr);

int main(int argc, const char * argv[])
{
    // Morse Code 0-9
    //char * num_codes[] = {"-----", "----.", "---..", "--...", "-....", ".....", "....-", "...--", "..---", ".----"};
    // Morse Code A-Z
    char * alp_codes[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
    // A-Z
    const char searchKey[] = {'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'};
    
    // string variables & attributes
    char random_text[] = "abcd";
    char * txtPtr = random_text;
    int text_size = sizeof(random_text)/sizeof(random_text[0]);
    char * morse_code[text_size];
    int alp_index;
    
    convertToLower(random_text);
    
    // seaches for the searchKey in the txt and
    // converts that character of random_txt to the alp_code
    for(int h = 0; h < text_size; h++)
    {
        for(int i = 0; i < LETTERS; i++)
        {
            txtPtr = strchr(txtPtr, searchKey[i]);
            alp_index = (int)(txtPtr - random_text);
            
            if(txtPtr != NULL)
            {
                txtPtr++;
                morse_code[h] = alp_codes[alp_index];
                break;
            }
        
            txtPtr = random_text;
        }
    }
    
    // prints morse code
    for(int i = 0; i < text_size-1; i++)
    {
        printf("%s | ", morse_code[i]);
    }
    
    return 0;
}

//----------------------------------------------------------------------------------------
//  FUNCTION DEFINITIONS

void convertToLower(char *sPtr)
{
    while(*sPtr != '\0')
    {
        *sPtr = tolower(*sPtr);
        *sPtr++;
    }
}

Edited by NUCLEAR RABBIT, 23 April 2014 - 01:50 PM.

------------------------My band: RISE OVER ME!

Sponsor:

#2 NUCLEAR RABBIT   Members   -  Reputation: 254

Like
0Likes
Like

Posted 23 April 2014 - 01:47 PM

 

Hello, I am writing a program that takes a sentence and tranforms that sentence into the morse code equivalent. Problem is, when I print the text after the convertion, all the letters after the 1st letter are the same morse code symbol, instead of the correct letter equivalent :/ I been looking over my code trying to see why this is happening, but I cannot see what the problem can be! Can anyone please help me see what I am doing wrong? I'd surely appreciate any help! ohmy.png

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define LETTERS 26
#define NUMBERS 10

void convertToLower(char * sPtr);

int main(int argc, const char * argv[])
{
    // Morse Code 0-9
    //char * num_codes[] = {"-----", "----.", "---..", "--...", "-....", ".....", "....-", "...--", "..---", ".----"};
    // Morse Code A-Z
    char * alp_codes[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
    // A-Z
    const char searchKey[] = {'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'};
    
    // string variables & attributes
    char random_text[] = "abcd";
    char * txtPtr = random_text;
    int text_size = sizeof(random_text)/sizeof(random_text[0]);
    char * morse_code[text_size];
    int alp_index;
    
    convertToLower(random_text);
    
    // seaches for the searchKey in the txt and
    // converts that character of random_txt to the alp_code
    for(int h = 0; h < text_size; h++)
    {
        for(int i = 0; i < LETTERS; i++)
        {
            txtPtr = strchr(txtPtr, searchKey[i]);
            alp_index = (int)(txtPtr - random_text);
            
            if(txtPtr != NULL)
            {
                txtPtr++;
                morse_code[h] = alp_codes[alp_index];
                break;
            }
        
            txtPtr = random_text;
        }
    }
    
    // prints morse code
    for(int i = 0; i < text_size-1; i++)
    {
        printf("%s | ", morse_code[i]);
    }
    
    return 0;
}

//----------------------------------------------------------------------------------------
//  FUNCTION DEFINITIONS

void convertToLower(char *sPtr)
{
    while(*sPtr != '\0')
    {
        *sPtr = tolower(*sPtr);
        *sPtr++;
    }
}

 

I changed the code to print out the value of alph_index and found out that after alph_index is incemented to 1, it doesnt increment any higher and just remains 1, I'm not sure why :(

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define LETTERS 26
#define NUMBERS 10

void convertToLower(char * sPtr);

int main(int argc, const char * argv[])
{
    // Morse Code 0-9
    //char * num_codes[] = {"-----", "----.", "---..", "--...", "-....", ".....", "....-", "...--", "..---", ".----"};
    // Morse Code A-Z
    char * alp_codes[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
    // A-Z
    const char searchKey[] = {'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'};
    
    // string variables & attributes
    char random_text[] = "abcd";
    char * txtPtr = random_text;
    int text_size = sizeof(random_text)/sizeof(random_text[0]);
    char * morse_code[text_size];
    int alp_index;
    
    convertToLower(random_text);
    
    // seaches for the searchKey in the txt and
    // converts that character of random_txt to the alp_code
    for(int h = 0; h < text_size-1; h++)
    {
        for(int i = 0; i < LETTERS; i++)
        {
            txtPtr = strchr(txtPtr, searchKey[i]);
            alp_index = (int)(txtPtr - random_text);
            
            if(txtPtr != NULL)
            {
                txtPtr++;
                morse_code[h] = alp_codes[alp_index];
                printf("(%d) ", alp_index);
                break;
            }
        
            txtPtr = random_text;
        }
    }
    
    // prints morse code
    for(int i = 0; i < text_size-1; i++)
    {
        printf("%s | ", morse_code[i]);
    }
    
    return 0;
}

//----------------------------------------------------------------------------------------
//  FUNCTION DEFINITIONS

void convertToLower(char *sPtr)
{
    while(*sPtr != '\0')
    {
        *sPtr = tolower(*sPtr);
        *sPtr++;
    }
}

------------------------My band: RISE OVER ME!

#3 Brother Bob   Moderators   -  Reputation: 8452

Like
3Likes
Like

Posted 23 April 2014 - 01:55 PM

Now is an excellent time to use the debugger to step through your program line by line.

 

First iteration searches for the letter 'a' in the string "abcd" and advances the string to "bcd". The second iteration searches for the letter 'a' in "bcd" and finds nothing, so the string is reset to "abcd", and proceeds to find 'b' and the string is advanced to "bcd". The third iteration searches for the letter 'a' and finds nothing, so the string is reset to "abcd", and proceeds to find 'b'... and so on.



#4 Vytis   Members   -  Reputation: 111

Like
1Likes
Like

Posted 23 April 2014 - 01:55 PM

You are not incrementing txtPtr to point to next character.

Basically you are converting the same character text_size times. Then searching for it in search array 26 times.

Change 45 line to
txtPtr = random_text + n;

But, there is a better way.
You could use the fact that letters a-z can be converted directly to integer 0-25 using ASCII table.
I leave the rest for You. A hint about debugger is also useful.

#5 DiegoSLTS   Members   -  Reputation: 1771

Like
0Likes
Like

Posted 23 April 2014 - 02:59 PM

Your code is kind of hard to follow, you should name your variables with meaningfull names (i and h in the first nested loop sure mean something more).

 

Also, it's not clear what algorithm you're using, it looks weird, the break instruction will finish the execution of that inner loop when the first letter is found in txtPtr. Are you sure you want to use "break" inside the loop instead of "continue"? With continue you skip the current iteration of the loop, but the loop will be executed again until it finishes.

 

Also note that a break in an inner loop won't break out of the outer loop (if that's what you intended).


Edited by DiegoSLTS, 23 April 2014 - 03:02 PM.


#6 Álvaro   Crossbones+   -  Reputation: 13661

Like
3Likes
Like

Posted 23 April 2014 - 03:55 PM

#include <stdio.h>
#include <string.h>
#include <ctype.h>
 
// Morse code for '0'-'9'
char const *morse_code_of_digit[] = {"-----", "----.", "---..", "--...", "-....", ".....", "....-", "...--", "..---", ".----"};
// Morse code for 'a'-'z'
char const *morse_code_of_alpha[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};

char const *morse_code_of_char(char c) {
  if (isalpha(c))
    return morse_code_of_alpha[tolower(c) - 'a'];
  if (isdigit(c))
    return morse_code_of_digit[c - '0'];
  return "";
}

int main(void) {
  char const *message = "abcd";
  int message_length = strlen(message);
  
  char const *morse_code[message_length];
  
  for (int i = 0; i < message_length; ++i)
    morse_code[i] = morse_code_of_char(message[i]);
  
  for(int i = 0; i < message_length; i++) {
    if (i > 0) printf(" | ");
    printf("%s", morse_code[i]);
  }
  puts("");
  
  return 0;
}






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS