PSX5Central

Non Gaming Discussions => Off-Topic => Topic started by: QuDDus on April 26, 2004, 09:43:38 AM

Title: NEED SOME c++ help
Post by: QuDDus on April 26, 2004, 09:43:38 AM
Man i am doing the program that find the gcd.
You gotta use a recursive function it returns
integer with two integer arguments n and m.
formula GCD: gcd(m,n%m)

This is my first time using a recursive function so I am having a little trouble understanding it.


If you can help thanx



#include
using namespace std;


int thegcd (int m, int n);
{

   int retrunVal;
   gcd=(m,n%m)
      returnVal = gcd;

}
void main()
{

   int num1, num2;
     cout << "Enter two numbers"
     cin >> num1 >> num2;
   if (num1 <= 0) || (num2 <= 0)
   gcd = num2
   cout << "The gcd of " << num1
       << " and " << num2 << " is "
       << thegcd << endl;

   getch()
}
Title: NEED SOME c++ help
Post by: EmperorRob on April 26, 2004, 04:02:45 PM
What the heck is gcd?  I know GDP is Gross Domestic Product.

Anyway you\'ll want a conditional expression in your function or it will never stop recursing.  Some people I know never stop recursing.

int myFactorial( int x)
{
    if (x > 1)
        x *= myFactorial( x - 1);
    return x;
}

int main()
{
    int y = 4;
    cout << "factorial of " << y << " ... " << myFactorial(y) << endl;
    return 0;
}


That should echo out 24 as the result.  If you want to, put a \'cout\' in there to see each recursive call:


int myFactorial( int x)
{
    cout << x << endl;
    if (x > 1)
        x *= myFactorial( x - 1);
    return x;
}
Title: NEED SOME c++ help
Post by: QuDDus on April 26, 2004, 08:27:28 PM
thanx rob but the code has to be tailored to these specifications

GCD is greatest common divisor

 write a code to find the greatest common divisor (gcd).

   1. Write a function (Recursion Function,  to find the (GCD) returns integer with two integer arguments n and m
      . Uses this formal to find the GCD: gcd(m,n%m).
   2. If one of the two integers is zero, the gcd will be the 2nd integer.
   3. Tell the user for exit just enters -1.


I scratched my first code and i am redoing the entire thing.
Title: NEED SOME c++ help
Post by: SwifDi on April 28, 2004, 04:18:58 PM
Ah... the Visual Basic days of old...
Title: NEED SOME c++ help
Post by: §ôµÏG®ïñD on April 28, 2004, 08:46:42 PM
errrrr lol ok...

C++ and Visual Basic are 2 different things.  But if you just blurted that out for no reason. Then more power to you. :)