LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-27-2005, 04:28 PM   #1
Mistro116@yahoo.com
Member
 
Registered: Sep 2005
Posts: 118

Rep: Reputation: 15
Recursive Question


I've gotten to a stage in my program where I'm testing runtime efficiency and I want to write a recursive function that will search a 2-dimensional array of characters for a words.

I'm trying to write the function for SearchLeft, where it will search entirely to the left throughout the entire grid of characters until it either finds the word or it doesn't and changes the value of its starting row and column; however, it's not finding the word, so I think there may be a few problems with it.

Here is my code so far:

Code:
int RecursiveSearchLeft (char letters
			 [LETTERARRAYROWSIZE][LETTERARRAYCOLUMNSIZE],
			 int row, int column, WORD *words,
			 int wordIndex, int characterIndex)
{
   if (words [wordIndex].word [characterIndex] ==
       letters [row][column])
   {
      if (characterIndex == 0)
      {
	 words [wordIndex].startRow = row;
	 words [wordIndex].startCol = column;
      }
      
      characterIndex++;
      
      if (characterIndex < strlen (words [wordIndex].word))
      {
	 RecursiveSearchLeft (letters, row, column - 1, words,
			      wordIndex, characterIndex);
      }
      
      return 1;
   }
   else if (column == NOBUFFERSTARTCOLUMN && row <= NOBUFFERENDROW)
   {
      RecursiveSearchLeft (letters, row + 1, column + 14, words,
			   wordIndex, characterIndex);
      
      return 0;
   }
   else
   {
      RecursiveSearchLeft (letters, row, column - 1, words,
			   wordIndex, characterIndex);
   }
   
   return 0;
}
The sample 2-d array would be:

Code:
 EHUKXDICNMYTYLF 
 WOCJSKLTIMHONII 
 NOTGNIHSAWZIFNP 
 IRBWNIIWPPIERCE 
 BMETVELIIOKOWOK 
 QUOWCLEVELANDLN 
 VNCCOORTHTSNUNA 
 AMKHRHFELSIONOM 
 DULNAANETXUQNSU 
 AIOATNVEORKBARR 
 MMPZVEANSUALBET 
 SYSDSWGNBITCLFI 
 OHZOMYDENNEKWFD 
 IAOTNARGRNVTFEF 
 KRRVFVEEDMBDMJZ
Specifically, I'm testing this by searching for the word GRANT, which is on row 14, col 8.

What is wrong with this code? Can anyone help? It's urgent.

Thanks once more,
Mistro116

Edit: The 2-d array has a buffer around it:

Code:
XXXXXXXXXXXXXXXXX
XEHUKXDICNMYTYLFX
XWOCJSKLTIMHONIIX
XNOTGNIHSAWZIFNPX
XIRBWNIIWPPIERCEX
XBMETVELIIOKOWOKX
XQUOWCLEVELANDLNX
XVNCCOORTHTSNUNAX
XAMKHRHFELSIONOMX
XDULNAANETXUQNSUX
XAIOATNVEORKBARRX
XMMPZVEANSUALBETX
XSYSDSWGNBITCLFIX
XOHZOMYDENNEKWFDX
XIAOTNARGRNVTFEFX
XKRRVFVEEDMBDMJZX
XXXXXXXXXXXXXXXXX

Last edited by Mistro116@yahoo.com; 11-27-2005 at 04:43 PM.
 
Old 11-27-2005, 05:29 PM   #2
Mistro116@yahoo.com
Member
 
Registered: Sep 2005
Posts: 118

Original Poster
Rep: Reputation: 15
This is some updated code, but it still does not function, but takes care of a few issues:

Code:
int RecursiveSearchLeft (char letters
			 [LETTERARRAYROWSIZE][LETTERARRAYCOLUMNSIZE],
			 int row, int column, WORD *words,
			 int wordIndex)
{
   int characterIndex = 0;
   
   if (letters [row][column] ==
       words [wordIndex].word [characterIndex] &&
       characterIndex <= strlen (words [wordIndex].word))
   {
      if (characterIndex == 0)
      {
	 words [wordIndex].startRow = row;
	 words [wordIndex].startCol = column;
      }
      
      characterIndex++;
      
      if (characterIndex == (strlen (words [wordIndex].word) - 1))
      {
	 return 1;
      }
      
      RecursiveSearchLeft (letters, row, column - 1, words,
			   wordIndex);
   }
   else if (column > NOBUFFERSTARTCOLUMN)
   {
      RecursiveSearchLeft (letters, row, column - 1, words,
			   wordIndex);
   }
   else if (column == NOBUFFERSTARTCOLUMN && row < NOBUFFERENDROW)
   {
      RecursiveSearchLeft (letters, row + 1, NOBUFFERSTARTCOLUMN,
			   words, wordIndex);
   }
   
   words [wordIndex].startRow = 0;
   words [wordIndex].startCol = 0;
   
   return 0;
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Recursive compilation Genjix Programming 1 12-07-2004 07:23 AM
Recursive Servers basic question in DNS emailssent Linux - Networking 1 10-02-2004 10:01 AM
rm -r what is recursive wogga Linux - Software 3 05-28-2004 02:29 PM
Recursive Function TriggerJ Programming 4 02-02-2004 08:58 PM
recursive downloads watashiwaotaku7 Linux - General 4 03-08-2003 09:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration