-
PowerOf Function
Code:
/* Title: c5 Review 9 (c05r09) */
#include <iostream.h>
// ----------------------------------------
void Introduction()
{
IGNORE THIS PART
}
// ----------------------------------------
double PowerOf(double x, int n)
{
CODE NEEDED HERE
return ();
}
// ----------------------------------------
int main()
{
double x;
cout << "Enter the base number: ";
cin >> x;
int n;
cout << "Enter the exponent: ";
cin >> n;
PowerOf(x, n);
return(0);
}
I need help figuring out how to get a number to a power and it displaying the results. What I mean is, I define x as 2 and say n as 3. That would be 2^3, or the number 8. Inside the PowerOf() function, I can't figure out the code to do that. I know it has something to do with a for loop though.
-
>I can't figure out the code to do that.
How would you do it on paper? Or with a calculator? Once you know that the code for such a simple function practically writes itself.
-
I know how to do it on the calculator, and on paper, I just can't get the for statement to give me what I want. It's probably a really simple 3-4 line code I'm just dancing around. Help?
-
>I just can't get the for statement to give me what I want
...
Code:
double PowerOf ( double base, int n )
{
double ret = 1.0;
for ( int i = 0; i < n; i++ ) {
ret *= base;
}
return ret;
}