Thread: Two problematic recursive programs

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    225

    Two problematic recursive programs

    I'm having trouble with two recursive programs, which don't work properly. Can someone look at these (DO NOT post complete solutions, only suggestions) :

    Code:
    #include <iostream>
    using namespace std;
    int pow( int base, int exp ) {
        if (exp == 0) {
             return 1;
     }        
       else {
              return base * pow(base,exp-1);
              }
    }
    int main ( ) {
        int base;
        int power;
        cout << "This program calculates exponential values." << endl;
        cout << "Enter the base: ";
        cin >> base;
        cout << "Enter the power: ";
        cin >> power;
        cout << "" << endl;
        cout << base << "^" << power << " = " <<
        cout << pow(base, power);
    }
    and
    Code:
    #include <iostream>
    using namespace std;
    int gcd(int number1, int number2) {
       if(number1 || number2 >= 0) {
       return gcd(number2, number1 % number2);
       }
                   
        else if(number1 || number2 == 0) {
               return 1;
        }
    }
    int main ( ) {
        int number;
        int another;
        int gcdd;
        cout << "This program calculates the greatest common divisor (GCD) for two integers." << endl;
        cout << "Enter a number: ";
        cin >> number;
        cout << "Enter another: ";
        cin >> another;
        cout << "" << endl;
        cout << "GCD = " << gcd(number, another);
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    In the pow one I would suggest rename the function to Pow; since pow is a C standard function.
    It is unlikely to fix any problem; but, since you state nothing about the problem it worth a try.
    Esp. since you have this line in your code of "using namespace std;"

    NOTE: Most recursive functions have the stopping condition checks before the recursive function call.
    Your second example does NOT follow that pattern.

    Edit: Read the 8 common mistakes FAQ you might get a hint on it for the second one.
    Edit2: Link http://www.cprogramming.com/tutorial/common.html

    Tim S.
    Last edited by stahta01; 10-26-2014 at 03:50 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Testing issue for "int pow( int base, int exp )" what happens if either base or exp is zero or negative?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    I still don't know what the problem is. For clarifications:

    Raise to Power works but produces a garbage result
    The other one doesn't work at all.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Kelton2 View Post
    I still don't know what the problem is. For clarifications:

    Raise to Power works but produces a garbage result
    Define garbage.
    It works as expected for me for some numbers, e.g. 10^4:

    Code:
    #include <iostream>
    using namespace std;
    
    int pow(int base, int exp)
    {
    	if (exp == 0)
    	{
    		return 1;
    	}
    	else
    	{
    		return base * pow(base, exp - 1);
    	}
    }
    int main()
    {
    	int base;
    	int power;
    	cout << "This program calculates exponential values." << endl;
    	cout << "Enter the base: ";
    	cin >> base;
    	cout << "Enter the power: ";
    	cin >> power;
    	cout << "" << endl;
    	cout << base << "^" << power << " = ";
    	cout << pow(base, power) << "\n";
    }
    (You can't actually cout cout as you did in your original code, so I removed it.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    When I put in 3 and 2, it gives me 0x44 something something.



    EDIT: OMG IT WORKS! Now I just have the other one. Any help there?
    Last edited by Kelton2; 10-26-2014 at 05:02 PM.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Works for me:
    Code:
    This program calculates exponential values.
    Enter the base: 3
    Enter the power: 2
    
    3^2 = 9
    Show the smallest possible compilable example that exhibits the problem. I can't reproduce the error.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Saw my edit?

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I did not.
    As for the other, an educated guess, but this
    "else if(number1 || number2 == 0) {"
    probably won't do what you think it will.
    It actually means
    "else if (number1 != 0 || number2 == 0)"
    You have to be explicit. It should probably be
    "else if (number1 == 0 || number2 == 0)"

    I am not taking into account if the actual algorithm is correct or not, however.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    That still does nothing.

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by stahta01 View Post
    NOTE: Most recursive functions have the stopping condition checks before the recursive function call.
    Your second example does NOT follow that pattern.
    Hint Hint

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  12. #12
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    What?

  13. #13
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Kelton2 View Post
    What?
    Use your mind trace the program with inputs of 0 and 0; what will happen?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  14. #14
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Oh I see, >= should be >. Got that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting recursive function to tail recursive
    By ajacobs365 in forum C Programming
    Replies: 1
    Last Post: 10-30-2011, 08:15 AM
  2. Make Recursive function 'Tail-Recursive'
    By dp2452 in forum C Programming
    Replies: 7
    Last Post: 12-04-2009, 10:13 AM
  3. merge sort: recursive is fasfter than non-recursive
    By rio_cat in forum C Programming
    Replies: 8
    Last Post: 12-04-2006, 12:52 AM
  4. Book's code: Problematic
    By RoD in forum Game Programming
    Replies: 14
    Last Post: 01-21-2003, 09:08 AM
  5. my problematic first program
    By lisab in forum C Programming
    Replies: 0
    Last Post: 10-28-2002, 09:58 AM