Thread: A Jumble Program in C

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    12

    A Jumble Program in C

    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.
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I can see only one error in your code.
    The call to bubblesort
    Code:
            //Initialize bubblesort array.
            bubblesort(list[i].word, list[i].sort);
    bubblesort expects the string to be sorted and the length of that string
    call it that way

    Code:
            //Initialize bubblesort array.
            bubblesort(list[i].sort, strlen(list[i].sort));
    Kurt

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Previously discussed here.

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    12
    Yes, thank you Kurt, that did remove the errors, but if you don't mind taking a look at it, or anyone else, where am I going wrong in my bubble sorting of the sorted words? I'm not sure how the terminology is exactly. But each letter in every word needs to be alphabetized.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197
    the fact is i want to know how you want to sort the words or letters. Do you want to sort the words alphabetically then print to a screen ? or do you want to sort them alphabetically and store them in the text file? this will make me help you

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    12
    I apologize, I didn't know I wasn't allowed to post the same question in different forums on different websites

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    12
    @Nyah Check,
    The words are already alphabetized in the dictionary file called jumbledct.txt that I included. I believe the assignment is asking me to copy the words into the jumble.txt file in one column, then in the column next to the copied words, have each word alpabetically sorted by letter.

  8. #8
    Registered User
    Join Date
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197
    thats not true forget about what he says and answer my question

  9. #9
    Registered User
    Join Date
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197
    ok you need pointers to start with from the first column you start comparing two strings at a time since they are already sorted in alphabetical order start copying to the next column and then strcmp the first word and second if the first is higher push it up the stack and send the one there down and in that order for all the words.

  10. #10
    Registered User
    Join Date
    Apr 2012
    Posts
    12
    Is it possible for you to give me an example without actually telling me the answer? I've looked for an example but have yet to find one in the forums. Thanks again. I'm assuming your talking about editing my bubblesort and adding strcmp between the lengths and the letters to alphabetize the letters in the words.

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Quote Originally Posted by deepdivin247 View Post
    I apologize, I didn't know I wasn't allowed to post the same question in different forums on different websites
    It's provided for context. The problem is really when people just post to multiple forums at nearly the same time without waiting for an answer at one of them.

  12. #12
    Registered User
    Join Date
    Apr 2012
    Posts
    12
    Quote Originally Posted by rags_to_riches View Post
    It's provided for context. The problem is really when people just post to multiple forums at nearly the same time without waiting for an answer at one of them.
    Okay gotcha. That wasn't my intentions, I just figured the more the merrier.

  13. #13
    Registered User
    Join Date
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197
    ok let me write an example......

  14. #14
    Registered User
    Join Date
    Apr 2012
    Posts
    12
    I started this program all wrong. I'm rewriting the whole code to read from two input files. I'm not supposed to write to any files. The code here in this thread wouldn't relate to the new code at all. How do I mark this as solved or close the thread?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  2. Replies: 5
    Last Post: 08-16-2007, 11:43 PM
  3. Replies: 18
    Last Post: 11-13-2006, 01:11 PM
  4. jumble socket data
    By stormy in forum C Programming
    Replies: 10
    Last Post: 08-23-2005, 10:07 AM
  5. How To Make The Program Installed In Program Files Folder?
    By javacvb in forum Windows Programming
    Replies: 4
    Last Post: 11-05-2003, 05:33 PM