+ 1
Can anybody fix it?
13 odpowiedzi
+ 5
^ is reserved for bitwise XOR
+ 3
Why not use pow function from <math.h>?
+ 1
maybe use for loop and multiply itself on each iteration?
+ 1
+ 1
#include <iostream>
using namespace std;
int pow(int a, int b) {
int base = a;
for(int i=1;i<b;i++){
a*=base;
}
return a;
}
int main()
{
int a;
int b;
cout << "Vnesi stevili za eksponent in osnovo: " << '\n';
cin >> a;
cin>>b;
cout << "Rezultat = " << pow(a,b);
return 0;
}
0
I still do not know what expression is returning in pow() definition.
0
There is no special arithmetic operator.
0
#include <iostream>
#include <math.h>
using namespace std;
double power(double a, int b) {
return pow(a,(double)b);
}
int main()
{
double a = 3.2;
int b = 5;
cout << "Vnesi stevili za eksponent in osnovo: " << '\n';
cin >> a >> b;
power(a,b);
cout << "Rezultat = " << pow(a,b);
}
you can use this way or else use loop
for (int i=1;i<=b;i++)
a*=b;
return a;
here a stores power value where a raised to b
0
Ye, it works. I know that here is type conversion but I have not how to do that.
0
Chicu, in your example I think you use pointer, right?
0
Marjan, no he uses recursion, which is not a good implementation because of memory consumption and program optimization.
0
Vasile i'm not going to argue with you on memory and optimisation since this is a simple function not a game engine,i'm wondering why you didn't said that it should be done in assembly