0

i genuinely have no idea where the problem lies so any help would be appreciated, thanks in advance.

check function

bool check(const char *word)
{
    trav = root;

    int index = 0;

    for (int n = strlen(word); index < n; index++)
    {
        if (trav->children[index] == NULL)
        {
            return false;
        }
        else
        {
            trav = trav->children[index];
        }
    }

    if (trav->is_word == true)
    {
       return true;
    }
    else
    {
        return false;
    }
}

load function

bool load(const char *dictionary)
{
    // allocate memory for root
    root = calloc(1, sizeof(node));

    //create cursor
    trav = root;

    // open dictionary for reading
    FILE *inptr = fopen(dictionary, "r");
    if (inptr == NULL)
    {
        printf("Couldn't open the file.\n");
        return false;
    }

    char word[LENGTH + 1];

    // scan through the dictionary one word at a time
    // and iterate through the trie
    while (fscanf(inptr, "%s", word) != EOF)
    {
        for (int i = 0, n = strlen(word); i < n; i++)
        {
            // create index for letters in the trie
            if (isalpha(word[i]))
            {
                index = tolower(word[i]) - 'a';
            }
            else
            {
                // for apostrophe
                index = 26;
            }

            // check next node
            // if null allocate memory and go there
            if (trav->children[index] == NULL)
            {
                trav->children[index] = calloc(1, sizeof(node));
                trav = trav->children[index];
            }
            // if not just go there
            else
            {
                trav = trav->children[index];
            }
        }
        trav->is_word = true;

        numwords++;

        // set cursor back to root
        trav = root;
    }

    fclose(inptr);
    return true;
}

size function

unsigned int size(void)
{
    if (numwords != 0)
    {
        return numwords;
    }
    else
    {
        return 0;
    }
}

unload function

void freenode (node *cursor)
{
    for (int i = 0; i < 27; i++)
    {
        if (cursor->children[i] != NULL)
        {
            freenode(cursor->children[i]);
        }
        else
        {
            free (cursor);
        }
    }
}

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
    freenode(root);
    return true;
}

check50: https://cs50.me/checks/94a2114125b9303c5b78862123f70745023a2dc6

1 Answer 1

1

check:

Do not, by no means, allocate nodes in check. You seem to replace your root node by an empty one, losing any way to access the nodes in the trie.

What's index? I don't see it's ever calculated from word[i]. Make sure it ignores case, as check receives words in any case.

if (trav->is_word == true) belongs after the loop. When examining the "c" node, you don't already know whether the word will be "cat" and exist or "Cthuluh" and not exist (clearly a misspelling of "Cthulhu").

load:

Looks fine. Maybe check whether calloc returned NULL (in case you run out of RAM), but in our case, we'd have bigger problems than a segmentation fault in that case.

size:

Do you understand what &load means? It's the memory address of the load function. Not that useful, and probably not false (or NULL, both of which are represented by value zero).

If you haven't loaded a dictionary, isn't your counter variable initialised to 0 anyway?

unload:

            cursor = cursor->children[i];
            freenode(cursor);

makes little sense. In the next iteration, you need cursor to have its original value. Combine those lines to

            freenode(cursor->children[i]);

and don't assign to cursor at all.

You surely meant to unload root, not trav

4
  • Hey! Thanks for the help. i did fix up everything you mentioned, and even though i think the code should run smoothly now, it doesn't. check50 outputs the same exact errors, I've updated my code and check50 link above. Thanks in advance
    – Ashley Moe
    Jul 31, 2018 at 8:29
  • In check, you need to do the same character to index calculation as in load.
    – Blauelf
    Jul 31, 2018 at 9:42
  • And I missed something else: The free(cursor) in freenode should not be in an else, but after the for loop.
    – Blauelf
    Jul 31, 2018 at 9:45
  • All worked out. Your help was priceless. Thank You.
    – Ashley Moe
    Aug 1, 2018 at 10:23

You must log in to answer this question.

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