Question: Must be C++ Objectives below 1. Write a function called factorialIterative(). This function should take a single BIG integer (bigint) as its parameter n, and
Must be C++ Objectives below
1. Write a function called factorialIterative(). This function should take a single BIG integer (bigint) as its parameter n, and it will compute the factorial n! of the value and return it as its result (as a bigint). Write this functions using a loop (do not use recursion).
2. Write the factorial function again, but using recursion. Call this function factorialRecursive(). The function takes the same parameters and returns the same result as described in 1.
3. Write a function called countCombinationsDirectly(). This function will compute the number of combinations that result from choosing i elements from set of n items. The function should take two bigint values as its parameters n and i, which will be integers >= 0. The function should use the equation 3 above to directly compute the number of combinations. You should use your factorialRecursive() function to compute the factorials in this function.
4. Write a function called countCombinationsRecursive(). This function will also count the number of combinations of choosing i elements from a set of n items. However, you need to implement this calculation as a recursive function, using the general and base cases described above for counting combinations in equation 4 (general case) and equation 5 (base cases). Your function will take the same two bigint parameters as input n and i with values >= 0, and will return a bigint result. 3 You will again be given 3 starting template les like before, an assg-04.cpp le of tests of your code, and a BinomialFunctions.hpp and BinomialFunctions.cpp header and implementation le. As before, you should practice incremental development, and uncomment the tests in the assg-04.cpp le one at a time, and implement the functions in the order shown. If you implement your code correctly and uncomment all of the tests, you should get the following correct output:
Main Assg.cpp
#include
using namespace std;
int main(int argc, char** argv) { // test iterative version of factorial cout << "Testing iterative version factorialIterative() " << endl; cout << "-------------------------------------------------------------" << endl; bigint res; res = factorialIterative(0); cout << "Test base case: factorialIterative(0) = " << res << endl; assert(res == 1);
res = factorialIterative(1); cout << "Test edge case: factorialIterative(1) = " << res << endl; assert(res == 1);
res = factorialIterative(-1); cout << "Test error case: factorialIterative(-1) = " << res << endl; assert(res == 1);
res = factorialIterative(10); cout << "Test general case: factorialIterative(10) = " << res << endl; assert(res == 3628800); res = factorialIterative(12); cout << "Test general case (largest 32 bit int): factorialIterative(12) = " << res << endl; assert(res == 479001600);
res = factorialIterative(20); cout << "Test general case (largest 64 bit int): factorialIterative(20) = " << res << endl; assert(res == 2432902008176640000);
// timing test for factorialIterative, do it 10000 times and see how much time // elapses // https://www.pluralsight.com/blog/software-development/how-to-measure-execution-time-intervals-in-c-- const int NUM_TIMING_LOOPS = 10000; auto start = chrono::high_resolution_clock::now(); for (int testnum = 0; testnum < NUM_TIMING_LOOPS; testnum++) { res = factorialIterative(20); } auto finish = chrono::high_resolution_clock::now(); chrono::duration
// test recursive version of factorial cout << endl; cout << "Testing recursive version factorialRecursive() " << endl; cout << "-------------------------------------------------------------" << endl;
res = factorialRecursive(0); cout << "Test base case: factorialRecursive(0) = " << res << endl; assert(res == 1);
res = factorialRecursive(1); cout << "Test edge case: factorialRecursive(1) = " << res << endl; assert(res == 1);
res = factorialRecursive(-1); cout << "Test error case: factorialRecursive(-1) = " << res << endl; assert(res == 1);
res = factorialRecursive(10); cout << "Test general case: factorialRecursive(10) = " << res << endl; assert(res == 3628800);
res = factorialRecursive(12); cout << "Test general case (largest 32 bit int): factorialRecursive(12) = " << res << endl; assert(res == 479001600);
res = factorialRecursive(20); cout << "Test general case (largest 64 bit int): factorialRecursive(20) = " << res << endl; assert(res == 2432902008176640000);
cout << "Test equivalence of iterative and recursive solutions from n=0 to 20: " << endl; for (int n = 0; n <= 20; n++) { cout << " Testing n = " << n << "..."; assert(factorialIterative(n) == factorialRecursive(n)); cout << " passed" << endl; }
// timing test for factorialRecursive, do it 10000 times and see how much time // elapses // https://www.pluralsight.com/blog/software-development/how-to-measure-execution-time-intervals-in-c-- start = chrono::high_resolution_clock::now(); for (int testnum = 0; testnum < NUM_TIMING_LOOPS; testnum++) { res = factorialRecursive(20); } finish = chrono::high_resolution_clock::now(); elapsed = finish - start; cout << "Elapsed time " << NUM_TIMING_LOOPS << " loops of factorialRecursive(20) " << elapsed.count() << endl;
// test direct calculation version of counting combinations cout << endl; cout << "Testing combinations countCombinationsDirectly() " << endl; cout << "-------------------------------------------------------------" << endl;
int i; int n;
n=5; i=0; res = countCombinationsDirectly(n, i); cout << "Test base case: n=" << n << " choose i=" << i << ": " << res << endl; assert(res == 1); n=5; i=5; res = countCombinationsDirectly(n, i); cout << "Test base case: n=" << n << " choose i=" << i << ": " << res << endl; assert(res == 1); n=0; i=0; res = countCombinationsDirectly(n, i); cout << "Test edge case: n=" << n << " choose i=" << i << ": " << res << endl; assert(res == 1); n=5; i=1; res = countCombinationsDirectly(n, i); cout << "Test general case: n=" << n << " choose i=" << i << ": " << res << endl; assert(res == 5);
n=4; i=2; res = countCombinationsDirectly(n, i); cout << "Test general case: n=" << n << " choose i=" << i << ": " << res << endl; assert(res == 6); n=15; i=6; res = countCombinationsDirectly(n, i); cout << "Test general case: n=" << n << " choose i=" << i << ": " << res << endl; assert(res == 5005); n=15; i=14; res = countCombinationsDirectly(n, i); cout << "Test general case: n=" << n << " choose i=" << i << ": " << res << endl; assert(res == 15);
max factorial using bigint n=20; i=10; res = countCombinationsDirectly(n, i); cout << "Test general case: n=" << n << " choose i=" << i << ": " << res << endl; assert(res == 184756); // timing test for countCobminationsDirectory, do it 10000 times and see how much time // elapses // https://www.pluralsight.com/blog/software-development/how-to-measure-execution-time-intervals-in-c-- start = chrono::high_resolution_clock::now(); for (int testnum = 0; testnum < NUM_TIMING_LOOPS; testnum++) { n=20; i=10; res = countCombinationsDirectly(n, i); } finish = chrono::high_resolution_clock::now(); elapsed = finish - start; cout << "Elapsed time " << NUM_TIMING_LOOPS << " loops of countCombinationsDirectly(" << "n=" << n << " choose i=" << i << ") :" << elapsed.count() << endl;
// test recursive calculation version of counting combinations cout << endl; cout << "Testing combinations countCombinationsRecursive() " << endl; cout << "-------------------------------------------------------------" << endl;
n=5; i=0; res = countCombinationsRecursive(n, i); cout << "Test base case: n=" << n << " choose i=" << i << ": " << res << endl; assert(res == 1); n=5; i=5; res = countCombinationsRecursive(n, i); cout << "Test base case: n=" << n << " choose i=" << i << ": " << res << endl; assert(res == 1); n=0; i=0; res = countCombinationsRecursive(n, i); cout << "Test edge case: n=" << n << " choose i=" << i << ": " << res << endl; assert(res == 1); n=5; i=1; res = countCombinationsRecursive(n, i); cout << "Test general case: n=" << n << " choose i=" << i << ": " << res << endl; assert(res == 5);
n=4; i=2; res = countCombinationsRecursive(n, i); cout << "Test general case: n=" << n << " choose i=" << i << ": " << res << endl; assert(res == 6); n=15; i=6; res = countCombinationsRecursive(n, i); cout << "Test general case: n=" << n << " choose i=" << i << ": " << res << endl; assert(res == 5005); n=15; i=14; res = countCombinationsRecursive(n, i); cout << "Test general case: n=" << n << " choose i=" << i << ": " << res << endl; assert(res == 15);
max factorial using bigint n=20; i=10; res = countCombinationsRecursive(n, i); cout << "Test general case: n=" << n << " choose i=" << i << ": " << res << endl; assert(res == 184756); cout << "Test equivalence of iterative and recursive solutions for counting combinations" << endl << "This is an exhaustive test of all combinations of n and i from 0 to 15" << endl; for (n = 0; n <= 15; n++) { for (i = 0; i <= n; i++) { cout << " Testing (n=" << n << " choose i=" << i << ") equivalence..."; assert(countCombinationsDirectly(n, i) == countCombinationsRecursive(n, i)); cout << " passed" << endl; } }
// timing test for countCobminationsRecursive, do it 10000 times and see how much time // elapses // https://www.pluralsight.com/blog/software-development/how-to-measure-execution-time-intervals-in-c-- start = chrono::high_resolution_clock::now(); for (int testnum = 0; testnum < NUM_TIMING_LOOPS; testnum++) { n=20; i=10; res = countCombinationsRecursive(n, i); } finish = chrono::high_resolution_clock::now(); elapsed = finish - start; cout << "Elapsed time " << NUM_TIMING_LOOPS << " loops of countCombinationsRecursive(" << "n=" << n << " choose i=" << i << ") :" << elapsed.count() << endl;
return 0 to indicate successful completion return 0; }
Bimomial Functions.cpp below this
#include "BinomialFunctions.hpp"
using namespace std;
The hpp below this
#ifndef _BINOMIALFUNCTIONS_H_ #define _BINOMIALFUNCTIONS_H_
#include
using namespace std;
// global definitions typedef long long int bigint; // give long int an alias name of bigint
// Function prototypes for our BinomialFunctions library go here
#endif // _BINOMIALFUNCTIONS_H_
Code must follow this
1. All indentation must be correct according to class style guidelines (2 spaces, no tabs, all code blocks correctly indented by level).
2. All functions (and member functions of classes) must have a function header documentation that gives a short description, documents all input parameters using @param tags, and documents the return value if the function returns a value, using @returns tag. You do not have to document class setter/getter methods, but all other class methods should probably be documented
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
