Question

Please solve the program in C++(Recursive) Write a function called factorialIterative(). This function should take a...

Please solve the program in C++(Recursive)

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1)

int factorialIterative(long n)
{
int i,f=1;
for(i=1;i<=n;i++)
f=f*i;
return(f);
}

2)

unsigned long factorialRecursive(int n)

{

if(n > 1)

return n * factorialRecursive(n - 1);

else

return 1;

}

3)

int fact(int no)
{
int i,f=1;
for(i=1;i<=no;i++)
f=f*i;
return(f);
}

int nCr(int n, int r)

{

    return fact(n) / (fact(r) * fact(n - r));

}

4)

int fact(int n);

  

int nCr(int n, int r)

{

    return fact(n) / (fact(r) * fact(n - r));

}

  

// Returns factorial of n

int fact(int n)

{

    int res = 1;

    for (int i = 2; i <= n; i++)

        res = res * i;

    return res;

}

Add a comment
Know the answer?
Add Answer to:
Please solve the program in C++(Recursive) Write a function called factorialIterative(). This function should take a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ PROGRAM ONLY! For this lab you need to write a program that will read in...

    C++ PROGRAM ONLY! For this lab you need to write a program that will read in two values from a user and output the greatest common divisor (using a recursive implementation of the Euclidean algorithm) to a file. In Lab #3, you implemented this program using an iterative method. Greatest Common Divisor In mathematics, the greatest common divisor (GCD) of two or more integers (when at least one of of them is zero then the larger value is the GCD....

  • USING PYTHON! The second function you will write should be called ‘varOrd’. Your function should take...

    USING PYTHON! The second function you will write should be called ‘varOrd’. Your function should take two (2) arguments, an integer and a string. The function should return one (1) integer calculated as follows. If the input integer is 1, the function should return the sum of the return value of the ord() function on each character of the string (e.g., varOrd(1, ‘cat’) should return the result of ord(‘c’) + ord(‘a’) + ord(‘t’)). If the input integer is 2, the...

  • Special Problem: Automated Factorial Create a MATLAB function called xfact that uses a loop to calculate...

    Special Problem: Automated Factorial Create a MATLAB function called xfact that uses a loop to calculate the factorial of an integer. Your function should take a single integer input. Your function should compute the factorials of every integer from 1 to the integer provided by the input. Test your function by prompting the user to enter an integer N and, using a loop create an array named X of length N that contains the factorials of the numbers 1 to...

  • Write a python program (recursive.py) that contains a main() function. The main() should test the following...

    Write a python program (recursive.py) that contains a main() function. The main() should test the following functions by calling each one with several different values. Here are the functions: 1) rprint - Recursive Printing: Design a recursive function that accepts an integer argument, n, and prints the numbers 1 up through n. 2) rmult - Recursive Multiplication: Design a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times...

  • i need this in c program please Computing Factorials Lab #2 Name your program lab2.c Start...

    i need this in c program please Computing Factorials Lab #2 Name your program lab2.c Start by asking the user to enter a number between 1 and 15 Check if the user has complied. Keep asking the user for a number between 1 and 15 until the user complies. Then ask if recursion is desired or not. If recursion is not desired call a function to calculate N! (where N is the number entered) – If recursion is required call...

  • c++ page 2 ]. Write a C++ program that uses a recursive function to compute factorial...

    c++ page 2 ]. Write a C++ program that uses a recursive function to compute factorial (n!) Include comments in your code. dawoe llustrate with a ]. [extra credit] What are function or class "templates" in C++ ? ] Provide (short!) C++ code fragments which demonstrate how to write and re files. your code fragment should include preprocessor directive(s), all needed I/O declarations, and illustrative code for writing and reading data.eb ert benistno geami 10]. Write C++ code to sort...

  • Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters...

    Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times y. Remember, multiplication can be performed as repeated addition as follows: 5×6=6+6+6+6+6 2. Recursive findings Write a recursive boolean method named reFinding. The method should search an array for a specified value, and return true if the value is found in the array, or false if the value is not found in...

  • Need help writing four short recursive programs: Write a program called Permutations.java that takes an integer...

    Need help writing four short recursive programs: Write a program called Permutations.java that takes an integer command-line argument, n, and enumerates all permutations of the first n letters in the English alphabet. Be sure to handle exceptions of the types: i) bad user input (i.e., double, string), ii) n > 26 or n < 0. Write a program called PermutationsK.java that takes two integers, n and k, as command-line arguments and prints out all P(n,k) permutations that contain exactly k...

  • Please write program in C++ Write a function called randCount with no parameters. The function should...

    Please write program in C++ Write a function called randCount with no parameters. The function should randomly generate 5 numbers between 8 to 15 and counts how many times a multiple of 2 occurred. Return the answer to the calling function. please write the program in C++

  • PYTHON The first function you will write should be called ‘countDown’. Your function should take zero...

    PYTHON The first function you will write should be called ‘countDown’. Your function should take zero (0) arguments. The function should return one (1) list containing integers from 10 to 1 and finally, the string “Liftoff!”. (i.e., [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, ‘Liftoff!’]). You function must be recursive (i.e., it should contain a call to itself within the function body). You will get NO credit if you use any loops (for, while, etc). 3. Function...

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