integePOWER function without math library functions

I need to create an integer^Power program for example. User gives 2 numbers (first is int, 2nd is power) (3,4) = 3*3*3*3 or 3^4. I think I need to use a for or while loop, but I don't know which? Can someone give me the calculation for the for loop?
1
2
3
4
5
6
7
8
9
10
11
12
13

int integerPower(int x, int y)
{ 
        int total = 1;
        for(int i = 0; i < y; i++)
        {
                total = total * x;
         }
          return total;
}


Last edited on
the above was a function. A program would be like this

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

void main()
{

int x, y, total = 1;


cout<< "This program calculates an exponet for example 2^2 is 4. Please enter the base: ";
cin >> x;
cout << "Please enter the power to raise the base too: ";
cin >> y;


for(int i = 0; i < y; i++)
         total = total * x;

cout << "The reslult of " << x << "^" << y << "=" << total << "." << endl;

system("pause");

}


that should do it
@btripp: Please don't just give answers out for free; most will just end up copying & pasting the code and learning nothing if you do that.
Not necessarily. Some learn by going in depth and making sure they understand every line of code. It would be dumb to turn in homework that you don't understand and then fail the test.

Like me, I am confused about the int i. Is that the loop counter? I see that it increments by 1 each time, so it's i=i+i.

1
2
for(int i = 0; i < y; i++)
                 total = total * x


I'm not sure what the computer/compiler is doing here. The loop creates a new variable (i) then it declares it to equal to 0. Then if exponent is less than 0 it increments the exponent itself until it equals 0? Then total is doing 1*integer? I don't get that, because wouldnt that be the same number due to multiplication logic? The program works however.
Last edited on
Ok. in the for loop the int i = 0 part is in fact a counter. then the middle part "i < y" is to be read: as long as i is less than y execute the code. after it executes i++ is the same as i = i +1; then it reavaluates the i < y

Here is a step through example of the code if the user entered 2^5

so in this case we will assume that the user entered a 2 and it was stored in the variable x
and the user also entered a 5 for the y value in the above code

and in the above code we set int total = 1;

ok when it hits the for loop the following happens

int i gets created and set to 0
then i < y gets evaluated : in this case 0 is less than 5
the code total = total * x gets executed: in this case total = 1 * 2 so now total= 2

now i++ gets executed and i now equals 1

the loop starts over. so now it checks i < y again: 1 is less than 5
so total = total * x gets executed again: in this case total = 2 * 2 so now total = 4

then i gets incremented and it checks i < y which it is: 2 is less than 5

so now total = 4 * 2 so now total = 8

i is incremented i < y is checked : 3 is less than 5

total = 8 * 2 so now total = 16

i is incremented and i < y checked : 4 is less than 5

total = 16 * 2 so now total = 32

i is incremented and i < y checked: 5 is NOT less than 5 so the loop stops and total remains 32


Hopefully you see how it works now :)
Some learn by going in depth and making sure they understand every line of code.


That's fine, but you still need practice making the code yourself. In fact, homework like is supposed to do this. The lectures are where you should be analyzing the code.

It would be dumb to turn in homework that you don't understand and then fail the test.


Too bad people DO do that, and they manage to pass the test anyway.
Topic archived. No new replies allowed.