Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

As you know, a numeric type in C is only an approximation to the mathematical en

ID: 3672227 • Letter: A

Question

As you know, a numeric type in C is only an approximation to the mathematical entity due to bit-size limitations. The goal of this lab is to deal with real problems caused by such differences. Along the way, you will also get more practice using loops.

Recall that n! (read n factorial) is defined to be (1)(2)(3)(4)...(n-1)(n). Many useful computer science problems require the computation of C(n,k) (read n choose k) which is defined as: n! / (k! * (n-k)!), for n>=0 and 0<=k<=n. For this lab, you will compute C(n,k) in several ways.

Two useful properties about C(n,k) are:

(P1) C(n,n-1) = n

(P2) C(n,k) = C(n,n-k)

Several tasks in this assignment ask you for an explanation. You don't need a detailed explanation (1 sentence each suffices), as long as you are clear. You may write your explanations in comments.

TASKS

1-Implement a program that computes C(n,k) directly by computing the 3 factorials (representing variables as ints). What is the largest value of n for which C(n,n-1) returns a correct result? Explain why the results are incorrect (you don't need to figure out the actual relevant bit patterns). What is the largest value for which it does not result in an overflow exception?

2-C(n,k) can be rewritten as n(n-1)(n-2)...(n-k+1)/k!. Implement a program that computes C(n,k) based on this formula. Repeat the questions from above. This program is obviously better than 1 since it works on more input values, but how does it compare to 1 efficiency-wise? The appropriate measure of efficiency here would be the number of multiplies.

3-Use property (P2) to make your solution to 2 more efficient.

4-The formula for C(n,k) can be expressed as the product (1+(n-k)/1) (1+(n-k)/2) (1+(n-k)/3) ... (1+(n-k)/k). Write a program to implement this. You should be able to handle much larger values of n and k now.
Warning: a naive implementation that ignores the type casting needed to convert between ints and intermediate data types will result in incorrect results - you can compare the results from programs 1-3 and this.

5- Another algorithm, which avoids floating point arithmetic modifies 2 as follows: After each multiplication in the numerator (i.e., one outer iteration), divide by as many consecutive factors in k! as possible, starting at 1. For example, the following happens on each outer iteration computing C(8,4):

"Compute" 8. Divide by 1 and 2 (first 2 terms of k!), resulting in 4. We can't divide by the 3rd term of k! since 3 doesn't divide into 4.

Multiply 4 by 7, resulting in 28. Since 3 is not a factor of 28, no division is done.

Multiply 28 by 6, resulting in 168. The remaining 2 terms of k! (3 and 4) are both factors of 168, resulting in 14.

Multiply 14 by 5, resulting in 70. There are no more terms of k! to check.

Hint: you will need a variable to keep track of how many factors of k you have already divided by. In the above example, this variable will have values 2,2,4,4 after the 4 iterations, respectively.

There are better algorithms that require only integer arithmetic, though we do not have enough C knowledge yet to implement these. If you wish to learn more, you can look up dynamic programming algorithms (and arrays to implement them).

Explanation / Answer

Multiple Questions : Answering 1st one.

#include<stdio.h>

int fact(int n){
    if(n==1 || n == 0)
        return 1;
    else
        return n * fact(n-1);
}
int main(){
    int n,k;
    printf("Enter n and k ");
    scanf("%d %d",&n,&k);
    //printf(" %d",fact(n));
    printf(" C(%d,%d) = %d",n,k,fact(n)/(fact(n-k)*fact(k)));
    return 0;
}

------------------------------

What is the largest value of n for which C(n,n-1) returns a correct result? 12

------------------------------

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote