0

I've run the debugger on my faulty program and reached the conclusion that the main that's causing the problem is among the following:

 while(fscanf(f, "%s", term) != EOF)
        {
            fscanf(f, "%s", term);
            node *nd = malloc(sizeof(node));
            if (nd != NULL)
            {
                strcpy(nd->word, term);
                int array_location = hash(term);
                if(table[array_location] == NULL)
                {
                    nd->next = NULL;
                    table[array_location] = nd;
                    count++;
                    printf("%i", count);
                }
                else
                {
                    nd->next = table[array_location];
                    table[array_location] = nd;
                    count++;
                    printf("%i", count);
                }
            }
            else
            {
                fclose(f);
                return false;
            }
        }

However, the debugger stopped at separate lines each time I used it. The first time, it stopped at while(...), the second at:

else
                {
                    nd->next = table[array_location];
                    table[array_location] = nd;
                    count++;
                    printf("%i", count);
                }

and so on, although I'm not quite sure why. As for the hash function, it appears to work correctly (as deduced by using debug50). Could you please help me solve the problem?

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

int count = 0;

// 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;

// Hash table
node *table[N];

// Returns true if word is in dictionary, else false
bool check(const char *word)
{
    // TODO
    char *term = NULL;
    strcpy(term, word);
    int array_location = hash(term);
    for(node *find = table[array_location]; find != NULL; find = find->next)
    {
        if (strcasecmp(word, find->word) == 0)
        {
            return true;
        }
    }
    return false;
}


// Hashes word to a number
unsigned int hash(const char *word)
{
    // TODO
    char first_letter = *word;
    char capital_letter = toupper(first_letter);
    int letter_number = capital_letter;
    int hash_code = letter_number - 65;
    return hash_code;
}

// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
    // TODO
    for(int y = 0; y < 26; y++)
    {
        table[y] = NULL;
    }
    FILE *f = fopen(dictionary, "r");
    if (f != NULL)
    {
        char term[LENGTH + 1];
        while(fscanf(f, "%s", term) != EOF)
        {
            fscanf(f, "%s", term);
            node *nd = malloc(sizeof(node));
            if (nd != NULL)
            {
                strcpy(nd->word, term);
                int array_location = hash(term);
                if(table[array_location] == NULL)
                {
                    nd->next = NULL;
                    table[array_location] = nd;
                    count++;
                    printf("%i", count);
                }
                else
                {
                    nd->next = table[array_location];
                    table[array_location] = nd;
                    count++;
                    printf("%i", count);
                }
            }
            else
            {
                fclose(f);
                return false;
            }
        }
    }
    else if (f == NULL)
    {
        fclose(f);
        return false;
    }
    fclose(f);
    return true;
}

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

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

You must log in to answer this question.

Browse other questions tagged .