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, 2022 in C++ by Nicholas
• 7,760 points
2,533 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, 2022 by Damon
• 4,960 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

Jun 27, 2022 in C++ by Nicholas
• 7,760 points
588 views
0 votes
0 answers

how the iterator in c++ could be printed?

Suppose, I have declared a vector in C++ like ...READ MORE

Jul 11, 2022 in C++ by Nicholas
• 7,760 points
639 views
0 votes
0 answers

How can I get the size of a C++ function?

In C++, how can I find the size& ...READ MORE

Aug 5, 2022 in C++ by Nicholas
• 7,760 points
581 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, 2022 in C++ by Nicholas
• 7,760 points
597 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
779 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, 2022 in C++ by Nicholas
• 7,760 points
543 views
0 votes
1 answer

lower_bound == upper_bound

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

answered Jun 21, 2022 in C++ by Damon
• 4,960 points
1,067 views
0 votes
0 answers

Why aren't cmath's pow and sqrt templates?

I'm curious as to why std::sqrt and ...READ MORE

Jul 13, 2022 in C++ by Nicholas
• 7,760 points
446 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, 2022 in C++ by Damon
• 4,960 points
1,159 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, 2022 in C++ by Damon
• 4,960 points
11,347 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP