Power function

Jul 12, 2014 at 11:19am
Firstly I'd like to thank the creators of this website, it has the best tutorials I could find anywhere!

I just started learning C++ as my first programming language with no prior knowledge of anything to do with programming whatsoever, so I'm sorry if this question sounds stupid.

I don't know how to use powers in C++ yet, but I decided to build something to solve powers just for practice. Here's what I have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  #include <iostream>
using namespace std;

int power (int x, int y)
{
    int z=0;

    do
    {
        x*=x;
        z++;
    }
    while (z!=y);

    return (x);
}

int main()
{
    int a, b;

    cout << "enter a value\n";
    cin >> a;
    cout << "enter a second value\n";
    cin >> b;

    int c;
    c = power (a,b);

    cout << '\n' << c;

}


I know this isn't "really" solving powers and it won't work for anything to the power of 1 or below, but like I said I'm doing this for practice. For some reason, this isn't working and I can't figure out why.

Any ideas?
Last edited on Jul 12, 2014 at 11:20am
Jul 12, 2014 at 11:36am
You should explain what you mean by "isn't working". Is it not compiling? If it is, what inputs are you trying to use... etc.

Anyway, change initial z value to 1 (or change the while loop terminator). Also note that this will only work correctly with two integer inputs.

Okay I kept thinking "multiplication" in my head... just ignore me.
Last edited on Jul 12, 2014 at 12:00pm
Jul 12, 2014 at 11:36am
Your problem is that you are multiplying X by itself, and then multiplying again by what was already there. Try this instead:
1
2
3
4
5
6
int power(int x, int y) {
    int result = 1;
    while (--y >= 0)
        result *= x;
    return result;
}


If you want the 'proper' power function, you can do it like this:
1
2
3
4
5
6
7
8
#include <iostream>
#include <cmath>

int main() {
    // ...
    std::cout << "\n" << std::pow(a, b);
    return 0;
}
Last edited on Jul 12, 2014 at 11:39am
Jul 12, 2014 at 11:37am
The problem is that the value of x changes so you are not multiplying with the original value of x in each iteration of the loop.
Jul 12, 2014 at 11:54am
@NT3

I tried yours and it still didn't work. I think it still kept multiplying x by itself like it did originally.

So I did this:

1
2
3
4
5
6
7
8
9
10
{
    int z=x;

    while (y > 1) {
        x =z*x;
        --y;
    }
    return x;

}


This way the value of z is the constant first value of x, and y>1 because the first value of y is still taken into account. This worked for me.
Last edited on Jul 12, 2014 at 11:54am
Jul 12, 2014 at 12:06pm
Don't forget when y == 0, the result should be 1.
1
2
3
4
5
6
7
8
9
int power (int x, int y)
{
    int z = 1;

    while (y--) 
        z *= x;

    return z;
}
Jul 12, 2014 at 12:14pm
When I said it didn't work I mean it didn't calculate it correctly.

I now changed all the numbers to float and tried to include if y<0 and if y==0.

The calculation works fine for y>0 and y==0, but not for y<0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
int power (float x, float y)

{
    float z=x;

if (y>0)
    {
    while (y > 1)
        {
        x =z*x;
        --y;}
    }

else if (y<0)
{
   while (y<-1)
    {
        x=z*x;
   ++y;
    }
    x=(1/x);
}

else if (y==0)
{
    x = 1;
}
    return x;
}
Jul 12, 2014 at 12:19pm
If y is less than zero , then that's like doing 1/pow(x,+y). You won't be able to give an accurate answer without truncating if you're only using ints for this.
Last edited on Jul 12, 2014 at 12:20pm
Jul 12, 2014 at 12:57pm
Good point regarding negative values of y. I'd make x and the return value type double. y can remain an int.

(If y is not an integer then it's probably easier to use logs / antilogs (or use the built-in pow() )
Topic archived. No new replies allowed.