• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 370
  • Last Modified:

LOOP NOT LOOPING

can anyone tell me whats wrong with this loop? it executes once but it does not repeat what its supposed to do...(search the word search puzzle diagonally at the upper part)... pls help me! tnx!


for(l2=1; l2<=cols; l2++)
    {
      lastcc = l2;
           
      for(l1 = 1; l1<=rows; l1++)
      {
       rowc = l1;
       ctr = 1;
       
       if(l1 = 1)
       {
        colc = l2;
       }
       
       else
       {
        lastcc++;
        colc = lastcc;
       }
       
       while((ctr <= sl) && (rowc<=rows) && (colc<=cols))
       {
        cword[ctr-1]=puzzle[rowc-1][colc-1];
        rowc++;
        colc++;
        ctr++;
       }
       
       for (z=0;z<=strlen(sword)-1;z++)
       {
        lsword[z]=tolower(sword[z]);
       }
       
       for (z=0;z<=strlen(cword)-1;z++)
       {
        lcword[z]=tolower(cword[z]);
       }
       
       if (strcmp(lcword, lsword) == 0)
       {
        for(b=l1, c=l2; (b<=l1+sl-1)&& (c<=l2+sl1);      b++, c++)
         {
           found = 0;
           printf("\t%c (%d,%d)", puzzle[b-1][c-1], b, c);
           printf("\n");
         }
       }
       
     }//for l1
   
   }//for l2
   
if(found != 0)
{
 printf("\nnot found!\n");
}
0
arabo
Asked:
arabo
  • 2
1 Solution
 
grueCommented:
Which loop? :)

I see one problem:

 if(l1 = 1)

I don't think you want that to be an assignment.  You probably want:

 if(l1 == 1)

I didn't walk through the logic of your function because I don't know what its goal is...  If you explain what your goal is maybe other problems might manifest themselves...  

For instance, is it necessary to have:

    for (z=0;z<=strlen(sword)-1;z++)
      {
       lsword[z]=tolower(sword[z]);
      }

so deep inside your loops?  Looks like you do it over and over and over again unnecessarily.  

Also, you should probably be null terminating your cword string:

      cword[ctr-1]=puzzle[rowc-1][colc-1];
      cword[ctr] = '\0';  // add this

if you're going to be using strcmp() on the string.
0
 
araboAuthor Commented:
i do not know which loop it stops...but the goal is... to search a word in a word search puzzle...my program already works when it comes to searching vertically and horizontally...the one i posted is just one part of the diagonal search...it would search from left to right... upper part of the puzzle... i followed your tip of making if(l1=1) to if(l1 == 1)...it loops now but the new problem is...it prints out letters it shouldn't be printing... here is an example...

if this is the puzzle...

 ab
 de
 


and i want to search for... 'ae', and 'b'....

it would say...

"ae" --> a(1,1) e(2,2) <-- (row, column)
 
the first one is always correct..but when it goes to the second one it becomes...

"b" ---> b(1,2) e(2,2)

...'e' shouldn't be included...but it still prints it...

 
 
0
 
araboAuthor Commented:
the only problem is when i search for the letter in the corner...

if this is the word search puzzle

a b c d e
f g h i j
k l m n o


if search for the letter 'e'...it would print...

e(3,5)
j(4,5)
o(5,5)


...it prints the last column...
0
 
jmcgOwnerCommented:
Nothing has happened on this question in over 10 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by grue (even if it is homework).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now