Hi everyone,
So I was given this assignment, where i'm given two files, one is the dictionary file called jumbledct.txt that contains about 23K words and the other is a file that i've named jumble.txt which contains a copy of all the words from the dictionary file in the next column and should have all the words but they are supposed to be alphabetized by letters for each words. For example, tangy and agnty. So far, i've managed to get all the words copied into the jumble.txt file and created a second column of the same words, but I cant for the life of me figure out how to print out the dictionary letters in the jumble file or the copy of them with the letters in alphabetical order. I feel like it has something to do with the bubblesort or pointers maybe? I'm sure it's not difficult, its just not coming to me, or maybe I've just completely done the bubblesort wrong. I feel like I cant continue with the program until I have this vital alphabetizing part figured out. And once I have that all figured out, I need the program to determine which words match to the alphabetical jumbled set of letters obviously by comparing the sorted set (jumbled) to the word set (dictionary). The program is supposed to print out all solutions to the jumbled word, like toersc could be corset, escort, rectos, sector. Also at the top of the dictionary file (called jumbledct.txt) the integer is the amount of words. And capital letters do not need to be worried about, the dictionary file is all lowercase. I'm not looking for someone to do the assignment for me, as i've seen so many be called out for on various forum sites, i'm just looking for some pointers on how to complete the assignment. Thank you in advance for any information you can give.
Code:
#include <stdio.h>
#include <string.h>
#define MAX_WORD_LEN 6
#define MAX_NUM_WORDS 30000
// Struct to use.
struct jumble {
char word[MAX_WORD_LEN+1];
char sort[MAX_WORD_LEN+1];
};
void bubblesort(char letters[], int length) {
char temp;
int i, j;
// initialize length array to hold pointers to each element in letters
//for (i = 0; i < MAX_WORD_LEN + 1; i++){
//length[i] = letters[i];
//}
// sort the length array
for(i = 0; i < MAX_WORD_LEN; i++){
for(j = 0; j < MAX_WORD_LEN-i-1; j++){
if(letters[j] < letters[j+1])
temp = letters[j];
letters[j] = letters[j+1];
letters[j+1] = temp;
}
}
}
int main() {
struct jumble list[MAX_NUM_WORDS];
FILE* ifp = fopen("jumbledct.txt", "r");
FILE* ofp = fopen("jumble.txt", "w");
// Read in the number of words from the dictionary.
int numwords;
fscanf(ifp, "%d", &numwords);
// Go through each word.
int i;
for (i = 0; i < numwords; i++) {
fscanf(ifp, "%s", list[i].word);
// Copy the word into the sort component of the struct.
strcpy(list[i].sort, list[i].word);
//Initialize bubblesort array.
bubblesort(list[i].word, list[i].sort);
//strcmp
}
// Print out the words and their other arrangement.
for (i = 0; i < numwords; i++) {
fprintf(ofp, "%s %s\n", list[i].word,
list[i].sort);
}
fclose(ifp);
fclose(ofp);
return 0;
}
I don't know if this matters but the code
Code:
void bubblesort(char letters[], int length)
has to stay exactly as it is shown according to my assignment. And I have some of the code in void bubblesort turned into comments because i'm not sure if I actually need that part or not, guessing that I do not.
Also, the errors i'm getting are
warning: passing argument 2 of 'bubblesort' makes integer from pointer without a cast
and
note: expected 'int' but argument is of type 'char *'
In summary, I think the questions I have at the moment are, what is wrong with my bubblesort and why is the code running with errors or warnings? Thanks again.