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 13-Jul-2005, 07:19
vinch vinch is offline
New Member
 
Join Date: Jul 2005
Posts: 6
vinch is on a distinguished road
Question

help in c++ recursive functions


had an assignment where in i have to create a recursive function program
given a base number and an exponent.

for example: power(10,2)= 100

10 is the base and 2 is the exponent;

here's what ive done so far:

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

int power(int base, int exponent);
int value;
main(void)
{	int b,e;
	cout<<"Enter base number:";cin>>b;
	cout<<"Enter exponent:";cin>>e;
	power(b,e);
	cout<<value;
	return 0;
}

power(int base, int exponent)
{
	int value;
	value=power(base*base,exponent);
	return (value);
}

pls. help me locate the error in the code. your help will be much appreciated. thanx in advance!!!
Last edited by LuciWiz : 13-Jul-2005 at 07:23. Reason: Please insert your C++ code betweend [c++] & [/c++] tags
  #2  
Old 13-Jul-2005, 08:13
Dave Sinkula Dave Sinkula is offline
Member
 
Join Date: Apr 2005
Posts: 199
Dave Sinkula will become famous soon enough
Recursion can be viewed as an alternate form of a loop. Just like a loop needs a terminating condition, so does the recursion. Each time you recurse, the function should be passed a value that converges to this condition (for example, a base that is successively one less than the previous and a check for a base that is greater than zero).

On your next spin, it might be good to use standard C++ (not pre-standard <iostream.h> headers, for example). And an implicitly declared main is over a decade out of date as well. And you don't need to use a global variable.
  #3  
Old 13-Jul-2005, 08:31
QED's Avatar
QED QED is offline
Member
 
Join Date: Feb 2005
Location: Hudson Valley, NY
Posts: 231
QED is a jewel in the roughQED is a jewel in the roughQED is a jewel in the rough
Quote:
Originally Posted by vinch
CPP / C++ / C Code:
power(int base, int exponent)
{
	int value;
	value=power(base*base,exponent);
	return (value);
}
Under what condition will your recursive function stop and finally return a value? You have written an infinitely recursing function. Once we enter power, we call power again; and in there, we call power again; and in there... forever! The key step when using recursion is to have some terminal condition (no, not an illess ) which controls when to stop recursing. For example,
CPP / C++ / C Code:
int eliminateFactor(int val, int factor)
{
  if (val % factor > 0) { // this is the terminal condition
    return val;     // see, we don't recurse in this case
  }

  val = val / factor;

  return eliminateFactor(val, factor);  // here, we recurse
}
Try rethinking your function and determine what will be the terminal condition(s), and how exactly to recurse. Then post any further questions/problems if they arise.

Hope this helps.

Matthew

Edit: Oops, I am rather late!
  #4  
Old 13-Jul-2005, 10:40
p_kumar811 p_kumar811 is offline
New Member
 
Join Date: Jul 2005
Posts: 3
p_kumar811 is on a distinguished road

ode for Power Function


Hi,
I just saw your code and was wondering on 2 count.
1) In the code for you power fuction will go on an infinite loop as there is no Exit condition.
2) In your main function you are not capturing the value that is being returned by the main function. You need to capture the value that is being returned by the function. And also the concept of using Global varaibles is a old concept and is not a good Object Orientation Practise.

The following code should work:

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

using namespace std;

int power (int base, int exp)
{
	exp--;
	if (exp>-1)
		return base * (power(base,exp));
	else
		return 1;
}

void main()
{
	int base, exp, value;
	cout<<"Enter the Base Value: ";
	cin >>base;
	cout<<"Enter The Exponent Value: ";
	cin>>exp;
	value = power(base, exp);
	cout<<"The value is: "<<value;
}


This has been tested in VC++ Compiler. and i guess this shld work in all Windows based Compilers.

Regards

Prasanth
  #5  
Old 15-Jul-2005, 06:55
vinch vinch is offline
New Member
 
Join Date: Jul 2005
Posts: 6
vinch is on a distinguished road
Quote:
Originally Posted by p_kumar811
Hi,
I just saw your code and was wondering on 2 count.
1) In the code for you power fuction will go on an infinite loop as there is no Exit condition.
2) In your main function you are not capturing the value that is being returned by the main function. You need to capture the value that is being returned by the function. And also the concept of using Global varaibles is a old concept and is not a good Object Orientation Practise.

The following code should work:

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

using namespace std;

int power (int base, int exp)
{
	exp--;
	if (exp>-1)
		return base * (power(base,exp));
	else
		return 1;
}

void main()
{
	int base, exp, value;
	cout<<"Enter the Base Value: ";
	cin >>base;
	cout<<"Enter The Exponent Value: ";
	cin>>exp;
	value = power(base, exp);
	cout<<"The value is: "<<value;
}


This has been tested in VC++ Compiler. and i guess this shld work in all Windows based Compilers.

Regards

Prasanth
thanx p_kumar811 for the help!!
  #6  
Old 04-Jan-2006, 05:52
luaik luaik is offline
New Member
 
Join Date: Jan 2006
Posts: 1
luaik is on a distinguished road

Re: help in c++ recursive functions


i should make a recursive programm
that print the partition of the number N
that's mean
for N=4
4
31
22
211
1111
if N=5
5
41
32
311
221
2111
11111

if N=6
6
51
42
411
33
321
3111
222
2211
21111
111111
  #7  
Old 04-Jan-2006, 09:50
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,305
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: ode for Power Function


Quote:
Originally Posted by p_kumar811
Hi,

CPP / C++ / C Code:

void main()



This has been tested in VC++ Compiler. and i guess this shld work in all Windows based Compilers.

Gives an error for Cygwin g++ (on Windows XP) and, therefore, will not compile:
Code:
15: error: `main' must return `int'

Regards,

Dave
  #8  
Old 04-Jan-2006, 12:22
Guidelines Plz Guidelines Plz is offline
Junior Member
 
Join Date: Sep 2005
Posts: 87
Guidelines Plz is on a distinguished road

Re: help in c++ recursive functions


Welcome luaik.

First of all, why would you want to post a reply to a very old thread rather than start a new thread for your problem?

Second:
__________________

Please read http://www.gidforums.com/t-5566.html. They were written to help you create a request that is readable and has enough information we can actually tell what you need help with.
 
 

Recent GIDBlogNot selected for officer school 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
friend functions error CaptnB C++ Forum 2 12-Jun-2006 13:39
Major problem with recursive function, help.. kakamuti C Programming Language 4 19-Dec-2004 07:47
Understanding Recursive Functions Nexa C++ Forum 5 19-Nov-2004 11:51
conflict between printf and stdarg.h va functions mirizar C Programming Language 3 12-Jul-2004 08:11

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

All times are GMT -6. The time now is 07:38.


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