GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 18-Nov-2004, 17:23
Nexa Nexa is offline
Awaiting Email Confirmation
 
Join Date: Nov 2004
Location: Florida
Posts: 23
Nexa is on a distinguished road

Understanding Recursive Functions


I do not really understand what is being asked here, would anybody like to offer there help to help me understand this word problem. Any help would be appreciated.

The greatest common divisor of integers x and y is the largest interger that
evenly divides both x and y. Write a recursive function gcd that returns the
greatest common divisor of x and y, which is defined recursively as follows:
If y is equal to 0, then gcd(x,y) is x: otherwise gcd(x,y) is gcd(y, x % y),
where % is the modulus operator.

Thanks,

Nexa
  #2  
Old 18-Nov-2004, 20:09
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
HI Nexa. I get a kick out of recursive functions. A recursive function simply keeps calling itself in a nested fashion until something inside the function tells it to return.

This function can be coded quite easily based upon the given alogorithm:

CPP / C++ / C Code:
int gcd(int x, int y)
{
  if(y)
    return gcd(y, x%y);
  else
    return x;
}

You need to test that because I didn't Also, I am leaving the commenting to you so you can figure out what is happening.
  #3  
Old 18-Nov-2004, 20:14
Nexa Nexa is offline
Awaiting Email Confirmation
 
Join Date: Nov 2004
Location: Florida
Posts: 23
Nexa is on a distinguished road
Thanks, dssmith, Any help at this point is better than no help.
  #4  
Old 18-Nov-2004, 22:49
Nexa Nexa is offline
Awaiting Email Confirmation
 
Join Date: Nov 2004
Location: Florida
Posts: 23
Nexa is on a distinguished road
Ahhhh!, Can you say hit a brick wall. I know I probably have something so simple that I am not seeing here, but truth is I am so lost right now. My text book it not offering much help.
Here is what I have so far, all I ask is how do I get my function to work correctly. When I run it, it completely skips my function and exits the program.

CPP / C++ / C Code:
#include <iostream>

using std::cout;
using std::endl;
using std::cin;

// function prototype that specifies default arguments
int gcd( int, int );

int main()
{

int number_1;
int number_2;

cout << "type in 1st number" << endl;
cin >> number_1;

cout << "type in 2nd number" << endl;
cin >> number_2;


return 0; 

}


int gcd(int x, int y)
{
  if(y)

    return gcd(y, x%y);

  else

    return x;
}
Last edited by Nexa : 18-Nov-2004 at 23:08. Reason: Make code read in c++ format
  #5  
Old 19-Nov-2004, 03:29
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
I guess you forgot to call your function in main(). . Function needs to be called for its code to be executed. You also need to print the value returned by gcd() function in main()
  #6  
Old 19-Nov-2004, 12:51
Nexa Nexa is offline
Awaiting Email Confirmation
 
Join Date: Nov 2004
Location: Florida
Posts: 23
Nexa is on a distinguished road
Suppose you are correct, I got a little ahead of myself and for got to print my code to screen and call the function. See sometimes it takes others to see little things you are missing.

Thanks much!!!!

Nexa
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Please help with functions... brookeville C++ Forum 36 05-Nov-2004 01:23
variables in functions help dopee MySQL / PHP Forum 5 16-Oct-2004 21:20
conflict between printf and stdarg.h va functions mirizar C Programming Language 3 12-Jul-2004 09:11
Understanding functions tommy69 C Programming Language 15 15-Mar-2004 18:59

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:28.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.