473,403 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

recursion help

I have a program do soon and I can get the function to work but I can't get it to work as a recursive function. The green(code after the slash marks) is my attempt at recursion but it doesn't work. This is what I have:

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<cmath>
  3.  
  4. using namespace std;
  5.  
  6. double power(double b, double p);
  7.  
  8.  
  9. void main()
  10. {
  11.     cout << "Programmed by Heather Fischer\n";
  12.  
  13.     double answer;
  14.     cout.setf(ios::fixed);
  15.     cout.precision(0);
  16.     answer = power (2,32);
  17.     cout << answer << endl;
  18.     answer = power(3,5);
  19.     cout << answer << endl;
  20.     answer = power(2,15);
  21.     cout << answer << endl;
  22.  
  23.  
  24. }
  25. double power(double b, double p)
  26. {
  27.     double answer;
  28.     if ( static_cast<int>(p) % 2 == 0)                        
  29.     {
  30.         //answer = power(b*b,p/2);                          
  31.         answer = pow(pow(b,2.0),(p/2.0));
  32.     }
  33.     else                                
  34.     {                                 
  35.         //answer = b * power(b*b,(p-1)/2);                     
  36.         answer = b * pow(pow(b,2.0),((p-1)/2.0));   
  37.     }
  38.     return answer;
  39. }    
what am i doing wrong?
Feb 7 '07 #1
3 1174
AdrianH
1,251 Expert 1GB
I have a program do soon and I can get the function to work but I can't get it to work as a recursive function. The green(code after the slash marks) is my attempt at recursion but it doesn't work. This is what I have:

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<cmath>
  3.  
  4. using namespace std;
  5.  
  6. double power(double b, double p);
  7.  
  8.  
  9. void main()
  10. {
  11.     cout << "Programmed by Heather Fischer\n";
  12.  
  13.     double answer;
  14.     cout.setf(ios::fixed);
  15.     cout.precision(0);
  16.     answer = power (2,32);
  17.     cout << answer << endl;
  18.     answer = power(3,5);
  19.     cout << answer << endl;
  20.     answer = power(2,15);
  21.     cout << answer << endl;
  22.  
  23.  
  24. }
  25. double power(double b, double p)
  26. {
  27.     double answer;
  28.     if ( static_cast<int>(p) % 2 == 0)                        
  29.     {
  30.         //answer = power(b*b,p/2);                          
  31.         answer = pow(pow(b,2.0),(p/2.0));
  32.     }
  33.     else                                
  34.     {                                 
  35.         //answer = b * power(b*b,(p-1)/2);                     
  36.         answer = b * pow(pow(b,2.0),((p-1)/2.0));   
  37.     }
  38.     return answer;
  39. }    
what am i doing wrong?
Try expanding:

power(2,32)
=power((2)*(2),16)
=power((2*2)*(2*2),8)
=power((2*2*2*2)*(2*2*2*2),4)
=power((2*2*2*2*2*2*2*2)*(2*2*2*2*2*2*2*2),2)
=power((2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2)*( 2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2),1)
=power((2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2* 2*2*2*2*2*2*2*2*2*2*2)*(2*2*2*2*2*2*2*2*2*2*2*2*2* 2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2),0)
=power((2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2* 2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2* 2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2)*(2*2*2*2*2*2* 2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2* 2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2* 2*2*2*2*2*2*2*2),0)


As you can see, this recursive function will never stop because 0%2 = 0

Hope that helps.


Adrian
Feb 8 '07 #2
I figured it out. If anyone is interested here is my code:

#include<iostream>
#include<cmath>

using namespace std;

double power(double b, double p);


void main()
{
cout << "Programmed by Heather Fischer\n\n";
double b =0, p=0;
double answer;
cout.setf(ios::fixed);
cout.precision(0);
answer = power (2,32);
cout << " 2 ^ 32 = " << answer << endl;
answer = power(3,3);
cout << " 3 ^ 3 = " << answer << endl;
answer = power(2,15);
cout << " 2 ^ 15 = " << answer << endl << endl;


}
double power(double b, double p)
{
double answer;
if (p == 0)
{
return 1;
}
else if ( static_cast<int>(p) % 2 == 0)
{

answer = power(b*b,p/2);
//answer = pow(pow(b,2.0),(p/2.0));
}
else
{
answer = b * power(b*b,(p-1)/2);
//answer = b * pow(pow(b,2.0),((p-1)/2.0));
}
return answer;
}
Feb 8 '07 #3
AdrianH
1,251 Expert 1GB
Great stuff.


Adrian
Feb 8 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Jakle | last post by:
Hi all. Need alittle help here. This is an example from "How to Think Like a Computer Scientist: Learning with Python, Chapter 5". It's an open source ebook, so if you feel like it you can find it...
4
by: Dan | last post by:
I've encountered some strange behavior in a recursive procedure I'm writing for a bill of materials. First let me ask directly if what I think is happening is even possible: It seems like the...
5
by: J. Wesolowski | last post by:
Hello, This is my first approach to comp.lang.c, so hello to everyone. I learn c programming from "Teach yourself c " by Aitken & Jones. So far everything is going well and I'm quite excited about...
11
by: Josiah Manson | last post by:
In the following program I am trying to learn how to use functional programming aspects of python, but the following program will crash, claiming that the recursion depth is too great. I am...
18
by: MTD | last post by:
Hello all, I've been messing about for fun creating a trial division factorizing function and I'm naturally interested in optimising it as much as possible. I've been told that iteration in...
2
by: mdeni | last post by:
I am sorry if this was allready asked or discussed, please redirect me. I have to make the program of postorder traversal of the binary search tree NOT using recursion. I have found many solutinos...
12
by: NOO Recursion | last post by:
Hi everyone! I am trying to write a program that will search a 12x12 for a thing called a "blob". A blob in the grid is made up of asterisks. A blob contains at least one asterisk. If an...
13
by: Mumia W. | last post by:
Hello all. I have a C++ program that can count the YOYOs that are in a grid of Y's and O's. For example, this Y O Y O O Y O Y O Y O O Y O Y Y O Y O Y O O Y O O Y Y O Y O
30
by: Jeff Bigham | last post by:
So, it appears that Javascript has a recursion limit of about 1000 levels on FF, maybe less/more on other browsers. Should such deep recursion then generally be avoided in Javascript?...
35
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.