Home » Programming & Data Structure » Programming and data structure miscellaneous » Question

Programming and data structure miscellaneous

Programming & Data Structure

  1. 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?
    1. p = n*(n – 1)*(n – 2)/ 6;
    2. p = n*(n – 1)/ 2*(n – 2)/ 3;
    3. p = n*(n – 1)/ 3*(n – 2)/ 2;
    4. p = n * (n - 1) * (n – 2)/ 6.0;
Correct Option: B

P = nC3

=
n(n - 1)(n - 2)
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 =
n(n - 1) / 2
×
(n - 2) / 3
P1P2

P1 will be having no error, thus P will be more accurate Option (c)
P =
n(n - 1) / 3
×
(n - 2) / 2
P1P2

There is possibility of truncation in P1, the accuracy of P is less



Your comments will be displayed only after manual approval.