Recursive program issue

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

Join Date: Nov 2008
Posts: 15
Reputation: nedsnurb is an unknown quantity at this point 
Solved Threads: 0
nedsnurb nedsnurb is offline Offline
Newbie Poster

Recursive program issue

 
0
  #1
Nov 19th, 2008
Hi
Have written this code for a program that requires the user to either enter 1 or 2 dependant on whether the program solves the problem of finding the divisors of any given to numbers by recursive or iteration methods.

  1.  
  2. #include<iostream>
  3.  
  4. using namespace std;
  5.  
  6. int recursiveGCD(int ,int );
  7. int iterativeGCD(int ,int );
  8. int input;
  9.  
  10. void main()
  11.  
  12. {
  13. int x,y;
  14. cout<<"Please enter the two numbers : ";
  15. cin>>x>>y;
  16. cout<<"Enter 1 for recursive calculation, 2 for iterative";
  17. cin>>input;
  18. {
  19. if input == 1
  20. return recursiveGCD;
  21. if input == 2
  22. return iterativeGCD;
  23. }
  24. cout<<"The Greatest Common Divisor of ("<<x<<","<<y<<") = " << recursiveGCD(x,y)<<endl;
  25. }
  26.  
  27.  
  28. int recursiveGCD(int x,int y)
  29. {
  30. if(y>x)return recursiveGCD(y,x);
  31. if(x==y)return x;
  32. if(x%y==0)return y;
  33. return recursiveGCD(x,x-y);
  34.  
  35. }
  36.  
  37. int iterativeGCD(int x,int y)
  38. {
  39. if (y == 0)
  40. return x;
  41. else
  42. return iterativeGCD(y, x % y);
  43. }

However I am getting the error C2061, saying that input is undeclared when I seem to have to declared it at the top??

Many Thanks
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 29
Reputation: Lokolo is an unknown quantity at this point 
Solved Threads: 1
Lokolo Lokolo is offline Offline
Light Poster

Re: Recursive program issue

 
0
  #2
Nov 19th, 2008
Why don't you just declare it in main? (like you have with x and y)
Last edited by Lokolo; Nov 19th, 2008 at 12:06 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 118
Reputation: chococrack is on a distinguished road 
Solved Threads: 14
chococrack's Avatar
chococrack chococrack is offline Offline
Junior Poster

Re: Recursive program issue

 
0
  #3
Nov 19th, 2008
  1. int recursiveGCD(int ,int );
  2. int iterativeGCD(int ,int );
  3. int input;

The first two are fine (function prototypes).

However the last line
int input;
when I was first reading I was thinking perhaps it was meant to be a function. If that was the original intention, then you would have needed the necessary parenthesis to have it appear as a function
  1. int input();

HOWEVER, the way you're using it you want to go with what Lokolo said in the above post, declare input in your main function
  1. int main
  2. {
  3. int input;
  4. ...
  5. }
I would love to change the world, but they won't give me the source code
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 15
Reputation: nedsnurb is an unknown quantity at this point 
Solved Threads: 0
nedsnurb nedsnurb is offline Offline
Newbie Poster

Re: Recursive program issue

 
0
  #4
Nov 19th, 2008
hi guys thanks for the quick replies however I did try this and it still throws up the error even when it is changed?

  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. int recursiveGCD(int ,int );
  6. int iterativeGCD(int ,int );
  7.  
  8. int main()
  9. {
  10. int input;
  11. int x,y;
  12. cout<<"Please enter the two numbers : ";
  13. cin>>x>>y;
  14. cout<<"Enter 1 for recursive calculation, 2 for iterative";
  15. cin>>input;
  16.  
  17. {
  18. if input == 1
  19. return recursiveGCD;
  20. if input == 2
  21. return iterativeGCD;
  22. }
  23.  
  24. cout<<"The Greatest Common Divisor of ("<<x<<","<<y<<") = " << recursiveGCD(x,y)<<endl;
  25. }
  26.  
  27.  
  28. int recursiveGCD(int x,int y)
  29. {
  30. if(y>x)return recursiveGCD(y,x);
  31. if(x==y)return x;
  32. if(x%y==0)return y;
  33. return recursiveGCD(x,x-y);
  34.  
  35. }
  36.  
  37. int iterativeGCD(int x,int y)
  38. {
  39. if (y == 0)
  40. return x;
  41. else
  42. return iterativeGCD(y, x % y);
  43. }
