So I'm fairly new to C++ and I need some help. I need to involute (I think that's how this action is called in English) the variable a by the variable b like below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
usingnamespace std;
int main(){
int a, b;
a = 3;
b = 4;
a = a ^ b; //So basically a = a * a * a * a;
cout << a;
return 0;
}
This code prints me a 0 instead of 81 as it should. What am I doing wrong? Typing in #include <cmath> didn't help aswell so that's not the case. Could someone help me out here? Thanks in advance! :)
#include <iostream>
int ipow(int, int);
int main()
{
int b = 3;
int e = 4;
std::cout << ipow(b, e) << "\n";
}
int ipow(int base, int exp)
{
int result = 1;
while (exp)
{
if (exp & 1)
{
result *= base;
}
exp >>= 1;
base *= base;
}
return result;
}
int ipow(int base, int exp)
{
if (exp < 0)
{
return -1; // only positive exponents calculated
}
if (exp == 0)
{
return 1;
}
int num = base;
for (int i = 1; i < exp; i++)
{
base *= num;
}
return base;
}
I never said it was wrong. The first version you posted was a bit more complex than I think should be, so I posted my version. And yes, the second way you posted is the same thing I posted.