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: - #include<iostream>
-
#include<cmath>
-
-
using namespace std;
-
-
double power(double b, double p);
-
-
-
void main()
-
{
-
cout << "Programmed by Heather Fischer\n";
-
-
double answer;
-
cout.setf(ios::fixed);
-
cout.precision(0);
-
answer = power (2,32);
-
cout << answer << endl;
-
answer = power(3,5);
-
cout << answer << endl;
-
answer = power(2,15);
-
cout << answer << endl;
-
-
-
}
-
double power(double b, double p)
-
{
-
double answer;
-
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;
-
}
what am i doing wrong?
3 1174
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: - #include<iostream>
-
#include<cmath>
-
-
using namespace std;
-
-
double power(double b, double p);
-
-
-
void main()
-
{
-
cout << "Programmed by Heather Fischer\n";
-
-
double answer;
-
cout.setf(ios::fixed);
-
cout.precision(0);
-
answer = power (2,32);
-
cout << answer << endl;
-
answer = power(3,5);
-
cout << answer << endl;
-
answer = power(2,15);
-
cout << answer << endl;
-
-
-
}
-
double power(double b, double p)
-
{
-
double answer;
-
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;
-
}
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
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;
}
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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?...
|
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 ??
|
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
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |