-5

I am writing two programs. One raises a number to the other number's power, and the other does greatest common divisor. Both of them crash using infinite recursion and I can't figure out why. Can someone look at these and give me suggestions? Please do not post complete solutions, only suggestions.

#include <iostream>
using namespace std;
int pow( int base, int exp ) {
int somevariable = pow(base,exp-1);
if (base == 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);
}
#include <iostream>
using namespace std;
int gcd(int number1, int number2) {
int returnj = 0;
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);

}

Kelton2
  • 1
  • 3
  • Why do you check that `base` is equal to zero? Learn to use a debugger please and go through your code to see what is happening. In the second ... c++ is not english, the logical operations do not work like you think they do. – luk32 Oct 26 '14 at 20:57
  • I check that base is equal to zero because I want to return 1 if base is 0, instead of the normal recursive call. – Kelton2 Oct 26 '14 at 21:00
  • Your `pow` function contains unconditional recursive call. It has no way to stop recursion. – RocketR Oct 26 '14 at 21:00
  • Ok you gotta work on your logic too =). 1st get the algorithm right. – luk32 Oct 26 '14 at 21:00
  • I understand that there is infinite recursion, I just don't know what the problem is with the call. – Kelton2 Oct 26 '14 at 21:02
  • When you want recursion to finish, you put the condition when the function should stop calling itself. Your code has no such condition. – RocketR Oct 26 '14 at 21:03
  • Like what condition? – Kelton2 Oct 26 '14 at 21:07
  • Your function `gcd` has undefined behavior - there are situations where it does not return a value. And format your code, it looks like my sister. – Captain Obvlious Oct 26 '14 at 21:07
  • Like `if (time_to_finish_recursing_already) { return result; } else { pow(); }` – RocketR Oct 26 '14 at 21:08
  • In other words, just remove the line with `somevariable`. – RocketR Oct 26 '14 at 21:12
  • Done. However, the result is now garbage. What's wrong with return base * pow (base,exp-1); – Kelton2 Oct 26 '14 at 21:13

2 Answers2

0

In pow function you need to change

if (base == 0)

To:

if (exp == 0)

Then it won't be endless recursion.

SHR
  • 7,940
  • 9
  • 38
  • 57
  • This will not fix the problem, because there's a call to `pow` immediately at the start of `pow`. So this `if` is not even reached ever. – RocketR Oct 26 '14 at 21:09
  • I fixed the crash- but now the result's garbage, and the other program is still broken. – Kelton2 Oct 26 '14 at 21:11
  • The result is incorrect because base 0 to any power(besides 0), is still 0, not 1. Use the SHR's answer. – RocketR Oct 26 '14 at 21:14
  • I'm assuming you mean that the code for if exp is 0 is wrong- what should I be returning then? Note that I removed that other variable with the pow call, and I have if (exp == 0). – Kelton2 Oct 26 '14 at 21:16
0

Now works

#include <iostream>
using namespace std;

int pow( int base, int exp ) {
    //There's was a line here creating a loop because it never arrives to a return
    if (exp == 0) { // base 0 makes not sense because the base it's always the same, it's the exp that decreases
          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 << " = "; // This line was not closed.
    cout << pow(base, power);
}
Umuril Lyerood
  • 175
  • 1
  • 1
  • 9