‹ Back to the paper
Question 2(iv)
3 marks
Computer ScienceISC 2024 Computer Science Paper 1Case basedImplementation of algorithms to solve problems
Fill in the blanks marked ?1?, ?2? ... so that the code works.
int small_dig(int n) { int min = ?1? ; while (n != 0) { int q=n/10; int r = ?2? * 10; min = r > min…
int small_dig(int n)
{ int min = ?1? ;
while (n != 0)
{
int q=n/10;
int r = ?2? * 10;
min = r > min ? ?3? : r;
n=q;
}
return min;
}The following function is a part of some class which is used to find the smallest digit present in a number. 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.
(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: 9
?1? = $9$: the largest possible digit, so any digit found is smaller or equal.
Answer (b)
Blanks: n - q
?2? = $n - q$: since $n - q * 10$ gives the last digit (by operator precedence).
Answer (c)
Blanks: min
?3? = $min$: keep the current minimum when the digit is larger.