Last edited by nedsnurb; Nov 19th, 2008 at 12:30 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 29
Reputation: Lokolo is an unknown quantity at this point 
Solved Threads: 1
Lokolo Lokolo is offline Offline
Light Poster

Re: Recursive program issue

 
0
  #5
Nov 19th, 2008
1. Next time try to tell us which line it is.

2. If( ) has brackets.

i.e.

if input == 1
return recursiveGCD;
if input == 2
return iterativeGCD;

Should be

if (input == 1)
return recursiveGCD;
if (input == 2)
return iterativeGCD;
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 182
Reputation: mrboolf will become famous soon enough mrboolf will become famous soon enough 
Solved Threads: 18
mrboolf mrboolf is offline Offline
Junior Poster

Re: Recursive program issue

 
0
  #6
Nov 19th, 2008
That's not where the problem lies.
  1. {
  2. if input == 1
  3. return recursiveGCD;
  4. if input == 2
  5. return iterativeGCD;
  6. }
is wrong both from the syntax point of view and logically. You miss some parentheses and you try to call iterativeGCD and recursiveGCD (that are functions) without passing them their arguments (x and y in this case). You should call a function like this recursiveGCD(x, y) . Then I think you don't want to return their output: in fact you try to print it with cout just in the following line. You should store their returned value in a variable and then print that value or directly put the cout instruction inside the if.

Just another thing: both your function are recursive, even the one named iterativeGCD.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 15
Reputation: nedsnurb is an unknown quantity at this point 
Solved Threads: 0
nedsnurb nedsnurb is offline Offline
Newbie Poster

Re: Recursive program issue

 
0
  #7
Nov 19th, 2008
thanks for your help, think i'll go back to the drawing board really! cheers
Last edited by nedsnurb; Nov 19th, 2008 at 12:46 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 118
Reputation: chococrack is on a distinguished road 
Solved Threads: 14
chococrack's Avatar
chococrack chococrack is offline Offline
Junior Poster

Re: Recursive program issue

 
0
  #8
Nov 19th, 2008
A common main function appears as
  1. int main()
  2. {
  3. // code
  4. return 0;
  5. }

Yours appears like this:
  1. int main()
  2. {
  3. // little bit of code
  4. { // unecessary brackets
  5. if(some contidion)
  6. return Function1; // trying to return a value
  7. else
  8. return Function2; // trying to return a value
  9. } // more unessary brackets
  10. // more code
  11. } // oops no return 0

You need to take out the brackets and give the functions something to store the values in (and pass their parameters.

  1. int main()
  2. {
  3. int getRecurs, getItera;
  4. // ...
  5. if (input==1)
  6. getRecurs = recursiveGCD(PUT PARAMETERS!!!);
  7.  
  8. if (input == 2)
  9. getItera = iterativeGCD(PARAMETERS);
  10. // ....
  11. return 0;
  12. }


I still love you, even if no one else does
Last edited by chococrack; Nov 19th, 2008 at 12:52 pm.
I would love to change the world, but they won't give me the source code
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,614
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Recursive program issue

 
0
  #9
Nov 19th, 2008
>// unecessary brackets
Which can be used to introduce a new scope without adding a one-off function or doing something dumb like:
  1. if ( true ) {
  2. // Stuff
  3. }
You'd see it more in C prior to C99 where you want to restrict the scope of a for loop control variable:
  1. /* i isn't visible here */
  2.  
  3. {
  4. int i;
  5.  
  6. for ( i = 0; i < N; i++ ) {
  7. /* Do stuff */
  8. }
  9. }
  10.  
  11. /* nor is i visible here. yay */
>// oops no return 0
That's perfectly legal in C++. If you omit the return value from main, 0 will be returned automagically.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 15
Reputation: nedsnurb is an unknown quantity at this point 
Solved Threads: 0
nedsnurb nedsnurb is offline Offline
Newbie Poster

Re: Recursive program issue

 
0
  #10
Nov 19th, 2008
you may be on crack choco so the validity of the I love you statement may be slightly in question, but the feeling is mutual . Got it working now though thanks to everyone for replies, you helped a retard and that must make you happy.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC