944,122 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 366
  • C++ RSS
Oct 12th, 2009
0

Recusion Problem-Help

Expand Post »
Quote ...
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
C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ross42111 is offline Offline
8 posts
since Sep 2009
Oct 12th, 2009
0
Re: Recusion Problem-Help
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?
Reputation Points: 1446
Solved Threads: 135
Practically a Master Poster
Tom Gunn is offline Offline
681 posts
since Jun 2009
Oct 12th, 2009
0
Re: Recusion Problem-Help
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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Oct 12th, 2009
0
Re: Recusion Problem-Help
Click to Expand / Collapse  Quote originally posted by Tom Gunn ...
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?
Quote ...
=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
: )
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ross42111 is offline Offline
8 posts
since Sep 2009
Oct 12th, 2009
0
Re: Recusion Problem-Help
Click to Expand / Collapse  Quote originally posted by Lerner ...
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.
Quote ...
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.
: )
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ross42111 is offline Offline
8 posts
since Sep 2009
Oct 12th, 2009
0
Re: Recusion Problem-Help
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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Account Class
Next Thread in C++ Forum Timeline: Beginers Basic Account Program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC