Write the definition of a function powerTo, which receives two parameters. The first is a double and the second is an int. The function returns a double. If the second parameter is negative, the function returns 0. Otherwise, it returns the value of the first parameter raised to the power of the second.
.
.
Click on the title for the solution
.
.
.
Click on the title for the solution
.
.
This is the answer:
:
double powerTo ( double x , int n ) { double prod = 1.0; if ( n < 0 ) return 0.0; while ( n > 0 ) { prod *= x; --n; } return prod; }
Comments
Post a Comment