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

  • Algorithm 1: built-in sort The first algorithm you must implement is in builtin_sort, which must use...

    Algorithm 1: built-in sort The first algorithm you must implement is in builtin_sort, which must use the std::sort or qsort functions built into C++ to sort a vector of words. The function should sort the vector in-place, so it takes the vector as a non-const reference and returns void. This function may only need to be one line long. Both std::sort and qsort have time complexity O(n n). In addition, since these are widely-used library functions, developers have put a...

  • Creating a Calculator Program

    Design a calculator program that will add, subtract, multiply, or divide two numbers input by a user.Your program should contain the following:-The main menu of your program is to continue to prompt the user for an arithmetic choice until the user enters a sentinel value to quit the calculatorprogram.-When the user chooses an arithmetic operation (i.e. addition) the operation is to continue to be performed (i.e. prompting the user for each number, displayingthe result, prompting the user to add two...

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

  • 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:...

  • C++ Part1: Palindrome detector Write a program that will test if some string is a palindrome...

    C++ Part1: Palindrome detector Write a program that will test if some string is a palindrome Ask the user to enter a string Pass the string to a Boolean function that uses recursion to determine if something is a palindrome or not. The function should return true if the string is a palindrome and false if the string is not a palindrome. Main displays the result ( if the string is a palindrome or not Part 2: Compute the Greatest...

  • Can somebody help me with this assignment. I will highly appreciate. Also, please display the output...

    Can somebody help me with this assignment. I will highly appreciate. Also, please display the output as well. I need to use JAVA to to write the program. This is the sample output provided by my professor. HOME WORK: due 09.17.2019 (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private instance variables of the class- the numerator and the denominator. Provide a constructor...

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