Solved

Help With Recursion

Posted on 2003-11-12
9
353 Views
Last Modified: 2010-04-02
I'm trying to recursivly search a vector of objects for the smallest value of a field.
It should work something like this.... Say you have 8 elements

          12345678      // not 2 or 1 split vector
         1234  8765     // not 2 or 1 split vector 1234 and 5678
       12  43  87  56   // min of 1&2 , min of 3&4, min of 5&6, min of 7&8
         1 3      7  5     .....
           1         5
                1

I have these functions
FindMid(string a, string b) // checks for the smallest value (I checked this function it works)
vector<Associate> split(vector<Associate> &a) (Will split a vector that looks like 012345678 into 01234 and 8765
string MinimumObject(vector<Associate> &Members, string key)

Code I'm Trying
****************************************************************************
string MinimumObject(vector<Associate> &Members, string key, int mode)
{
      vector<Associate> Members2;
      
      if (Members.size() == 0)
            return "NULL";

      if (Members.size() == 1)
      {
           return Members[0].getKey(key);
      }

      if (Members.size() == 2)
      {
           return FindMin(Members[0].getKey(key), Members[1].getKey(key));
      }

      else
      {
           Members2 = split(Members); // Split Members vector
           return FindMin(MinimumObject(Members, key, mode), MinimumObject(Members2, key, mode));      
      }
}

string FindMin(string a, string b)
{
     bool first = (a<=b);
    if (first)
    {
      cout<<a<<" is less than "<<b<<endl;
      return a;
   }
   else
   {
                cout<<b<<" is less than "<<a<<endl;
      return b;
   }
}

vector<Associate> split(vector<Associate> &a)
{
       //define local variables
       vector<Associate> b;
       int size, count;

       Associate temp;

       size=a.size();
       size = size -1;
       count=size;
       
      size=size/2;

       while (count!=size){
         temp=a[count];
         a.pop_back();
         b.push_back(temp);
         count = count - 1;
       }
       return b;
}//end split

****************************************************************************

Something is wrong though becuase when I output what the recursive function MinimumObject returns by typing
string hold;
hold = Senate_Class.MinimumObject(Senate_Class.Members, "phone", 0);
cout<<hold<<endl;

I get a value that is not the smallest

however

the function calls FindMin several times and the last comparison that function makes is always the minimum.

So bottom line for some reason the FindMin function can locate the minimum value in the vector but the recusive function does not return it..




0
Comment
Question by:RiMZ
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 6

Expert Comment

by:GloomyFriar
Comment Utility
Could you show all FindMin output?
0
 
LVL 11

Expert Comment

by:bcladd
Comment Utility
You say "the last comparison that function makes is always the minimum". There should be a sequence of comparisons with the minimum, right? 3 comparisons with 1 if there were 8 entries initially (the log base 2 of the size of the original array, give or take).

Are you sure the returned value is not the smallest? That is you are comparing strings so if a string contains non-digits (or if the lengths of the keys are not identical) the comparison may appear to sort in an odd way. You may want to print hold inside of quotes as in cout << "\"" << hold << "\"" << endl so you can see if there are any spaces at the front of the string.

All of this indicates that I don't see anything immeadiately and cannot run your code so there is noting I can do but suggest tests to you.

Good luck,
-bcl
PS: Mode is never used in MinimumObject (or at least the version we were shown). IThere are several studies that corelate unused variables/parameters with errors in code (it is not an error in and of itself).
0
 

Author Comment

by:RiMZ
Comment Utility
the output created by FindMin is....


(202) 224-2441 is less than (202) 224-3441
(202) 224-2441 is less than (202) 224-5852
(202) 224-2644 is less than (202) 224-4343
(202) 224-2644 is less than (202) 224-3954
(202) 224-2441 is less than (202) 224-2644
(202) 224-2921 is less than (202) 224-3521
(202) 224-2921 is less than (202) 224-4451
(202) 224-2531 is less than (202) 224-5641
(202) 224-2531 is less than (202) 224-5054
(202) 224-2531 is less than (202) 224-2921
(202) 224-2441 is less than (202) 224-2523
(202) 224-5047 is less than (202) 224-5521
(202) 224-5042 is less than (202) 224-5721
(202) 224-4623 is less than (202) 224-6521
(202) 224-3553 is less than (202) 224-4623
(202) 224-3553 is less than (202) 224-5042
(202) 224-5444 is less than (202) 224-5623
(202) 224-2651 is less than (202) 224-5444
(202) 224-4024 is less than (202) 224-5941
(202) 224-4944 is less than (202) 224-6361
(202) 224-4024 is less than (202) 224-4944
(202) 224-2651 is less than (202) 224-4024
(202) 224-2651 is less than (202) 224-3553
(202) 224-2441 is less than (202) 224-2651
0
 

Author Comment

by:RiMZ
Comment Utility
In main I code this...

      string hold;
      hold = Senate_Class.MinimumObject(Senate_Class.Members, "phone", 0);
      cout<<":"<<hold<<":"<<endl;

This couts this

(202) 224-4944 is less than (202) 224-6361
:(202) 224-4944:


why is (202) 224-4944 coming up instead of (202) 224-2441
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 11

Expert Comment

by:bcladd
Comment Utility
You are testing your find min routine BEFORE your assignment to hold?

You only show one line of comparison (an odd line of comparison) before you show the return value. Your MinimumObject is DESTRUCTIVE and if you don't make a COPY of the input data that data will be destroyed (or rather reduced to a two element array: bet those two phone numbers are the first numbers in your original list).

-bcl
0
 
LVL 9

Accepted Solution

by:
_ys_ earned 500 total points
Comment Utility
> string MinimumObject(vector<Associate> &Members, string key)
Try removing the pass-by-reference functionality.

The left and right code paths are likely _confused_:
> return FindMin(MinimumObject(Members, key, mode), MinimumObject(Members2, key, mode));
0
 
LVL 11

Expert Comment

by:bcladd
Comment Utility
Looking at the output, that seems to be the case. My suggestion: Change split so it takes three vectors, the original and references to the first and second half. Then take the original apart into the other two vecotrs.
Then in MinimumObject you have two vectors declared locally and make the call to split as:

split(Members, frontOfMembers, backOfMembers);

and then pass front and back into the recursive call.

If I haven't explained myself clearly, please feel free to follow up here.

-bcl
0
 

Author Comment

by:RiMZ
Comment Utility
THANK F***IN U!!!!!!!!!!!!!!!!!!  LOL THANK YOU THANK YOU THANK YOU.

I JUST SPENT 7 HOURS STRIGHT TRYING TO FIGURE OUT WHAT WAS WRONG IT WAS THE "&"
0
 
LVL 9

Expert Comment

by:_ys_
Comment Utility
7 hours ... world time zones are great.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now