Power Function Help :(

I'm having a tough time making a formula when the raise is higher then one. I have the variable counter set which will increase until the users desired raise. Meanwhile, in that loop, im trying to do base*raise. How can I possibly do this?

I'm trying to use this technique. Lets say the base was ten and the raise was three. I'm trying to set similar to 10*10*10. Only formula I can think of but don't know how to convert it into c++.
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

double power(double base, int raise)
{
	if(raise=0)
		return 1;
	else if (raise=1)
		return base;

	else if (raise >1)
	{
		//After this while loop, counter will equal number of time it needs to be multiplied by the base
		int basetimes;
		int counter =2;
		while (counter<=raise)
		{
		basetimes=base * //base *totalbases of total counter. If four was the counter then it will be base * 4totalbases. This is what im thinking of here. ;
		counter++;
		
		
		}

	}

}


So basically you want to raise the base by a power of "raise"?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
double power(double base, int raise)
{
	if(raise=0)
		return 1;
	else if (raise=1)
		return base;

	else if (raise >1)
	{
		int basetimes;
		int counter =2; //What is the purpose of starting counter from 2?
		while (counter<raise) //Remove the equal sign
		{
		basetimes=basetimes*base; //Or basetimes*=base; for short
		counter++;
		
		
		}

	}

}


EDIT: Wait, what is the purpose of your counter? Doesn't "raise" already tell you how many times it gets multiplied? You need the counter to start from 1 if you want to successfully get the power of basetimes.
Last edited on
Ahh I don't know what I was thinking but you did make my brain think more :P thank you :)
Actually need help again. In my function, return base and return 1 are giving me the correct value, but something is wrong with my last else if statement. For instance I do power(3,2) I get three as my answer. The purpose of my counter is to keep multiplying it until I have reached the raise. If I wanted to raise it by three.

Example


suppose I want to do 3^3


while(2(counter)<3(raise))
basetimes=3*3;
counter++;

Second Loop:
while(3(counter)<=3(raise))
basetimes=9*3;
counter++

answer:27

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
33
34
35
36
37
38
39
40
41

double power(double base, int raise)
{
	if(raise=0)
		return (1);
	else if (raise=1)
		return base;

	else if (raise >1)
	{
		//After this while loop, counter will equal number of time it needs to be multiplied by the base
		int basetimes;
		basetimes=base;
		int counter =2;
		while (counter<=raise)
		{
		basetimes=basetimes*base;
		counter++;
		
		
		}
		return basetimes;

	}
	

}





int _tmain(int argc, _TCHAR* argv[])
{
	
	cout<<"The power of 2 raised to three is " << power(3,2);
	_getch();
	return 0;
}

Yes that is because you are using a single = and not a == to check if it is equal. So you are equaling raise to 0 in the first part, then equaling it to 1 in the second. Which is why you get 3.
1
2
3
4
5

	if(raise=0) //Should be if(raise==0)
		return (1);
	else if (raise=1) //Should be else if(raise==1)
		return base;


Also, remember to set your counter to 1. If it is set at 2, which is what you have now, then you will have an incorrect calculation.

Suppose we go with what you have and we start counter at 2, then you want to raise 5 by a power of 3 raise. In your while (counter<=raise) loop, counter goes up to raise. Since counter is 2, it will be 3 after one round (counter++). You will end up with 5*5=25, and not 5*5*5=125. Well, actually it would work in your case since you used <= and not a single <, but I just don't see the point of counter being initialized at 2.
Last edited on
If raise is less than 0 you should return 1 / value.
Topic archived. No new replies allowed.