Ben Reubenson - 5 months ago 104

C++ Question

So, after a lot of research on many sites, I finally broke down and made an account. I'm new and also new to programming so please be keep that in mind :)

I am making the classic example of recursion by making a program that uses Euclid's Method for greatest common divisor. I also need to keep track of how many times the function calls itself/how many times the recursion happens.

I have the program working perfectly, but I am using a global (very bad!) variable to hold the count of recursion. I need to replace the global variable with a reference parameter passed through my gcd function, but I am *very* poor in my understanding of how that works. That is my question. Could someone please show me how I can remove the global variable by using a reference parameter? Thank you so much!

`#include <iostream>`

using namespace std;

//Prototype functions

int gcd(int first, int second);

//Global varibles

//Depth will count how many times the recursive function runs.

int depth = 0;

int main ()

{

//variables

int firstNum, secondNum;

do

{

cout << "Welcome to Euclid's method for finding the GCD." << endl << "To quit, enter 0 for either number." << endl;

cout << "Enter the first number:";

cin >> firstNum;

cout << "Enter the second number:";

cin >> secondNum;

//The program quits if the user enters 0. If this happens, the program won't bother with running the recursive function.

if (firstNum != 0 && secondNum != 0)

{

cout << "The GCD of " << firstNum << " and " <<secondNum << " is ";

cout << gcd(firstNum, secondNum) << "." << endl;

cout << "The recursive calculation required a depth of " << depth <<"." << endl;

}

}

while (firstNum != 0 && secondNum != 0); //Program quits on 0 being entered

return (0);

}

//Recursive function. It will call itself until second number is 0.

int gcd(int first, int second)

{

if(second != 0)

{

depth++;

return gcd(second, first % second);

}

else

return first;

}

Answer

Try passing that parameter by reference.

```
int main ()
{
//variables
int firstNum, secondNum;
int depth = 0;
do
{
cout << "Welcome to Euclid's method for finding the GCD." << endl << "To quit, enter 0 for either number." << endl;
cout << "Enter the first number:";
cin >> firstNum;
cout << "Enter the second number:";
cin >> secondNum;
//The program quits if the user enters 0. If this happens, the program won't bother with running the recursive function.
if (firstNum != 0 && secondNum != 0)
{
cout << "The GCD of " << firstNum << " and " <<secondNum << " is ";
cout << gcd(firstNum, secondNum, depth) << "." << endl;
cout << "The recursive calculation required a depth of " << depth <<"." << endl;
}
}
while (firstNum != 0 && secondNum != 0); //Program quits on 0 being entered
return (0);
}
//Recursive function. It will call itself until second number is 0.
int gcd(int first, int second, int &depth)
{
if(second != 0)
{
depth++;
return gcd(second, first % second, depth);
}
else
return first;
}
```