Thread: Finding Words in a array[size][size]

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    21

    Question Finding Words in a array[size][size]

    Hi there! I need some help to find words in a array[size][size]
    Just like the game soup of letter. It has to find words in vertical, horizontal, diagonal and random positions.
    Example of random:

    EDIT:
    ___W
    ____O__TO
    _____RD___FI
    ___________ND

    The problem is that i can find horizontal, vertical and diagonal but not random ones. Please help me!!
    Last edited by ^DJ_Link^; 03-07-2006 at 11:50 AM.

  2. #2
    Registered User
    Join Date
    Mar 2006
    Location
    Turkey
    Posts
    5
    Hi...
    if u write your code, it will be better for helping...
    Thanks...

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Do you know what words you're looking for?

    I would suggest using a recursive function.

    Post your code, as suggested.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Jan 2004
    Posts
    21
    Yes i do know what words im looking for. U tought of recursion also, the problem is that i know well how to work with it.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I would use a recursive function that looks in all directions for the words you're looking for. You would call the function on all elements of the array.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Jan 2004
    Posts
    21
    I've tought in that also, the question is. Imagine that you find 3 correct letters and find a dead end. but in another direccion that you haven't choose there is the rest??

  7. #7
    Registered User
    Join Date
    Jan 2004
    Posts
    21
    oh sorry, here's the code. This one finds in all directions except for the random


    Code:
         do
        {
         i=0;
         j=0;
         x=pos_x;
         y=pos_y;
         encontrou=0;
         nletras=0;
         do{
             encontrou=0;      
             if(sopa[x][y]==palavras[pos_palavra].palavra[i]){
                 palavras[pos_palavra].ponteiro[i]=&sopa[x][y];
                  i++;
                  nletras++;
                 
                 encontrou=1;
             
                   switch(vez){
                         case 0: x--; break;        /* Cima */
                         case 1: {x--; y++; }break; /* Diagonal Cima Direita */ 
                         case 2: y++; break;        /* Direita */
                         case 3: {x++; y++; }break; /* Diagonal Baixo Direita */
                         case 4: x++; break;        /* Baixo */
                         case 5: {x++; y--; }break; /* Diagonal Baixo Esquerda*/            
                         case 6: y--; break;        /* Esquerda */
                         case 7: {x--; y--; }break; /* Diagonal Cima Esquerda */
                     }                
             }
    
              
             if(nletras==strlen(palavras[pos_palavra].palavra)-1){
                palavras[pos_palavra].encontrou=1;
                printf("Encontrou a palavra %s\n", palavras[pos_palavra].palavra);
             }
                   
         }
         while(encontrou==1 && palavras[pos_palavra].encontrou==0 && x>=0 && x<tamsopa && y>=0 && y<tamsopa);
         vez++;
        } 
        while (vez<8 && palavras[pos_palavra].encontrou==0);

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Try something like this (add bounds checking):
    Code:
    int find_word(int xp, int yp, int letter) {
        if(letter == length) return 1;
    
        if(array[xp][yp] != word[letter]) return 0;
    
        if(find_word(xp+1, yp, letter+1)) return 1;
        if(find_word(xp-1, yp, letter+1)) return 1;
        /* ... */
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Jan 2004
    Posts
    21
    thank you! it's now fully working =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginners Contest #2 For those who wanted more!!
    By ILoveVectors in forum Contests Board
    Replies: 16
    Last Post: 08-12-2005, 12:03 AM
  2. Problem with malloc() and sorting words from text file
    By goron350 in forum C Programming
    Replies: 11
    Last Post: 11-30-2004, 10:01 AM
  3. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 08:00 PM
  4. using strlen and finding shortest and longest words
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 09-30-2001, 06:09 PM