Hello

Welcome, Guest. Please login or register.
Did you miss your activation email?

Author Topic: NEED SOME c++ help  (Read 624 times)

Offline QuDDus
  • Taste so gooood!!!!
  • Legendary Member
  • ******
  • Posts: 3545
  • Karma: +10/-0
NEED SOME c++ help
« 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()
}
« Last Edit: April 26, 2004, 09:46:04 AM by QuDDus »
\"confucious say - he who sleeps with itchy ass wakes up with smelly fingers\".
\"dont trust anything that bleeds for a week and dont die\" - A pimp
\"FF7 was the greatest game ever made!!!\" -MM

Offline EmperorRob
  • Mr Sexual Harassment
  • Legendary Member
  • ******
  • Posts: 3932
  • Karma: +10/-0
NEED SOME c++ help
« Reply #1 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;
}
« Last Edit: April 26, 2004, 04:06:28 PM by EmperorRob »
This is America and I can still pay for sex with pennies

Offline QuDDus
  • Taste so gooood!!!!
  • Legendary Member
  • ******
  • Posts: 3545
  • Karma: +10/-0
NEED SOME c++ help
« Reply #2 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.
\"confucious say - he who sleeps with itchy ass wakes up with smelly fingers\".
\"dont trust anything that bleeds for a week and dont die\" - A pimp
\"FF7 was the greatest game ever made!!!\" -MM

Offline SwifDi
  • Legendary Member
  • ******
  • Posts: 9620
  • Karma: +10/-0
NEED SOME c++ help
« Reply #3 on: April 28, 2004, 04:18:58 PM »
Ah... the Visual Basic days of old...

Offline §ôµÏG®ïñD

  • ñµñ©Håkµ må§tË®
  • Global Moderator
  • Legendary Member
  • ******
  • Posts: 9682
  • Karma: +10/-0
  • Ǧµî✟å® Ĵµñķîë
    • §ôµÏG®ïñD'§ Electrical / Electronics shit.
  • PSN ID: SoulGrind81
NEED SOME c++ help
« Reply #4 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. :)
  Ǧµî✟å® Ĵµñķîë!!  

 

SMF spam blocked by CleanTalk