Home > OS >  Check50 Errros: Not passing "handles substrings properly" tests. Check50 also can't c
Check50 Errros: Not passing "handles substrings properly" tests. Check50 also can't c

Time:12-17

I am working on Pset5 Speller. I made hashing function using the concept of binary numbers of base 26. So single alphabet words are stored in index 1-26, words containing apostrophes are stored in index 0 and the remaining are hashed using the first two alphabets. I tried to debug the code and run Valgrind on it still was unable to find the reason for failing of Substring tests any suggestions

// Implements a dictionary's functionality
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>
#include <cs50.h>

#include "dictionary.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 = (26 * 26)   26   1 ;
// Hash table
node *table[N];
int numberofwords = 0;
// Returns true if word is in dictionary, else false
bool check(const char *word)
{
    int index = hash(word);


    node *checker;
    checker = table[index];
    while (checker != NULL)
    {
        int comp = strcasecmp(word, checker->word);
        if (comp == 0)
        {
        return true;
        }
        else
        {
         checker = checker->next;
        }
    }


    // TODO
    return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
    int value = 0;
   int length = strlen(word);


    for (int i = 0; i < length; i  )
    {
        char c = word[i];
        if (c == '\'')
        {
            value = 0;
            return value;
        }
    }
    if (length == 1)
    {
        value = tolower(word[0]) - 'a'   1;
        return value;
    }
    else
    {
        value = (26 * (tolower(word[1]) - 'a'   1))   (tolower(word[0]) - 'a'   1);
        return value;
    }

}

// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
    //open dictionary and loop till EOF
    char buffer[LENGTH   1];

    FILE *temp = fopen(dictionary, "r");


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

    for (int i = 0; i < N; i  )
    {
        table[i] = NULL;
    }

    while (fscanf(temp, "%s", buffer) != EOF)
    {
        node *temp_node = malloc(sizeof(node));
        if (temp_node == NULL)
        {
            return false;
        }
        strcpy(temp_node->word, buffer);

        temp_node->next = NULL;

        //hash the word

        int locate = hash(buffer);

        if (table[locate] == NULL)
        {
            table[locate] = temp_node;
        }
        else
        {
            temp_node->next = table[locate];

            table[locate] = temp_node -> next;
        }

        numberofwords  ;
    }
    fclose(temp);
    // TODO
    return true;
}

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

// Unloads dictionary from memory, returning true if successful, else false
bool unload(void)
{
    for (int i = 0; i < N; i  )
    {
        node *start = table[i];
        node *freeer = start;
        node *remember = start;
        while (remember != NULL)
        {
            remember = remember->next;
            free(freeer);
            freeer = remember;
        }
    }
    // TODO
    return true;
}

here is the link to errors

https://submit.cs50.io/check50/ef7901d85e821af117b76bf7a2f43f44f38e3301

CodePudding user response:

This looks like a problem in load:

temp_node->next = table[locate];
 
table[locate] = temp_node -> next;

It looks circular, they are pointing to each other.

Try debugging with the small dictionary and a text file that contains the 5 words in ACTUAL OUTPUT pane plus the word cat.

check50 did not check for memory errors because of the failed test ("can't check until a frown turns upside down")

  • Related