Question

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

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).

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
As you know, a numeric type in C is only an approximation to the mathematical entity...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • Write a c++ program where it computes n choose k by using the formula: n(n-1)(n-2)...(n-k+1)/k!. But...

    Write a c++ program where it computes n choose k by using the formula: n(n-1)(n-2)...(n-k+1)/k!. But do not use algorithms where you can use only use integer arithmetic. How is this better than writing a program that uses the formula n!/k!(n-k)!

  • *Write a parallel program pie.c in C or C++ (pie.cc) for Linux that computes an approximation of the number π using a se...

    *Write a parallel program pie.c in C or C++ (pie.cc) for Linux that computes an approximation of the number π using a series with N+1 terms.* --The series sum is partitioned in T non-overlapping partial sums, each computed by T separate child processes created with the fork() library function.* --This program demonstrates data parallelism and interprocess communication using pipes. Each child process could perform a (potentially) long computation on a separate CPU (or core). Depending on the computer architecture, the...

  • Write a C++ program that takes two sets ’A’ and ’B’ as input read from the...

    Write a C++ program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the...

  • How would you combine reactions A-C, shown below, to obtain the overall reaction: NH, () +BH2(g)+0,(3)...

    How would you combine reactions A-C, shown below, to obtain the overall reaction: NH, () +BH2(g)+0,(3) —> 2H,0(g) +HBNH(s) Please select all that apply. 24,(g) +0,(g) → 24,0(3) H BNHz() NH3(g) +BH3(g) HBNH,(s) -> 2H, (g) + HBNH(s) Choose one or more: A multiply A by 2 B. add resulting equations together C. multiply C by 2 D. reverse A E. divide A by 2 E multiply B by 2 G divide B by 2 H. divide C by 2...

  • The description below explains the Strategy Pattern: In Strategy pattern, a class behavior or its algorithm...

    The description below explains the Strategy Pattern: In Strategy pattern, a class behavior or its algorithm can be changed at run time. This type of design pattern falls under the set of behavior patterns. Its Intent is: • Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from the clients that use it. • Capture the abstraction in an interface, bury implementation details in derived classes You have the following operations:...

  • I have to make a Reversi game in C++ where 2 algorithms play against eachother and I do not know ...

    I have to make a Reversi game in C++ where 2 algorithms play against eachother and I do not know how to even start it. In this project you are required to: 1. Implement Reversi for a board size of n x n where 4 ≤ n ≤ 16 and n is always even. 2. Implement two different algorithms that will play Reversi against each other e.g. an algorithm that randomly chooses a valid move may be one of your...

  • 1) Implement a C program that multiplies two matrices with dimension n x m and m...

    1) Implement a C program that multiplies two matrices with dimension n x m and m x r (n, m, r are provided by the user as argument to the main function. The elements of the matrix are generated randomly. Test the program. example: matmult 2 3 4 will multiply two matrices of dimensions 2 x 3 and 3 x 4, the elements are generated randomly. 2) Determine the highest dimension(s) for which the program will crash 3) Please Explain...

  • this program is in C. Write a program that computes the number of elements in an...

    this program is in C. Write a program that computes the number of elements in an array divisible by a user specified number. Declare an integer array of size 7 and read the array elements from the user. Then, read a number k from the user and compute the number of elements in the array divisible by k. Consider the following example. 3 elements in this array are divisible by 2 ({2,2,4}). Sample execution of the program for this array...

  • You know that to multiply a binary integer by 16, all you have to do is...

    You know that to multiply a binary integer by 16, all you have to do is shift the value over by 4 bit positions -- which in hardware can literally be done without a function unit simply by connecting bus wires such that bit k goes to position k+4 and positions 0-3 are connected to ground. Well, it's harder to multiply by 15. However, by using Booth's algorithm, a single 32-bit add/subtract ALU is sufficient to implement multiply of a...

  • Implement the following simple version of QR iteration with shifts for computing the eigenvalues of a...

    Implement the following simple version of QR iteration with shifts for computing the eigenvalues of a general real matrix A Repeat until convergence: 1. o an,n (use corner entry shift) C as 2. Compute QR factorization QR= A- oI 3. A RQ ol (These steps will be casy if you use a package such as MATLAB but more involved if library routine for the QR factorization or write your own.) What convergence test should you use? Test your program on...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT