1

I have tried again and again different ideas but I simply can not get Speller to find the right misspelled words. I have no idea what I'm doing wrong. I keep changing things in various functions to see if I can tweak it, but I still get the same check50 output.

check50 link: https://submit.cs50.io/check50/b79644001ae08803b220e34ea6d104ae37fe7ee5

// Implements a dictionary's functionality

include

include

include

include

include

include

include "dictionary.h"

define HASHTABLE_SIZE 1000

// Represents a node in a hash table typedef struct node { char word[LENGTH + 1]; struct node *next; } node;

node *hashtable[HASHTABLE_SIZE];

// Number of buckets in hash table const unsigned int N = 26;

// Hash table node *table[N]; int word_count = 0;

// creating function to index the words int hash_index(char *hash_this) { unsigned int hash = 0; for (int i = 0, n = strlen(hash_this); i < n; i++) { hash = (hash << 2) ^ hash_this[i]; } return hash % HASHTABLE_SIZE; }

// Returns true if word is in dictionary else false bool check(const char *word) { // creating copy of word to put into the hash function int n = strlen(word); char word_copy[LENGTH + 1]; for (int i = 0; i < n; i++) { word_copy[i] = tolower(word[i]); }

word_copy[n] = '\0'; // add null terminator to end string
int h = hash_index(word_copy); // creating index for hashed word
node *cursor = hashtable[h]; // set cursor to point at the same address of the bucket 

// searching through the dictionary for the word
while (cursor != NULL)
{
    // if the word has been found
    if (strcasecmp(cursor->word, word_copy) == 0)
    {
        return true;
    }
    // if the word isn't found, advance the cursor 
    else 
    {
        cursor = cursor->next;
    }
}
// if word has not been found
return false;

}

// Hashes word to a number unsigned int hash(const char *word) { // implamenting the hash function // using the djb2 function http://www.cse.yorku.ca/~oz/hash.html unsigned long hash = 5381; int c;

while ((c = *word++))
{
    hash = ((hash << 5) + hash) + tolower(c); /* hash * 33 + c */ 
}

// keeping the words in the hash tables
return hash % N;

}

// Loads dictionary into memory, returning true if successful else false bool load(const char *dictionary) { // creating a pointer to connect for (int i = 0; i < N; i++) { table[i] = NULL; }

// open dictionary
FILE *file = fopen(dictionary, "r");

if (file == NULL)
{
    printf("file cannot be opened\n");
    return false;
}

// create a string for word to be read into
char word[LENGTH + 1];

// reading one string at a time through the dictionary
while (fscanf(file, "%s", word) != EOF)
{
    // allocating memory for a new node
    node *n = malloc(sizeof(node));

    if (n == NULL)
    {
        unload();
        return false;
    }

    strcpy(n->word, word); //copies word to malloc
    int h = hash_index(n->word); // creating index of words to inset into hastable
    node *head = hashtable[h]; // creating head to point to node

    // putting nodes in starting at the head 
    if (head == NULL)
    {
        hashtable[h] = n;
        word_count++;
    }
    else
    {
        n->next = hashtable[h];
        hashtable[h] = n;
        word_count++;
    }
    free(n);
}
fclose(file);
return true;

}

// Returns number of words in dictionary if loaded else 0 if not yet loaded unsigned int size(void) { return word_count; }

// Unloads dictionary from memory, returning true if successful else false bool unload(void) { node *head = NULL; node *cursor = head; // freeing linked lists while (cursor != NULL) { node *temp = cursor; cursor = cursor->next; free(temp); } return true; }

  • can you post the whole dictionary.c so someone can compile/run/debug it? – stensal May 29 at 8:58
  • can you edit your question please. – Ojou Nii May 29 at 13:02
  • I put the whole dictionary.c on. Does that work better? – Bethany May 30 at 5:42
2

Once a free is issued, as in free(n);, all the data associated with it is gone. load goes through all the work to build the node and point the node then POOF!

| improve this answer | |
  • That was it!!! I thought I needed to free the node right after I used it... but I do all that in the function. – Bethany May 30 at 6:03
  • Good LORD!! So this is what was tripping me up for 4 days! @DinoCoderSaurus.... If I had kingdom, I'd give it to you. An obvious corollary question is this an experiential thing, or is it possible to debug? I couldn't see anything obvious in dbg. – user3671 May 30 at 13:09
  • It's possible to debug. Wait, what? It never finds any misspelled words? What could possibly be wrong with the check function? Stepping through the debugger reveals the head of the list is always null. Wait, what? What could possibly be wrong with the load function? And so on. IMO sometimes "watching" a program execute one line at a time is very revealing because it concentrates your focus. Now that you know the solution, it might be worth it to reinstall the bug, and take it for a spin in debug50. – DinoCoderSaurus May 30 at 14:13
  • Good idea. Tbh, I wasn't using just debug50, just dbg (but maybe they are the same thing under the hood?). Anyway... thanks to you, I'm going to take a break from my screen today. Muchas Gracias! – user3671 May 30 at 15:44

You must log in to answer this question.

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