-
Suppose n and p are unsigned int variables in a C program. We wish to set p to nC3 . If n is large, which one of the following statements is most likely to set p correctly?
-
- p = n*(n – 1)*(n – 2)/ 6;
- p = n*(n – 1)/ 2*(n – 2)/ 3;
- p = n*(n – 1)/ 3*(n – 2)/ 2;
- p = n * (n - 1) * (n – 2)/ 6.0;
- p = n*(n – 1)*(n – 2)/ 6;
Correct Option: B
P = nC3
= | |
6 |
If we multiply, n, (n – 1) and (n – 2) together, it may go beyond the range of unsigned integer. (\ option (a) and (d) are wrong) For all values of n, n(n – 1)/2 will always be an integer value, But for n(n – 1)/3, it is not certain.
Take options (b)
P = | × | |||
P1 | P2 |
P1 will be having no error, thus P will be more accurate Option (c)
P = | × | |||
P1 | P2 |
There is possibility of truncation in P1, the accuracy of P is less