Like Tree4Likes
  • 1 Post By Adak
  • 2 Post By AndiPersti
  • 1 Post By Adak

Wordsearch Puzzle Help

This is a discussion on Wordsearch Puzzle Help within the C Programming forums, part of the General Programming Boards category; Hello folks, I've been having some trouble with a school assignment and I wondered if you could help me. The ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    9

    Angry Wordsearch Puzzle Help

    Hello folks, I've been having some trouble with a school assignment and I wondered if you could help me.
    The program that I have to create must read a NxM character matrix that contains a certain number of words (N<=10 and M<=10) and a list of these words, and then find the words in the matrix replacing the characters that don't belong to the provided words by the character "#".

    input:
    • the number of lines and columns of the matrix (N, M)
    • the matrix itself;
    • the number P of words;
    • the list of words.

    output:
    • a NxM matrix with the characters that don't belong to the provided words replaced by #


    I need to create a function that finds strings along the rows of the matrix, a function that replaces characters by #, and a function that inverts the order of the characters in a string (words can be displayed backwards in the puzzle, horizontally or vertically).

    So, can anyone give some ideas? Thanksss!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, anpaolo!

    How about starting with the input() function to get all the parameters for the matrix, and words? Check it out so you know it's working correctly. Then you will need to print up the matrix, so a printIt() function is essential. You'll need a function to get all the valid letter combo's, and then reverse them, as well.

    Take it one function at a time, and check it that it's OK, before you go onto the next one.

    It's VERY much up to you to get this started, and know all the requirements (like what about letters that go off at diagonals, do they need to be checked as well?), and to show us the part of your code that you are having trouble with, when you get stuck. We can't work from descriptions of code.

    And use the code tags, so your code won't get mangled by the forum software (it squishes it to the left hand side). Code tag icon is ONLY found in the advanced editor, but you can always type it yourself as well:

    [*code]
    Just remove the star * from these forum tags, to make them into code tags.
    [*/code]
    Salem likes this.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Adak View Post
    [*code]
    Just remove the star * from these forum tags, to make them into code tags.
    [*/code]
    There's a noparse-tag which allows you to use any other tag within your text (e.g. for explanations):

    [code]
    // here comes your
    // code lines
    [/code]


    Bye, Andreas
    Salem and Adak like this.

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    9
    I'm at odds with the the part in which the program is supposed to scanf the matrix/array.

    I tried
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAX 10
    
    int main() {
      char puzzle[MAX][MAX];
      int m, n, i, j;
    
      scanf("%d %d", &m, &n);     
      m++; n++;
    
    
        for(i=0; i<m; i++)
            for(j=0; j<n; j++)
                scanf("%c", &puzzle[i][j]);
    
    return 0;
    }
    Is that even right? Thanks!

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    9
    I guess the code for inputting the matrix is okay, but I can't get to printf the characters properly:

    Code:
    ...
        for(i=0; i<m; i++) {
            for(j=0; j<n; j++)
                printf("%c ", puzzle[i][j]);
            printf("\n");
        }
    ...
    The characters don't appear organized:

    Input:
    3 3
    a f s
    s j e
    ! s g

    Output:
    a f
    s
    s
    j e


    ! s


  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In the second scanf(), add one space, just before the % operator:
    Not
    Code:
    scanf("%c", &puzzle[i][j]);
    
    but
    scanf(" %c", &puzzle[i][j]);
    After you get this input block of code working correctly and tested, I'd set your puzzle values either by loading the chars in from a file, or set the automatically in the program, for now.

    You can save a LOT of time if, while the rest of the program is being written and tested, you don't have to re-enter the chars, every time you start the program.
    Last edited by Adak; 10-14-2012 at 12:12 PM.
    Salem likes this.

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    9
    Great, thank you! Now, my program needs a list of words as part of the input. I need to provide the number of words, P, and it should allow me to enter P words. How to do that?

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by anpaolo View Post
    Great, thank you! Now, my program needs a list of words as part of the input. I need to provide the number of words, P, and it should allow me to enter P words. How to do that?
    With programming, the question is "How do you WANT to do that?".

    Three common ways of getting input:
    1) From the user - but we don't want that just now
    2) From a file located in the same directory as the program.
    3) Fixed into the program itself:
    Code:
    words[5][5]={
    {"sails"},
    {"color"},
    {"fixed"},
    {"horse"},
    {"house"}
    };
    Should work for a row test for words.

    Did you get an answer to whether you need to check for words on the diagonal directions?

  9. #9
    Registered User
    Join Date
    Oct 2012
    Posts
    9
    Quote Originally Posted by Adak View Post
    With programming, the question is "How do you WANT to do that?".

    Three common ways of getting input:
    1) From the user - but we don't want that just now
    2) From a file located in the same directory as the program.
    3) Fixed into the program itself:
    Code:
    words[5][5]={
    {"sails"},
    {"color"},
    {"fixed"},
    {"horse"},
    {"house"}
    };
    Should work for a row test for words.

    Did you get an answer to whether you need to check for words on the diagonal directions?
    No diagonals.

    I will create a double-dimension array for the words, then. Then I'll try to find out how to find the words in the puzzle, especially for those who share letters. Again, thank you very much!

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    One note: Whatever m and n are in your program, I would make your words[][] with n+1 char's instead of n (as I showed above). You need to leave 1 char more, as a space for the end of string char to occupy.

  11. #11
    Registered User
    Join Date
    Oct 2012
    Posts
    9
    The way I'm using to fit the words in an array is "scanfying" each letter at a time, i.e.

    words: GLASS, THREE, NINE
    words[1][1] = 'G', words[2][3] = 'R'; words[3][4] = 'E'...

    But I want my program to read words until I press Enter, that is, words[i][j] == 10 (ASCII correspondant to Enter). I wanted to use a break statement for that command. Unfortunately, this command exits both "for" loops, when I just want it to exit the inner loop. Any suggestions?

    Code:
    char words[MAX][MAX+1];
        int P;
        scanf("%d", &P);
    
    
        for(i=0; i<P; i++) {
            for(j=0; j<MAX; j++) {
                scanf("%c", &words[i][j]);
                if(words[i][j] == 10) break;
            }
        }

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by anpaolo View Post
    The way I'm using to fit the words in an array is "scanfying" each letter at a time, i.e.

    words: GLASS, THREE, NINE
    words[1][1] = 'G', words[2][3] = 'R'; words[3][4] = 'E'...

    But I want my program to read words until I press Enter, that is, words[i][j] == 10 (ASCII correspondant to Enter). I wanted to use a break statement for that command. Unfortunately, this command exits both "for" loops, when I just want it to exit the inner loop. Any suggestions?

    Code:
    char words[MAX][MAX+1];
        int P;
        scanf("%d", &P);
    
    
        for(i=0; i<P; i++) {
            for(j=0; j<MAX; j++) {
                scanf("%c", &words[i][j]);
                if(words[i][j] == 10) break;
            }
        }
    You are given the # of rows, and the number of columns, so entering letter by letter is simply VERY tedious.

    Remove the inner loop, and scanf("%s", words[i]) <==note no &

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 3D wordsearch.
    By jh294 in forum C Programming
    Replies: 12
    Last Post: 04-08-2011, 06:45 PM
  2. wordsearch
    By egomaster69 in forum C Programming
    Replies: 4
    Last Post: 01-23-2005, 10:55 PM
  3. wordsearch
    By linuxdude in forum C Programming
    Replies: 2
    Last Post: 02-20-2004, 09:13 PM
  4. wordsearch solver, need help
    By lakai02 in forum C Programming
    Replies: 3
    Last Post: 01-14-2003, 02:55 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21