943,610 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 702
  • C++ RSS
Nov 19th, 2008
0

Recursive program issue

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
nedsnurb is offline Offline
27 posts
since Nov 2008
Nov 19th, 2008
0

Re: Recursive program issue

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.
Reputation Points: 11
Solved Threads: 1
Light Poster
Lokolo is offline Offline
29 posts
since Nov 2008
Nov 19th, 2008
0

Re: Recursive program issue

C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  1. int main
  2. {
  3. int input;
  4. ...
  5. }
Reputation Points: 92
Solved Threads: 16
Junior Poster
chococrack is offline Offline
149 posts
since Oct 2008
Nov 19th, 2008
0

Re: Recursive program issue

hi guys thanks for the quick replies however I did try this and it still throws up the error even when it is changed?

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Light Poster
nedsnurb is offline Offline
27 posts
since Nov 2008
Nov 19th, 2008
0

Re: Recursive program issue

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;
Reputation Points: 11
Solved Threads: 1
Light Poster
Lokolo is offline Offline
29 posts
since Nov 2008
Nov 19th, 2008
0

Re: Recursive program issue

That's not where the problem lies.
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 134
Solved Threads: 18
Junior Poster
mrboolf is offline Offline
182 posts
since Jun 2008
Nov 19th, 2008
0

Re: Recursive program issue

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
nedsnurb is offline Offline
27 posts
since Nov 2008
Nov 19th, 2008
0

Re: Recursive program issue

A common main function appears as
C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. // code
  4. return 0;
  5. }

Yours appears like this:
C++ Syntax (Toggle Plain Text)
  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.

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 92
Solved Threads: 16
Junior Poster
chococrack is offline Offline
149 posts
since Oct 2008
Nov 19th, 2008
1

Re: Recursive program issue

>// unecessary brackets
Which can be used to introduce a new scope without adding a one-off function or doing something dumb like:
C++ Syntax (Toggle Plain Text)
  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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 19th, 2008
0

Re: Recursive program issue

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
nedsnurb is offline Offline
27 posts
since Nov 2008

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: How to read CVS Tag of a fille/directory from CPP Program
Next Thread in C++ Forum Timeline: Inheritence, .h files and its a long one





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


Follow us on Twitter


© 2011 DaniWeb® LLC