Write the definition of a function named 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.

Do NOT use the standard library pow() function. Instead, you will need to use a loop to calculate the power.

This is what I have so far:

double powerTo(double, int){
if(int < 0){
return 0;

I'm having trouble with the loop. Please help, thanks.

12 Years
Discussion Span
Last Post by Narue

>double powerTo(double, int){
You need to name the parameters as they're declarations, not placeholders.

>if(int < 0){
How could the type int be compared with a value of type int?

/* x to the nth power */
double powerTo ( double x, int n )
  double ret = 1;

  while ( --n >= 0 )
    ret *= x;

  return ret;

Note that if you turn that in, you'll fail for cheating. You may only use it as a template as I've written it using subtle techniques to hinder plagiarism. ;)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.