0

i am a beginner and wanted to solve the pset with hashfunction and than a linked list.

I wanted to start out with the simplest way i can thought of. One bucket for every letter in the alphabet. Here is my hash function:

int hash(new_node->word)
    {
    int hasvalue;
    hahvalue=tolower(new_node->word[0])-96;
    return hasvalue;    
    }

Can somebody tell me what i did wrong here ?

Please dont be too hard on me i am really trying

  • I am as you a beginner, but try : put "new_node -> word" in a () and check it – mojtaba1 Jul 15 '15 at 13:17
  • I am taking CS50x and currently doing the spell checking problem set. I am working on the load() function and was wondering if I could use your hash function in my work? I will credit you of course for the hash function in my code. Kind regards, Adi from London, UK. – AdiKhajuria Oct 16 '16 at 18:08
5

Often, people look for something complicated when it's the simple things. ;-)

Assuming you did a direct cut and paste, the first problem is very simple. You have a typo.

hahvalue=tolower(new_node->word[0])-96;
  ^

It shoud be HASvalue, not HAHvalue. [emphasis added] I am assuming that you have the correct include to support tolower()?

Next, your function prototype needs work. What is the variable type for the parameters being passed?

int hash(<type> new_node->word)

I would also recommend a change. It looks like you're passing in an element of a structure. Make it simpler and replace new_node->word with something simpler, like word and give it the same type as word from the node structure. For example:

int hash( char* word )

However, Mojitaba1 does have a better suggestion. It looks like you're trying to pass a structure element to the function - two levels removed from what you want to hash. Instead of passing that, the hash function would be more flexible if you simply passed the letter to be hashed. Here is an example of the call to the function,

int wordhash = hash(new_node->word[0]);

followed by the structure of the hash function.

int hash(char letter)
{
    int hashvalue;
    hashvalue=tolower(letter)-96;
    return hashvalue;    
}

When debugging compiler errors, it helps to look very carefully at what the error says and exactly where it's indicating the problem is on the line of code causing the error.

Typos and syntax errors are common problems when programming, especially when you start out. Don't worry, it gets easier with time and practice. We all went through it.

Here's a heads-up. You should incorporate a special case into your function to handle apostrophes.

If this answers your question, please accept this answer to close the question. Let's keep up on forum maintenance. ;-)

| improve this answer | |
0

instead of what you put in the parentheses you should put what you passed to the function. for example before calling this function you put str as a variable to pass to the function, then in the function body you should use str as an argument for the tolower function.

| improve this answer | |
  • I thought i did that with int hash(new_node->word) – hmmmbob Jul 15 '15 at 15:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .