Reputation: 87
I am writing a simple program to calculate 6^5 using a function. Here's my code
#include <iostream>
using namespace std;
int raiseToPower(int &base, int exponent){
for (int i = 1; i < exponent; i = i + 1){
base= base * base;
}
return base;
}
int main() {
int base = 6;
cout << "6^5 is " << raiseToPower(base, 5)<< endl;
return 0;
}
The result is
6^5 is -683606016
instead of 7776
Can you guys explain to me why I got this result? I am not looking for another way to write this code. I know there are simpler ways but I am trying to understand what went wrong
Upvotes: 1
Views: 128
Reputation: 5156
Your calculation is incorrect. The value of variable base should not change. Try this:
int raiseToPower(int &base, int exponent){
int result = 1;
for (int i = 1; i <= exponent; i = i + 1){
result = result * base;
}
return result;
}
Upvotes: 2
Reputation: 310930
Let's consider this loop
for (int i = 1; i < exponent; i = i + 1){
base= base * base;
}
After the first iteration when i is equal to 1 you have
base= base * base;
that is the result is base ^ 2
.
When i = 2 you have
base= base^2 * base^2;
that is the result is base^4.
When i is equal to 3 you have
base= base^4 * base^4;
that is the result is base^8.
When i is equal to 4 you have
base= base^8 * base^8;
that is the result is base^16.
It seems the resulted value is too big to be accomodated in an object of the type int.
Also it is not a good idea when an argument is passed by reference. And the second parameter should have the type unsigned int.
Here is a demonstrative program that shows how the function can be implemented.
#include <iostream>
long long int raiseToPower( int base, unsigned int exponent )
{
long long int result = 1;
while ( exponent-- )
{
result *= base;
}
return result;
}
int main()
{
int base = 6;
std::cout << "6^5 is " << raiseToPower(base, 5) << std::endl;
return 0;
}
Its output is
6^5 is 7776
Upvotes: 2
Reputation: 1563
Why are you using pass by reference. Setting as much const
as possible reduces the odd of making mistakes:
#include <iostream>
using namespace std;
int raiseToPower(const int base, const int exponent) {
int result = base;
for (int i = 1; i < exponent; i = i + 1){
result = result * base;
}
return result;
}
int main() {
int base = 6;
cout << "6^5 is " << raiseToPower(base, 5)<< endl;
return 0;
}
If it has to be pass by reference you could do this:
#include <iostream>
using namespace std;
void raiseToPower(const int base,
const int exponent,
int &result)
{
result = base;
for (int i = 1; i < exponent; i = i + 1){
result = result * base;
}
}
int main() {
int base = 6;
int result;
raiseToPower(base, 5, result);
cout << "6^5 is " << result << endl;
return 0;
}
Upvotes: 3
Reputation: 1498
You probably want your loop like this:
int result = 1;
for (int i = 0; i < exponent; i = i + 1) {
result = result * base;
}
return result;
Also, you are taking your base parameter to raiseToPower by reference - which means you are changing the base variable in main - probably not what you want to do.
Upvotes: 2