Computing x^n in C++ without Math Library

  • C/C++
  • Thread starter ineedhelpnow
  • Start date
  • Tags
    C++ Program
In summary, Bac's code declares and initializes i, while EM's code uses a while loop. Neither implementation requires any changes outside of the loop.
  • #1
ineedhelpnow
651
0
Program should compute the power x^2
Code:
#include<iostream>
int main() {
double x;
int n;
double pow=1;

cout<<"Please enter x:";
cin>>x;
cout<<"Please enter n:";
cin>>n;

[B]LOOP FOR X^N[/B]
pow=pow*x

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

as you can see, there is no math library included and i need to be able to perform the operation x^n. i figured everything out except the main part which is the loop. do i need to use a do loop?
 
Technology news on Phys.org
  • #2
$$x^0 = 1$$
$$x^1 = x = x^0 \cdot x = x^{1 - 1} \cdot x$$
$$x^2 = (x) \cdot x = x^1 \cdot x = x^{2 - 1} \cdot x$$
$$x^3 = ((x) \cdot x) \cdot x = x^2 \cdot x = x^{3 - 1} \cdot x$$
$$\vdots$$
$$x^n = x^{n - 1} \cdot x$$

See the pattern? So you can use a for-loop, like this:

1. set xn = 1, and assert that x^0 = xn, which is correct, so far so good
2. for i = 1 to n, set xn = xn * x, and assert that at the end of the ith iteration, xn = x^i. this holds because then xn = x^(i - 1) * x = x^i, as the previous iteration computed x^(i - 1) and put it into xn
3. once the loop terminates, xn = x^n, and you are done

Can you implement that with a for loop (or other)? Does it work?

PS: there is a more efficient way of computing x^n that takes only around log(n) multiplications instead of n. Can you work it out? (if you can't, don't worry about it, it is a bit advanced).
 
  • #3
it looks like he didnt give initialize i for us so i don't think we can add anything to the code from besides what he gave us. is that the only loop that would work? if so, i guess i have no choice but to add i.

im not sure how to do it with log(n). i suck at programming. ill die with anything advanced. (Giggle)
 
  • #4
ineedhelpnow said:
it looks like he didnt give initialize i for us so i don't think we can add anything to the code from besides what he gave us. is that the only loop that would work? if so, i guess i have no choice but to add i.

im not sure how to do it with log(n). i suck at programming. ill die with anything advanced. (Giggle)

... you could add a variable i, yes. Also note that you aren't actually using the loop variable in the loop (it's just used to get something that runs n times) so you can get by with a while loop, perhaps as:

while (n--)
{
// stuff
}

which will run exactly n times.
 
  • #5
oh i see. but why n--?
 
  • #6
ineedhelpnow said:
oh i see. but why n--?

The idea is to decrease n until it reaches zero, doing one iteration every time you decrease it. You can rewrite it as n = n - 1 if you prefer.
 
  • #7
but how do you know you want to decrease. what if n is 1 and next you want it to be 2. or next you want it to be 7.
 
  • #8
Another version of the loop is
Code:
for (int i = 0; i < n; i++)
  // body of the loop
 
  • #9
yes that one is simpler but like i was telling Bac, since my instructor didnt initialize the variable i, I am not sure if he wants us to or if he wants us to use a different loop.
 
  • #10
ineedhelpnow said:
yes that one is simpler but like i was telling Bac, since my instructor didnt initialize the variable i, I am not sure if he wants us to or if he wants us to use a different loop.

Maybe try asking him? I mean, I get that he doesn't want you to completely change the program, but it seems to me that adding a for loop does not detract from the spirit of the question, which is: implement the naive algorithm to compute x^n.
 
  • #11
yeah its kind of difficult getting in touch with him and i don't know if ill have time to see him before the test but i think ill send him an email now. ill stick with the for loop. its simpler. i just wish he was sometimes more clear about what in the world he wants :) thank you guys
 
  • #12
The code I wrote both declares and initializes [m]i[/m] in the first line. Note that it says [m]int i = 0[/m] and not [m]i = 0[/m]. In the latter case, [m]i[/m] would have to be declared previously. As it is, no changes outside of the loop are necessary. On the other hand, you need a loop counter; otherwise you don't know how many iterations you've completed.
 
  • #13
for (int i = 0; i < n; i++)
pow=pow*x;

is that right?
 
  • #14
ineedhelpnow said:
for (int i = 0; i < n; i++)
pow=pow*x;

is that right?
Yes, that's right.
 
  • #15
thanks Bac. thanks EM.
 
  • #16
i think ill just do it your way. and if the teacher says anything ill tell him to (Swearing) :eek: i reeeaaaally appreciate your help mark to take the time and to actually show me how to exactly do the code. thank you for showing me how to do it in a MORE CLEAR MANNER than my instructor would. you're the best programmer i know around :eek:
 
Last edited:

FAQ: Computing x^n in C++ without Math Library

1. What is the purpose of a C++ program to compute x^n?

The purpose of a C++ program to compute x^n is to calculate the result of raising a number x to the power of n. This can be useful in various mathematical and scientific calculations.

2. What are the input parameters for a C++ program to compute x^n?

The input parameters for a C++ program to compute x^n are the base number x and the power n. These values can be provided by the user or can be pre-defined within the program.

3. How does a C++ program compute x^n?

A C++ program computes x^n by using a loop or recursive function to multiply the base number x by itself n times. This is equivalent to raising x to the power of n.

4. Can a C++ program handle negative values for n?

Yes, a C++ program can handle negative values for n. If n is a negative number, the program will calculate the reciprocal of x^n, which is equivalent to raising x to the power of -n.

5. How accurate is a C++ program in computing x^n?

The accuracy of a C++ program in computing x^n depends on the data type used for the variables x and n. Using a larger data type, such as double or long double, can increase the accuracy of the result. Additionally, the algorithm used in the program can also affect the accuracy.

Similar threads

Replies
5
Views
2K
Replies
39
Views
4K
Replies
6
Views
9K
Replies
35
Views
3K
Replies
118
Views
7K
Replies
22
Views
2K
Back
Top