Your idea makes a little sense to me and I'm sure it would be much quicker and save a lot of memory, but there are a few things in the assignment that have to stay how they were posted in my original code. Like the bubble sort for example. I completed the code, even if it isn't the most logical or quickest way, but it does run correctly. Your help was much appreciated. For your viewing pleasure:
Code:
#include <stdio.h>
#include <string.h>
#define MAX_WORD_LEN 6
#define MAX_NUM_WORDS 30000
// Struct to use.
typedef struct {
char word[MAX_WORD_LEN+1];
char sort[MAX_WORD_LEN+1];
} jumble_type;
void bubblesort(char letters[], int length) {
char temp;
int i, j;
// Sort each word alphabetically by letter.
for(i = 0; i < length; i++){
for(j = i; j < length; j++){
if(letters[i] > letters[j]){
temp = letters[j];
letters[j] = letters[i];
letters[i] = temp;
}
}
}
}
int main() {
// Declare variables for dictionary and jumbled words.
jumble_type list[MAX_NUM_WORDS];
jumble_type jumb[MAX_NUM_WORDS];
int i, j, k;
// Open the files to write into.
FILE* dictionary = fopen("jumbledct.txt", "r");
FILE* jumbled = fopen("jumble.txt", "r");
// Check to make sure files open.
if ((dictionary == NULL) || (jumbled == NULL))
printf("Error, file does not exist.");
else {
// Read in the number of words from the dictionary of words.
int numwords;
fscanf(dictionary, "%d", &numwords);
// Read in the number of words from the jumble of words.
int num_jumb;
fscanf(jumbled, "%d", &num_jumb);
// Go through each dictionary word, store in list, copy, then sort each.
for (i = 0; i < numwords; i++) {
fscanf(dictionary, "%s", list[i].word);
strcpy(list[i].sort, list[i].word);
bubblesort(list[i].sort, strlen(list[i].sort));
}
// Go through each jumbled word, store in jumb, copy, then sort each.
for (i = 0; i < num_jumb; i++) {
fscanf(jumbled, "%s", jumb[i].word);
strcpy(jumb[i].sort, jumb[i].word);
bubblesort(jumb[i].sort, strlen(jumb[i].sort));
}
// Make sure number of jumbled words x: 0 < x < 100
if ((num_jumb > 0) && (num_jumb < 100)) {
// Print out the jumbled words.
for (j = 0; j < num_jumb; j++){
printf("JUMBLE PUZZLE #%d: %s\n", j+1, jumb[j].word);
// Compare each word, then print out all dictionary word solutions.
int x=0;
for (k = 0; k < numwords; k++) {
if (strcmp(jumb[j].sort, list[k].sort) == 0) {
printf("%s\n", list[k].word);
x++;
printf("\n");
}
}
// Print out no solutions.
if (x == 0) {
printf("Sorry, this puzzle has no solutions.\n");
printf("\n");
}
}
}
// Print out error for negative values or values over 100.
else
printf("Your number must be greater than 0 and less than 100.\n");
}
return 0;
}
Thank you for all your help. Newbie question: How do I mark as solved? Or did this post just do that?