[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


C++ Recursive Search

Posted on 2007-08-07
Medium Priority
Last Modified: 2013-11-27
Greetings experts,
I am currently helping my nephew with a C++ class, though my C++ is marginal at best, and I must admint I'm a bit stumped on a question he has for an assingment. The question is to write a recursive sequential search function for a linked list and an array. The thing that is troubling me is that the functions prototype is to only pass to values, an int for the value to search for and a bool item to mark the search as true/false. So the prototype looks like..
void ItemType::Look(int val, bool& gotit)
I'm stumped to code this recursively..
P.S. I plan to help him not give the answer to him.. HA HA
Question by:PIXADMIN
  • 2
LVL 11

Accepted Solution

alexcohn earned 200 total points
ID: 19652295
An item in a list has a pointer to the next item, e.g.

class ItemType
  ItemType* m_pNext;
  int m_Value;

The function will be something like:

void ItemType::Look(int val, bool& gotit)
  if (m_Value == val)
    gotit = true;

  // here comes recursion:
  if (m_pNext)
    return m_pNext->Look(val, gotit):

In an array, the pointer to next item can be retrieved as follows:

ItemType* pNext = this+1;

But, without additional information, in plain C++ you cannot know where the array ends.
LVL 11

Expert Comment

ID: 19816352
If the author does not object, my answer should be considered as a solution.

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question