Reubens4Dinner
Reubens4Dinner

Reputation: 343

Changing a Global Variable to a Reference Parameter C++

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;
    }

Upvotes: 1

Views: 957

Answers (4)

Gregg
Gregg

Reputation: 2624

add depth to prototype parameters to (note the reference notation) it does not need to be named depth.

int gcd(int first, int second, int &depth);

remove the global definition for depth.

add depth to local variables

int firstNum, secondNum, depth=0;

add depth to first call (this passes by reference)

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

add depth to function definition

int gcd(int first, int second, int &depth)

add depth to recursive call

return gcd(second, first % second, depth);

Upvotes: 1

Tomasz Plaskota
Tomasz Plaskota

Reputation: 1367

int gcd(int first, int second, int& depth)
{
    if(second != 0) 
    {
        ++depth;
        return gcd(second, first % second, depth);
    }
    else
    {
        return first;
    }
}

You have to call it little bit different tho. Create new int, initialize it to 0 and pass as third argument. After this is done it will hold depth.

Upvotes: 1

Barmar
Barmar

Reputation: 781726

Add the reference parameter to the function definition.

int gcd(int first, int second, int& depth)
    {
        if(second != 0) 
        {
            depth++;
            return gcd(second, first % second, depth);
        }
        else
            return first;
    }

Then declare depth as a local variable in main(), initialize it to 0, and call gcd(firstNum, secondNum, depth).

Upvotes: 1

nanocv
nanocv

Reputation: 2229

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;
    }

Upvotes: 1

Related Questions