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

Just question about p(2)!!!!! (Number 4) (P1) C(n,n-1) = n (P2) C(n,k) = C(n,n-k

ID: 3884198 • Letter: J

Question

Just question about p(2)!!!!! (Number 4) (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. Also, you may assume that all inputs are legal for this lab (so, no error checking needed). TASKS Implement a program that inputs n and k and then computes C(n,k) directly by first computing the 3 factorials: n!, k!, and (n-k)!. You probably want to do this by writing one loop each, mostly cut/paste of the first loop. You should represent variables as ints. What is the smallest value of n for which C(n,n-1) returns an incorrect result? Explain why the results are incorrect (you don't need to figure out the actual relevant bit patterns). Repeat 1, except using "long int" in 3.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, using ints. Hint: the numerator is obviously an iteration, but what is the iteration variable?). 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? An appropriate measure of efficiency here would be the number of multiplies. 4. Use property (P2) to make your solution to 3 more efficient (in some cases). 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. Note that the terms in the product may not be integers. 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 previous tasks and this. !!!!!!!!!!!!!!!!!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). !!!!!!!!!!!!!!!!!!! Just question about p(2)!!!!! (Number 4) (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. Also, you may assume that all inputs are legal for this lab (so, no error checking needed). TASKS Implement a program that inputs n and k and then computes C(n,k) directly by first computing the 3 factorials: n!, k!, and (n-k)!. You probably want to do this by writing one loop each, mostly cut/paste of the first loop. You should represent variables as ints. What is the smallest value of n for which C(n,n-1) returns an incorrect result? Explain why the results are incorrect (you don't need to figure out the actual relevant bit patterns). Repeat 1, except using "long int" in 3.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, using ints. Hint: the numerator is obviously an iteration, but what is the iteration variable?). 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? An appropriate measure of efficiency here would be the number of multiplies. 4. Use property (P2) to make your solution to 3 more efficient (in some cases). 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. Note that the terms in the product may not be integers. 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 previous tasks and this. !!!!!!!!!!!!!!!!!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). !!!!!!!!!!!!!!!!!!! (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. Also, you may assume that all inputs are legal for this lab (so, no error checking needed). TASKS Implement a program that inputs n and k and then computes C(n,k) directly by first computing the 3 factorials: n!, k!, and (n-k)!. You probably want to do this by writing one loop each, mostly cut/paste of the first loop. You should represent variables as ints. What is the smallest value of n for which C(n,n-1) returns an incorrect result? Explain why the results are incorrect (you don't need to figure out the actual relevant bit patterns). Repeat 1, except using "long int" in 3.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, using ints. Hint: the numerator is obviously an iteration, but what is the iteration variable?). 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? An appropriate measure of efficiency here would be the number of multiplies. 4. Use property (P2) to make your solution to 3 more efficient (in some cases). 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. Note that the terms in the product may not be integers. 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 previous tasks and this. !!!!!!!!!!!!!!!!!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). !!!!!!!!!!!!!!!!!!! (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. Also, you may assume that all inputs are legal for this lab (so, no error checking needed). TASKS Implement a program that inputs n and k and then computes C(n,k) directly by first computing the 3 factorials: n!, k!, and (n-k)!. You probably want to do this by writing one loop each, mostly cut/paste of the first loop. You should represent variables as ints. What is the smallest value of n for which C(n,n-1) returns an incorrect result? Explain why the results are incorrect (you don't need to figure out the actual relevant bit patterns). Repeat 1, except using "long int" in 3.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, using ints. Hint: the numerator is obviously an iteration, but what is the iteration variable?). 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? An appropriate measure of efficiency here would be the number of multiplies. 4. Use property (P2) to make your solution to 3 more efficient (in some cases). 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. Note that the terms in the product may not be integers. 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 previous tasks and this. !!!!!!!!!!!!!!!!!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

#include<iostream>

using namespace std;

int main(){

   double n,k;
   double p;

   cout << "Enter n and k : " ;
   cin >> n >> k;
   p = 1;
   for (int i = 1; i<=k; i++){
      p = p * (1+ (n-k)/i);
   }  
   cout << "C(" << n <<"," << k << ") = " << p << endl;
   return 0;
}