Jump to content

  • Log In with Google      Sign In   
  • Create Account


NUCLEAR RABBIT

Member Since 12 Apr 2006
Offline Last Active May 21 2014 05:42 PM

Posts I've Made

In Topic: strchr question (C programming)

27 April 2014 - 03:13 PM

thank you guys! biggrin.png


In Topic: Array Location? (C Programming)

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++;
    }
}

In Topic: String help in C

16 April 2014 - 09:43 PM

Look more carefully at your loop. You are using strPtr to store the result, but using random_text to search on.  You probably meant to assign the string to the pointer before the loop, and use strPtr as the parameter.

 

ahhhhhhh, thank you!biggrin.png


In Topic: Array Problem in C

30 March 2014 - 08:32 PM

 

  for(int g = 0; g < new_array_size; g++) {

Should be
  for(int g = 0; g < size; g++) {
EDIT: Why is that function called `binarySearch'? I can only imagine that you are in the early stages of putting together some code and this is just some test, and not really what you intend to do. But giving a function a name unrelated to what it does is extremely confusing and I wouldn't even do it during tests. I certainly wouldn't post it to a forum.

 

 

Wow thank you!! Yes you're right it is pretty confusing, very sorry for any inconvinience


In Topic: Array Problem in C

30 March 2014 - 08:11 PM

You are quite ambiguous with the results don't really come out to what they should be.
This code will never put the value 0 into the new array because NULL evaluates to 0 and you only add non-zero values. C/C++ is not like Java in which null refers to nothing. In C/C++, NULL evaluates to 0 and you set pointers to NULL or nullptr because accessing memory at location 0 is guaranteed to crash your program.

Setting the ones you don't want to -1 would work better because rand() % 10 will always be positive because rand() is always positive.
 

i changed all references to NULL and made them a random number outside the scope I made the random numbers (999), but the same problem is still occuring :/


Perhaps you could give your program's output and your expected output.

 

 

Oh okay I see, I was not aware of that, thank you! Well what I mean is that, some of the values in the new array are the same as the original array, but some of the values are not even in the original array to begin with. The size of the new array is also not the correct size it should be, which I believe is on account of the counter variable be incremented when it shouldn't be

 

updated code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void binarySearch(int rand_nums[], int size);
void printArray(int nums[], int size);

int main(int argc, const char * argv[])
{
    srand((unsigned)time(NULL));
    
    int rand_nums[20];
    int rand_num_size = sizeof(rand_nums)/sizeof(rand_nums[0]);
    
    // fills array with random nums 0-10
    for(int i = 0; i < rand_num_size; i++)
        rand_nums[i] = rand() % 10;
    
    binarySearch(rand_nums, rand_num_size);
    
    return 0;
}

void printArray(int nums[], int size) {
    printf("Array set: ");
    for(int i = 0; i < size; i++)
        printf("%d ", nums[i]);
}

void binarySearch(int rand_nums[], int size) {
    int counter = 0;
    
    printf("OLD Array:\n");
    printArray(rand_nums, size);
    
    // goes through array and makes duplicates NULL
    for(int i = 0; i < size; i++) {
        for(int j = i+1; j < size; j++) {
            if(rand_nums[i] == rand_nums[j] && rand_nums[j] != 999 && rand_nums[i] != 999) {
                rand_nums[j] = 999;
                counter++;
                printf("\ncounter = %d", counter);
            }
        }
    }
    
    // new array with custome size
    int new_array[size-counter];
    int new_array_size = (int)sizeof(new_array)/sizeof(new_array[0]);
    
    int index = 0;
    
    // fills new array with non NULL values of rand_nums
    for(int g = 0; g < new_array_size; g++) {
        if(rand_nums[g] != 999) {
            new_array[index] = rand_nums[g];
            index++;
        }
    }
    
    // victory
    printf("\n\nNEW Array:\n");
    printArray(new_array, new_array_size);
}

PARTNERS