0

here is my code /** * dictionary.c * * Computer Science 50 * Problem Set 5 * * Implements a dictionary's functionality. */

#include <stdbool.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include "dictionary.h"

// size of hash-table
#define SIZE 100000

// count size
int dictionarySize = 0;
// creating linked list nodes
typedef struct node
{
    char word[LENGTH + 1];
    struct node* next;
}node;

// create a hash-table

node* hashtable[SIZE] = {NULL};

// hash function
int hash (const char* word)
{
    int hash = 0;
    int n;
    for (int i = 0; word[i] != '\0'; i++)
    {
        if (isalpha(word[i]))
        {
            n = word[i] - 'a' + 1;
        }
        else
        {
            n = 27;
        }
        hash = (( hash * 8) + n) % SIZE;
    }
    return hash;
}

/**
 * Returns true if word is in dictionary else false.
 */
bool check(const char* word)
{
    char temp[LENGTH + 1];
    int len = strlen(word);
    for (int i = 0; i < len; i++)
    {
        temp[i] = tolower(word[i]);
    }
    temp[len] = '\0';

    int index = hash(temp);

    if (hashtable[index] == NULL)
    {
        return false;
    }

    // compare to word
    node* cursor = hashtable[index];

    while (cursor != NULL)
    {
        if (strcmp(temp, cursor->word) == 0)
        {
            return true;
        }
        cursor = cursor->next;

        return false;
    }
}

/**
 * Loads dictionary into memory.  Returns true if successful else false.
 */
bool load(const char* dictionary)
{
    // open dictionary
    FILE* file = fopen(dictionary, "r");
    if (file == NULL)
    {
        return false;
    }
    char word[LENGTH+1];
    // scanning and loading words in hash table
    while (fscanf(file,"%s\n",word) != EOF)
    {
        dictionarySize++;

        // allocat memory to new word
        node* newword = malloc(sizeof(node));

        newword->word = word;

        // index of word in array
        int index = hash(word);

        if (hashtable[index] == NULL)
        {
            hashtable[index] = newword;
            newword->next = NULL;
        }
        else
        {
            newword->next = hashtable[index];
            hashtable[index] = newword;
        }
        fclose(file);
        return true;
    }


}

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

/**
 * Unloads dictionary from memory.  Returns true if successful else false.
 */
bool unload(void)
{
    int index = 0;

    // iterate through entire hash table
    while (index < SIZE)
    {
        if (hashtable[index] == NULL)
        {
            index++;
        }
        else
        {
            while (hashtable[index] != NULL)
            {
                node* cursor = hashtable[index];
                hashtable[index] = cursor->next;
                free(cursor);
            }
            index++;
        }
    }
    return true;
}

and following are the errors

~/workspace/pset5/ $ make dictionary
clang -ggdb3 -O0 -Qunused-arguments -std=c11 -Wall -Werror   -c -o dictionary.o dictionary.c
dictionary.c:88:1: error: control may reach end of non-void function [-Werror,-Wreturn-type]
}
^
dictionary.c:110:23: error: array type 'char [46]' is not assignable
        newword->word = word;
        ~~~~~~~~~~~~~ ^
2 errors generated.
make: *** [dictionary.o] Error 1

please help.

1

the first error error: control may reach end of non-void function says your function can end without returning value

now what happens if cursor equals null on first iteration

bool check(const char* word)
{

............................

 while (cursor != NULL)
    {
        if (strcmp(temp, cursor->word) == 0)
        {
            return true;
        }
        cursor = cursor->next;

        return false;
    }
}

the while loop never gets executed and the function will return void instead of a boolean value

for the second error array type 'char [46]' is not assignable says you cannot assign value to array directly instead try to copy the string letter by letter, or you can use strcpy()

1
1

First error, at the end check function simply return false, you must return something, just use return in any condition IF, this causes the compiler "not know" that value is returned. we have always put a return statement at the end of a function that returns some value (not void function).

Second is not correct way to copy two strings, it is better to use the strcpy (new_node-> word, word);

1

You must log in to answer this question.

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