0

I've seen a similar post about this but I couldn't really figure out why my code doesn't pass check50. It might be on the load function, but I'm not entirely sure. I already ran the code with the dictionary (small and large) and texts, and there were no errors from it and produces same output from the keys folder. Can somebody check what's wrong? Thank you!

bool check(const char *word) {

// find index of current word
unsigned int index = (hash(word) % N);

// create temp node
node *tmp = table[index];

while(tmp != NULL) {

    if(strcasecmp(word, tmp->word) == 0) {
        return true;
    } else {
        tmp = tmp->next;
    }
}

    return false;
}


unsigned int hash(const char *word) {
// djb hash from https://www.programmingalgorithms.com/algorithm/djb-hash/
// a little bit modified

unsigned int hash = 5381;
unsigned int i = 0;
unsigned int length = strlen(word);

//actual hashing
for (i = 0; i < length; word++, i++)
{
    hash = ((hash << 5) + hash) + (tolower(*word));
}

    return hash;
}

bool load(const char *dictionary) {

    char word[LENGTH + 1];
    FILE *file = fopen(dictionary, "r");
    unsigned int index = 0;

    if (file == NULL) {
        return false;
    }

while (fscanf(file, "%s", word) != EOF) {

        if (feof(file)) {
            fclose(file);
            return true;
        }

        index = 0;

        // create new node and insert values
        node *n = malloc(sizeof(node));
        if (n == NULL) {
            return false;
        }

        strcpy(n->word, word);
        n->next = NULL;

        // get index via hash function
        index = (hash(n->word) % N);

        //insert node to it's corresponding array & list
        if(table[index] == NULL) {
            table[index] = n;
            d_words++;
        } else {
            n->next = table[index];
            table[index] = n;
            d_words++;
        }

}

fclose(file);
return true;

}

unsigned int size(void)
{
    return d_words;
}

bool unload(void)
{
    // create two temporary node
    node *tmp = table[0];
    node *cur = tmp;

    for (int i = 0; i < N; i++) {

    while (tmp->next != NULL) {
        tmp = tmp->next;
        free(cur);
        cur = tmp;
    }

}

return true;
}

Here's an image of check50 output.

check50 output of the code

2
  • check and hash functions would be helpful to see.
    – Cliff B
    May 25 '20 at 5:13
  • oh sure I'll edit the code above
    – Mars
    May 25 '20 at 6:48
1

Interesting! Tested with the large dictionary and it worked. Tested it with the small dictionary of a few words and it generates a seg fault. Good lesson on why to test with different data sets!

I tested it with the small dictionary and it seg faulted. That's why check50 is failing. It's testing with various small dictionaries of one or a few words.

I'll give you a hint. There's a problem in unload()

Now, go attack the problem! ;-)

1
  • Actually fixed it! All I did is NOT do any 'mallocs' for the unload pointers and just assign the needed values for each directly at their declaration. Thank you for pointing that out, Sir!
    – Mars
    May 26 '20 at 7:03

You must log in to answer this question.

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