0

how do i check the logic of this ... i have not yet implemented the hash function ... can i check for errors without doing this or it is impossible to check without the hash function...?

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    // TODO
    //sort them in the table by number of chars rather that alphabetically

    //open dictionary in read mode
    FILE *dic = fopen("dictionary", "r");
    //check if file exists
    if (dic == NUL)
    {
        return false;
    }

    char *dic_word[46];
    //scan each word from the dictionary 
    while (fscanf(dic, "%s", dic_word) != EOF)
    {
        //create new node
        node *n = malloc(sizeof(node));
        if (n ==NULL)
        {
            return false;
        }
        //copy the word into the node
        strcpy(n->word, dic_word);
        n->next = NULL;
        
        //hash the word (how many chars does it have)
        int i = hash(dic_word);
        
        //if no elements at the ith location`s list, insert node
        if (table[i] == NULL)
        {
            table[i] = n;
        }
        //if there are other elements in the list, insert node in the front
        else
        {
            n->next = table[i];
            table[i] = n;
        }
    }
    return true;
}
2

One does not need to implement hash to test. Since the distro hash returns 0, all entries will go to index 0, which isn't great for performance but is valid for testing the logic.

Perhaps the question comes from the fact that this program always returns false from load and gives "Could not load...." message. There is a problem here FILE *dic = fopen("dictionary", "r");. Since dictionary is in double-quotes, it is a string literal; program will try to open a file named dictionary in the current working directory and if not found, which is the likely case, will fail and bail.

| improve this answer | |
  • ok i took the double-quotes away ( now it is FILE *dic = fopen(dictionary, "r"); ) but i still get the "could not load..." message – andream Oct 3 at 20:30
  • Also ... if i run DEBUG50 on the load function it loads ok (returns true)....i am very confused .... – andream Oct 3 at 20:46
  • Did debug50 remind you to compile the program? – DinoCoderSaurus Oct 3 at 20:53
  • no ...i compiled it, compiled ok....tried to run it..got the "could not load.." message ...then i ran debug50 with the small dictionary and cat.txt...load function returned true ...tried to run speller again ...got the same "could not load .." message.... – andream Oct 3 at 20:56
  • Intresting, on closer inspection, I don't see how it compiles at all (on the IDE) because of mispelling here if (dic == NUL). What environment are you using? Are you using make to compile? – DinoCoderSaurus Oct 3 at 21:06

You must log in to answer this question.

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