Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


NUCLEAR RABBIT

Member Since 12 Apr 2006
Offline Last Active Jan 28 2015 11:07 PM

Posts I've Made

In Topic: Polymorphism and Interfaces in Java

07 October 2014 - 03:19 PM


You've got this backwards. The code creates an object of type ArrayQueue and assigns it to a reference of type QueueInterface. Because ArrayQueue implements the QueueInterface, ArrayQueue "is a" QueueInterface. It is valid for a QueueInterface to point to an ArrayQueue.

Ohhhh, okay. I was def wrong on how the assignment was going on. Makes a lot more sense now!

 


Yes, he could have used ArrayQueue on the left too, but in OOP it's a good practice to keep things general. If you can use an interface or a base class to define a variable you should do it. That way, if you found ArrayQueue is not the best QueueInterface implementation, you only need to change one word in the code. It doesn't sound that great (change one word instead of two), but in bigger projects and complex situations it can make a difference, mainlly in the readability and mantainability of the code.

I see your point about "general programming", not sure why I skipped that aspect. I guess I was just confused when I thought an interface object was being created, which didnt make sense to me because the methods aren't defined. Totally forgot that myQueue was a reference type and it makes sense for it to refer to a type that extends QueueInterface because it "is-a" type of QueueInterface

 

Thanks guys! biggrin.png


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


PARTNERS