PRASHNIKAप्रश्निका
Back to the paper

The following function is a part of some class which is used to find the smallest digit present in…

Computer Science20243 marksFill in
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.
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;
}
(a)[1.0]
What is the expression or statement at ?1?
(b)[1.0]
What is the expression or statement at ?2?
(c)[1.0]
What is the expression or statement at ?3?

Answer

Answer (a)

AI
?1? is `9` (the largest possible digit value, so the first digit compared correctly becomes the running minimum).

Answer (b)

AI
?2? is `n - q` (so `r = n - q * 10` extracts the last digit of n, since q = n/10).

Answer (c)

AI
?3? is `min` (keep the existing minimum when the new digit r is not smaller than it).
Implementation of algorithms to solve problems

From ISC 2024 Computer Science Paper 1, question 2(iv).