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 Rating: Thread Rating: 2 votes, 5.00 average.
  #1  
Old 09-Nov-2005, 19:12
thulaotzu thulaotzu is offline
New Member
 
Join Date: Oct 2005
Posts: 26
thulaotzu is on a distinguished road

Recursive function power(base,exponent)


I have an assignment that I think has some logical errors in the base case and recursive step. My program actually calculates values correctly, but I think it can use some improvements in the logic. Can someone help me fix it so that it will be logically correct?

Here are the instructions given by my instructor:
Write a recursive function power(base,exponent) that, when invoked, returns
base^exponent
For example, power( 3, 4 ) = 3 * 3 * 3 * 3. Assume that exponent is an integer greater than or equal to 1. Hint: The recursion step would use the relationship
base exponent = base · base ^ exponent- 1
and the terminating condition occurs when exponent is equal to 1 because
base^1 = base


Below is my code:

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

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


int power (int, int);

int main ()

{
	int base, exp, result;

	//obtain integer from user
	cout<<"Enter the base value: ";
	cin >>base;
	cout<<"Enter the exponent value: ";
	cin>>exp;

	result = power(base, exp);

	cout<<"The result is: "<< result << endl;
	
	return 0;
}


// recursive definition
int power (int base, int exp)
{
	// base case	
	if (exp==1)
		return base * 1;
		
	// recursive step	
	else if (exp >-1)
		exp--;
		
		return base * (power(base,exp));
		
		
}
  #2  
Old 10-Nov-2005, 08:15
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: Recursive function power(base,exponent)


Hi Thu,

Suppose if the user enters 0 as the exponent, the return answer should be 1.
So, here is the modified recursion function:
CPP / C++ / C Code:
int power (int base, int exp)
{
   if(exp >= 1)
       return base * (power(base,exp - 1));

   else
       return 1;
}

The logic is that you must multiply base until exp becomes zero.
i.e:
consider that user enters 3 and 4.
So,
CPP / C++ / C Code:

return base * (power(base,exp - 1));

this line will expand like this:
base * base * base * base * 1

There is one more problem.
What if the user enters negative numbers for the exponent value?
Think about that and add the code for that also.

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #3  
Old 10-Nov-2005, 10:37
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,435
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Recursive function power(base,exponent)


Quote:
Originally Posted by Paramesh
Suppose if the user enters 0 as the exponent, the return answer should be 1.

Suppose the reader of posts keep in mind the instructions for the program
Quote:
Originally Posted by thulaotzu
Here are the instructions given by my instructor:
Assume that exponent is an integer greater than or equal to 1.
__________________

Definition: Politics
Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
  #4  
Old 10-Nov-2005, 15:53
thulaotzu thulaotzu is offline
New Member
 
Join Date: Oct 2005
Posts: 26
thulaotzu is on a distinguished road

Re: Recursive function power(base,exponent)


Paramesh,
it works like a charm!

Thanks, mate.
 


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
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 13:12
[Tutorial] Function Pointers aaroncohn C++ Forum 4 17-Feb-2006 12:33
Major problem with recursive function, help.. kakamuti C Programming Language 4 19-Dec-2004 08:47
Revising Script style ?????? pepee MySQL / PHP Forum 4 14-Apr-2004 04:59

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

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


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