‹ Back to the paper
Question 2(iv)
3 marks
Computer ScienceISC 2024 Specimen Computer Science Paper 1Case basedImplementation of algorithms to solve problems
Fill in the blanks marked ?1?, ?2? ... so that the code works.
The following function is a part of some class which computes and returns the value of a number ‘p’…
The following function is a part of some class which computes and returns the value of a number ‘p’ raised to the power ‘q’ ($p^q$). 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?;
}
}(a)[1]
What is the expression or statement at ?1?
(b)[1]
What is the expression or statement at ?2?
(c)[1]
What is the expression or statement at ?3?
Answer
Answer (a)
Blanks: 1
?1? = 1
Answer (b)
Blanks: r = r * p
?2? = r = r * p
Answer (c)
Blanks: 1 / r
?3? = 1 / r