Recusion Problem-Help

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

Join Date: Sep 2009
Posts: 8
Reputation: ross42111 is an unknown quantity at this point 
Solved Threads: 0
ross42111 ross42111 is offline Offline
Newbie Poster

Recusion Problem-Help

 
0
  #1
Oct 12th, 2009
THe problem is supposed to give the recsive answer and the loop answer. The recusive anwer is always giving 0 - as its answer.and that is inccorrect It is supposed to show the steps and so is the loop answer. Can someone plase help me
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. float wk7recur (float fnum, int inum);
  6. float wk7loop (float fnum, int inum);
  7.  
  8. int main()
  9. {
  10.  
  11. float recurive_answer,loop_answer, num;
  12. int num1;
  13.  
  14.  
  15. cout<<endl;
  16. cout<<" This program calculates the recusive and interation " << endl;
  17. cout<<" version of the problem and displays the answers "<<endl;
  18.  
  19. do
  20. {
  21.  
  22. recurive_answer = wk7recur( num, num1);
  23. loop_answer = wk7loop ( num, num1);
  24.  
  25. cout <<endl <<endl;
  26. cout << " Please enter two numbers ";
  27.  
  28. cin >> num >> num1;
  29. cout <<" " << recurive_answer <<" " << loop_answer;
  30. }while (num !=0);
  31.  
  32.  
  33.  
  34. cout<<endl<<endl;
  35. cout << "Press [enter] to exit" <<endl;
  36. cin.ignore(); //needed because of keyboard input
  37. cin.get();
  38. return 0;
  39. }
  40. float wk7recur (float fnum, int inum)
  41. {
  42.  
  43. if (inum == 0)
  44. return 1;
  45. else
  46. return (fnum * wk7recur(fnum,inum -1));
  47.  
  48. }
  49. float wk7loop (float fnum, int inum)
  50. {
  51. for (fnum = 1; fnum <= inum; fnum++)
  52.  
  53. fnum = inum;
  54. return fnum;
  55. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 132
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster
 
0
  #2
Oct 12th, 2009
I think a bigger problem is that the recursive and iterative functions do not do anything remotely similar. If this program is supposed to be comparing two equivalent algorithms that solve the same problem, it is way off target.

What are these functions supposed to be doing?
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,676
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 262
Lerner Lerner is offline Offline
Posting Virtuoso
 
0
  #3
Oct 12th, 2009
Where do you give num and num1 a value before you pass it to either the recursive or the iterative process? Shouldn't line 22 and 23 be between line 28 and 29?

Expand line 46 to three lines:
double temp = fnum * wk7recur(fnum,inum -1);
cout << temp << endl;
return temp;

num1 will be an int. It will control how many recursive calls to the recursive function there will be and how many times through the loop you will need to go. To generate a new running total each time through the loop you can use a holding variable and the *= operator.

Basically you are calculating fnum raised to the inum power, or something like that, with either approach.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 8
Reputation: ross42111 is an unknown quantity at this point 
Solved Threads: 0
ross42111 ross42111 is offline Offline
Newbie Poster
 
0
  #4
Oct 12th, 2009
Originally Posted by Tom Gunn View Post
I think a bigger problem is that the recursive and iterative functions do not do anything remotely similar. If this program is supposed to be comparing two equivalent algorithms that solve the same problem, it is way off target.

What are these functions supposed to be doing?
=Both prgorams are supposed to take two numbers and give the same answer but use different calulations to comeup with the same answer, one recusive and one as a loop. I am not sure which type of loop to use and my recursive satement is not functioing correctly
: )
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 8
Reputation: ross42111 is an unknown quantity at this point 
Solved Threads: 0
ross42111 ross42111 is offline Offline
Newbie Poster
 
0
  #5
Oct 12th, 2009
Originally Posted by Lerner View Post
Where do you give num and num1 a value before you pass it to either the recursive or the iterative process? Shouldn't line 22 and 23 be between line 28 and 29?

Expand line 46 to three lines:
double temp = fnum * wk7recur(fnum,inum -1);
cout << temp << endl;
return temp;

num1 will be an int. It will control how many recursive calls to the recursive function there will be and how many times through the loop you will need to go. To generate a new running total each time through the loop you can use a holding variable and the *= operator.

Basically you are calculating fnum raised to the inum power, or something like that, with either approach.
THanks I have got the recursive part working, now I just need to try to get the correct loop to give me the same answer.
: )
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,676
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 262
Lerner Lerner is offline Offline
Posting Virtuoso
 
0
  #6
Oct 12th, 2009
Each function call in the recursive approach decreases inum by 1 until inum is zero. When inum is zero the function stops calling itself. That same control can be written into the control structure of a for loop.

The reursive function first returns 1 at the last of the function calls, when inum equals 0; then returns 1 * fnum from the next to last function call; then returns 1 * fnum * fnum from the second to last function call, then returns 1 * fnum * fnum * fnum from the next function call back, etc. So, if a holding variable, call it temp, started out with a value of 1 then each time you go through a function call you multiply the previous value of temp by fnum. This can be readily built into the body of the for loop using a single line of code and either of two syntaxes.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC