0

I am currently working on Pset5, Speller. My output seems to be identical with cs50's solution (mine is simply way slower), but I still can't pass check50, it simply shows a blank window for my output.

The link: https://submit.cs50.io/check50/cb607c4de605d1fd0b763908edee69abb37c3df7

I have tried searching for answers online, tried it with other provided texts, own texts with only one letter (or more) and the output seems to be right as well.

I am thankful for any advices!

My output with "lalaland.txt"

WORDS MISSPELLED:     955
WORDS IN DICTIONARY:  143091
WORDS IN TEXT:        17756
TIME IN load:         0.02
TIME IN check:        0.49
TIME IN size:         0.00
TIME IN unload:       0.00
TIME IN TOTAL:        0.52

And here is CS50s output:

WORDS MISSPELLED:     955
WORDS IN DICTIONARY:  143091
WORDS IN TEXT:        17756
TIME IN load:         0.02
TIME IN check:        0.01
TIME IN size:         0.00
TIME IN unload:       0.01
TIME IN TOTAL:        0.04

Last but not least, my code:`

// Implements a dictionary's functionality

#include <stdbool.h>
#include <stdio.h>
#include "dictionary.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <strings.h>

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

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

// Hash table
node *table[N];

// Returns true if word is in dictionary else false
bool check(const char *word)
{
node* cursor = NULL;
    int x = hash(word);
    cursor = table[x];

    while (true)
    {
        //returns true if strings are identical
        if (strcasecmp(cursor -> word, word) == 0)
            return true;
        else
        {
            //if not, cursor points to next element the linked list
            cursor = cursor -> next;
            if (cursor == NULL)
                return false;
        }
    }
}

// Hashes word to a number
unsigned int hash(const char *word)
{
    unsigned long hash = 5381;

    int c = *word;
    c = tolower(c);
    while (*word != 0)
    {
        hash = ((hash << 5) + hash) + c;
        c = *word++;
        c = tolower(c);
    }
    return hash % N;
    //hash function that Ive copied from: u/SocratesSatisfied, reddit
    //https://www.reddit.com/r/cs50/comments/eo4zro/good_hash_function_for_speller/

}
unsigned int sizer = 0;
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
     FILE *file = fopen(dictionary, "r");
    if (file == NULL)
    {
        fclose(file);
        return false;
    }

    //read input from file
    char temp[LENGTH + 1];

    //reading elements from a file until the end
    while(fscanf(file, "%s", temp) != EOF)
    {
        int hasher = hash(temp);
        node *n = malloc(sizeof(node));

        n -> next = NULL;
        if (n == NULL)
        {
            return false;
        }

        //reading the value of temp inside the table array
        strcpy(n -> word, temp);

        if (table[hasher] == NULL)
        {
            table[hasher] = n;
        }
        else
        {
            n -> next = table[hasher];
            table[hasher] = n;
        }
        //storing amount of read elements
        sizer++;
    }
    fclose(file);
    return true;
}

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

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
    for (int i = 0; i < N; i++)
    {
        node* temp = NULL;
        node* cursor = NULL;
        cursor = table[i];

        //jumping from element to elemnet and free it
        while (true)
        {
            temp = cursor;
            cursor = cursor -> next;
            free(temp);
            if (cursor == NULL)
                break;

        }
    }
    return true;
}

1 Answer 1

2

Have you tried running with the small dictionary? Observe the "WORDS IN DICTIONARY" counts in the check50 report. If the word to check hashes to an empty index (which will not happen with the large dictionary) then this line cursor = cursor -> next; in check will seg fault. cursor is NULL, therefore cursor -> next doesn't exist.

1
  • Thanks, finally worked!!! Can finally move on to python Nov 30, 2020 at 13:14

You must log in to answer this question.

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