/* Solution for Comp2303 Assignment 1 It implements all normal and advanced functionality. This program consists of 3 main pieces: 1) WordList and associated routines - add to list, sort list, write to file. 2) Grid and associate routines - read, search for words, find unused letters. 3) main() and file handling functions. I have passed structs by value unless they need to be modified. */ #include #include #include #include typedef int boolean; #define TRUE 1 #define FALSE 0 #define BAD_USE 1 #define BAD_SIZE 4 #define BAD_FILE_OPEN 3 #define MISSING_DATA 5 #define DEFAULT_OUT_NAME "found" #define MAX_LETTERS 40 /* Comment me */ /* WordList stuff The elements are stored in an array. */ typedef struct { int len; int capacity; char** data; } WordList; /* Populate WordList as an empty list */ void alloc_list(WordList* list) { int i; list->len = 0; list->capacity = 10; list->data = (char**)malloc(sizeof(char*)*10); for (i = 0; i<10; ++i) { list->data[i] = 0; } } /* Add a word to the list (increasing capacity if required) */ void add_item(WordList* list, char* word) { char* t; int newCap = list->capacity+10; if (list->len==list->capacity) { list->data = (char**)realloc(list->data, newCap*sizeof(char*)); list->capacity = newCap; } t = (char*)malloc(sizeof(char)*strlen(word)+1); strcpy(t, word); list->data[list->len++] = t; } /* free memory used by the list, does not free the head (l) */ void free_list(WordList l) { int i; for (i = 0; i-1)) typedef struct { int dim; char** data; } Grid; /* Allocate memory for (r) as a 2D array with dimension (size) */ void alloc_grid(Grid* r, unsigned int size) { unsigned int c; r->dim = size; r->data = (char**)malloc(sizeof(char*)*size); for (c = 0; cdata[c] = (char*)malloc(sizeof(char)*size); } } /* Free memory for Grid g - does not free the struct itself */ void free_grid(Grid g) { int c; for (c = 0; cdata[i][j] = c; used->data[i][j] = '*'; } res = fscanf(f, "%c", &c); if ((res!=1) || (c!='\n')) { return MISSING_DATA; /* actually its too much */ } } return 0; } /* End Grid routines */ /* Send unused letters from g to stdout */ /* used for debugging */ void print_remaining(Grid g, Grid used) { int r, c; for (r = 0; r6) { print_and_exit(BAD_USE, 0); } /* Look for -sort and -out in either order */ while (i