(iv)
The following function is a part of some class which computes and returns the value
of a number ‘p’ raised to the power ‘q’ (pq). There are some places in the code
marked by ?1? , ?2? , ?3? which must be replaced by an expression / a statement so
that the function works correctly.
double power ( double p , int q )
{
double r = ?1? ;
int c = ( q< 0 ) ? -q : q ;
if ( q == 0)
return 1 ;
else
{
for (int i = 1; i <= c ;?2?, i++);
return (q>0)? r : ?3?;
}
}
Solution
?1? : 1.0
?2? : r = r*p
?3? : 1/r