Computing x^n in C++ without Math Library

  • Context: C/C++ 
  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    C++ Program
Click For Summary

Discussion Overview

The discussion revolves around implementing a program in C++ to compute the power of a number, specifically x^n, without using the math library. Participants explore various looping constructs to achieve this, including for-loops and while-loops, and discuss the initialization of loop variables.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related
  • Mathematical reasoning

Main Points Raised

  • One participant suggests using a for-loop to compute x^n, iterating from 1 to n and multiplying the result each time.
  • Another participant mentions the possibility of a more efficient method that computes x^n in log(n) multiplications but expresses uncertainty about implementing it.
  • Concerns are raised about the initialization of the loop variable i, with some participants questioning whether they can modify the provided code.
  • Several participants discuss the use of a while-loop as an alternative, with one explaining that decrementing n can serve as a loop counter.
  • Clarifications are made regarding the mechanics of decrementing n and the implications for loop execution.
  • One participant expresses frustration with the instructor's lack of clarity and seeks confirmation on the correctness of their proposed code.
  • Another participant emphasizes the importance of declaring and initializing loop variables correctly to avoid confusion.

Areas of Agreement / Disagreement

Participants generally agree on the need to use a loop for the computation, but there is no consensus on the specific implementation details, such as whether to use a for-loop or while-loop, and how to handle variable initialization. Multiple competing views remain regarding the best approach to take.

Contextual Notes

Some participants express uncertainty about the instructor's expectations and the constraints of the assignment, particularly regarding variable initialization and the choice of loop structure. There is also mention of a more efficient algorithm that remains unexplored.

Who May Find This Useful

This discussion may be useful for students learning C++ programming, particularly those interested in implementing algorithms for mathematical computations without relying on external libraries.

ineedhelpnow
Messages
649
Reaction score
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
$$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).
 
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)
 
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.
 
oh i see. but why n--?
 
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.
 
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.
 
Another version of the loop is
Code:
for (int i = 0; i < n; i++)
  // body of the loop
 
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) :o 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 :o
 
Last edited:

Similar threads

Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 39 ·
2
Replies
39
Views
5K