How to capture a string into variable in a recursive function in C++?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2009
Posts: 3
Reputation: gundalav is an unknown quantity at this point 
Solved Threads: 0
gundalav gundalav is offline Offline
Newbie Poster

How to capture a string into variable in a recursive function in C++?

 
0
  #1
24 Days Ago
I tried to print all the possible combination of members of several vectors. Why
the function below doesn't return the string as I expected?

  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4. #include <sstream>
  5. using namespace std;
  6.  
  7. string EnumAll(const vector<vector<string> > &allVecs, size_t vecIndex, string
  8. strSoFar)
  9. {
  10. string ResultString;
  11. if (vecIndex >= allVecs.size())
  12. {
  13. //cout << strSoFar << endl;
  14. ResultString = strSoFar;
  15. return ResultString;
  16. }
  17. for (size_t i=0; i<allVecs[vecIndex].size(); i++) {
  18. strSoFar=EnumAll(allVecs, vecIndex+1, strSoFar+allVecs[vecIndex][i]);
  19. }
  20. ResultString = strSoFar;
  21. return ResultString;
  22.  
  23. }
  24.  
  25.  
  26. int main ( int arg_count, char *arg_vec[] ) {
  27.  
  28. vector <string> Vec1;
  29. Vec1.push_back("T");
  30. Vec1.push_back("C");
  31. Vec1.push_back("A");
  32.  
  33. vector <string> Vec2;
  34. Vec2.push_back("C");
  35. Vec2.push_back("G");
  36. Vec2.push_back("A");
  37.  
  38. vector <string> Vec3;
  39. Vec3.push_back("C");
  40. Vec3.push_back("G");
  41. Vec3.push_back("T");
  42.  
  43.  
  44. vector <vector<string> > allVecs;
  45.  
  46. allVecs.push_back(Vec1);
  47. allVecs.push_back(Vec2);
  48. allVecs.push_back(Vec3);
  49.  
  50.  
  51.  
  52.  
  53. string OutputString = EnumAll(allVecs,0,"");
  54.  
  55. // print the string or process it with other function.
  56. cout << OutputString << endl; // This prints nothing why?
  57.  
  58. return 0;
  59. }
The expected output is:

  1. TCC
  2. TCG
  3. TCT
  4. TGC
  5. TGG
  6. TGT
  7. TAC
  8. TAG
  9. TAT
  10. CCC
  11. CCG
  12. CCT
  13. CGC
  14. CGG
  15. CGT
  16. CAC
  17. CAG
  18. CAT
  19. ACC
  20. ACG
  21. ACT
  22. AGC
  23. AGG
  24. AGT
  25. AAC
  26. AAG
  27. AAT
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 48
Reputation: pecet is an unknown quantity at this point 
Solved Threads: 10
pecet pecet is offline Offline
Light Poster
 
0
  #2
24 Days Ago
  1. cout << OutputString << endl; // This prints nothing why?
It prints.
That's the output I got, when I launched your program:
  1. TCCGTGCGTACGTCCCGTGCGTACGTACCGTGCGTACGT
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 3
Reputation: gundalav is an unknown quantity at this point 
Solved Threads: 0
gundalav gundalav is offline Offline
Newbie Poster
 
0
  #3
24 Days Ago
Originally Posted by pecet View Post
  1. cout << OutputString << endl; // This prints nothing why?
It prints.
That's the output I got, when I launched your program:
  1. TCCGTGCGTACGTCCCGTGCGTACGTACCGTGCGTACGT
I mean it doesn't print the output as I expected, i.e. list of 27 length 3 strings.

I have corrected the comment in the code.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 25
Reputation: Sodabread is an unknown quantity at this point 
Solved Threads: 1
Sodabread's Avatar
Sodabread Sodabread is offline Offline
Light Poster
 
0
  #4
24 Days Ago
The problem is the setting of strSoFar to whatever EnumAll returns and changing what the previous function needs to have in that variable.

Not setting strSoFar in your for loop gives you this effect:

Initial function call: strSoFar = ""
1st recurse: strSoFar = "T"
2nd recurse: strSoFar = "TC"
1st 3rd recurse: strSoFar = "TCC" (print from if statement)
Fall back to 2nd w/out setting strSoFar: strSoFar = "TC"
2nd 3rd recurse: strSoFar = "TCG" (print from if statement)
Fall back to 2nd w/out setting strSoFar: strSoFar = "TC"
3rd 3rd recurse: strSoFar = "TCT" (print from if statement)

Sorry if my numbering is confusing. It's early and I couldn't come up with anything better =)

Recursion is really tricky when you're first starting out, but keep working at it and it will become almost second nature.

Hope that helps you.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 25
Reputation: Sodabread is an unknown quantity at this point 
Solved Threads: 1
Sodabread's Avatar
Sodabread Sodabread is offline Offline
Light Poster
 
0
  #5
24 Days Ago
Double posted. My bad.
Last edited by Sodabread; 24 Days Ago at 9:21 am. Reason: Double Post
Reply With Quote Quick reply to this message  
Reply

Tags
c++, function, recursion

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC