0

I think I have done everything you're supposed to do in PSET5 Speller. when I manually check the outputs of my code, it works perfectly well. It matches the staff solutions one to one. Yet Check50 fails in all points other than being able to compile the code.

When I check what is wrong it gives me an error similar to the image below for each task. I couldn't decipher the meaning of the error unfortunately. But even in the output I can see that my code correcty identified the misspelled words. It just did not print the benchmarks. Which is done in speller.c which we are not allowed to alter anyway.

enter image description here

I've put the entirety of dictionary.c (my code) below. What am I doing wrong? Why does check50 fail?

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

#include "dictionary.h"

typedef struct node
{
    char word[LENGTH + 1];
    struct node *next;
}
node;

const unsigned int N = 26;

node *table[N];

bool check(const char *word)
{
    char my_word[LENGTH+1];
    for (int i=0;i<LENGTH+1;i++)
    {
        my_word[i]='\0';
    }
    for (int i=0;i<strlen(word);i++)
    {
        my_word[i]=tolower(word[i]);
    }

int hash_loc=hash(my_word);
for(node *temp=table[hash_loc];temp!=NULL;temp=temp->next)
{
    if(strcmp(temp->word,my_word)==0 && temp->word[0]!='\0')
    {
        return true;
    }
}
return false;
}

unsigned int hash(const char *word)
{
    return toupper(word[0]) - 'A';
}
unsigned int my_index=0;

bool load(const char *dictionary)
{
    // TODO
    FILE *file = fopen(dictionary, "r"); //open the file
    if (file != NULL) //check if the file exists
    {
        char each_word[LENGTH + 1]; //create a variable that will hold each word of the text
        while(fscanf(file,"%s",each_word)!=EOF) //loop to scan each word of the text
        {
            node *new_memory=malloc(sizeof(node)); //create new node
            if(new_memory==NULL) //check valid memory
            {
                return 50;
            }
            my_index++;

            strcpy(new_memory->word,each_word); //copy the scanned text into new node

            int hash_loc=hash(each_word);
            if(table[hash_loc]!= NULL) 
            {
                new_memory->next=table[hash_loc]; 
                table[hash_loc]=new_memory;
            }
            else
            {
                table[hash_loc]=new_memory;
            }
        }
        fclose(file);
        return true;
    }
    return false;
}

unsigned int size(void)
{
    return my_index;
}

bool unload()
{
    for(int i=0;i<N;i++)
    {
        for(node* temp=table[i]->next;temp!=NULL;temp=table[i]->next)
        {
            {
                free(table[i]);
                table[i]=temp;
            }
        }
        free(table[i]);
    }
    return true;
}

2 Answers 2

0

Did you actually run the program yourself locally or did you just run check50 to see if it passed?

When I compiled and ran the code, it terminated early and consistently with a seg fault. This matches the results shown by check50, except that it doesn't display the seg fault. Another clue is that the results displayed for your code always match the misspelled words but never displays the statistics generated. The output is never complete.

The reason the code fails is the seg fault. There are serious issues with the unload function that require attention. I'll let you try and fix them on your own. If you need help, please post a new question with details.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • I just ran my code again, no seg faults occured. It completed the program and gave me all the results including the benchmarks. Benchmarks dont appear only when I run check50.
    – dohaal
    Commented Dec 8, 2022 at 7:44
0

This is how my terminal looks when I run the program. No seg faults occur. It is running on lalaland.txt by the way.

enter image description here

1
  • then are you sure that the source code you submitted to check50 is the same code that you are compiling and running? No chance you have two different source code files? Also, did you run the program with valgrind and get a no leaks possible result? It is possible that code will generate a seg fault in one environment and not in another. If yours isn't generating the seg fault it will run but not correctly and not show up. The problems are with unload. Try putting a return true; statement in as the first line of unload and submit to check50 and see what happens.
    – Cliff B
    Commented Dec 8, 2022 at 23:04

You must log in to answer this question.

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