how could I use the power function in c c without pow functions or recursion

0 votes

I'm using a C++ compiler, but I'm writing C code (if that helps)

There is a string of numbers.

(-1^(a-1)/2a-1)B^(2a-1)

A and X are user-specified... 

A must be positive, although X might be positive or negative...

in order to decipher this sequence... 

I need to utilise exponents/powers, but I have some constraints... 

I can't create a new function, utilise recursion, or use pow() (among other advanced math functions that come with cmath or math.h).

There were other comparable queries, however many solutions employed functions and recursion that were not immediately related to this subject.

This is the code that works fine with pow(); I spent a lot of time attempting to adapt it to replace pow() with my own code, but nothing appears to be working... I'm mostly getting incorrect results. 

X and J are variables that have been entered by the user.

for (int i = 1; i < j; i++) 
    sum += (pow(-1, i - 1)) / (5 * i - 1) * (pow(x, 5 * i - 1));
}
Jun 20 in C++ by Nicholas
• 2,520 points
14 views

1 answer to this question.

0 votes

It is part of a series. 

Replace pow() with the previous iteration's value.

There is no need for code to call pow (). 

Pow(x, 5 * I - 1) and pow(-1, I - 1) may be formed since both have an int exponent dependent on the iterator I from the previous loop iteration.

Example:
Let f(x, i) = pow(x, 5 * i - 1)
Then f(x, 1) = x*x*x*x
and f(x, i > 1) = f(x, i-1) * x*x*x*x*x

double power_n1 = 1.0; 
double power_x5 = x*x*x*x; 
for (int i = 1; i < j + 1; i++) 
  // sum += (pow(-1, i - 1)) / (5 * i - 1) * (pow(x, 5 * i - 1));
  sum += power_n1 / (5 * i - 1) * power_x5;
  power_n1 = -power_n1;
  power_x5 *= x*x*x*x*x; 
}
answered Jun 21 by Damon
• 4,240 points

Related Questions In C++

0 votes
0 answers

How can I get the maximum or minimum value in a vector?

In C++, how can I find the greatest or minimum value in a vector? Is it correct to assume that it would be similar with an array? Do I require an iterator?  I tried max element, but I kept receiving errors. vector<int>::const_iterator it; it = max_element(cloud.begin(), cloud.end()); error: request for ...READ MORE

3 days ago in C++ by Nicholas
• 2,520 points
5 views
0 votes
0 answers

Use of min and max functions in C++

Are std::min and std::max better than fmin ...READ MORE

Jun 2 in C++ by Nicholas
• 2,520 points
21 views
0 votes
1 answer

Are virtual functions the only way to achieve Runtime Polymorphism in C++?

fprintf is a polymorphism function in the C programming language. It can print to a file, stdout, a printer, a socket, or whatever else the system can represent as a stream if you supply it different handles. FILE* file = fopen("output.txt", "w"); ...READ MORE

answered Jun 21 in C++ by Damon
• 4,240 points
17 views
0 votes
1 answer

What is the best way to use a HashMap in C++?

The ordered and unordered map containers (std::map and std::unordered map) are included in the standard library.  The items in an ordered map are sorted by key, and insert and access are in O (log n).  For ordered maps, the standard library often use red black trees.  However, this is only an implementation detail.  Insert and access are in O in an unordered map (1).  It is simply another term for a hashtable. An illustration using (ordered) std::map: #include <map> #include <iostream> #include <cassert> int main(int argc, char ...READ MORE

answered Jun 10 in C++ by Damon
• 4,240 points
21 views
0 votes
1 answer

Power Math in Python

When you use "^" operator, it is ...READ MORE

answered Nov 12, 2018 in Python by ana1504.k
• 7,910 points
185 views
0 votes
0 answers

What is the C++ function to raise a number to a power?

What's the best way to raise a n ...READ MORE

Jun 1 in C++ by Nicholas
• 2,520 points
17 views
0 votes
1 answer

lower_bound == upper_bound

Lower bound: the initial greater-or-equal element. Upper bound: ...READ MORE

answered Jun 21 in C++ by Damon
• 4,240 points
22 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 11, 2018 in Python by Priyaj
• 58,080 points
191 views
0 votes
1 answer

How to use std::sort to sort an array in C++

We receive std::begin and std::end in C++0x/11, which are overloaded for arrays: #include <algorithm> int main(){ int v[2000]; ...READ MORE

answered Jun 1 in C++ by Damon
• 4,240 points
15 views
0 votes
1 answer

Use of min and max functions in C++

The functions fmin and fmax are designed ...READ MORE

answered Jun 21 in C++ by Damon
• 4,240 points
15 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP