I have provided a template that contains a programmer-built function for factorial. I need to modify this program by adding another function that will perform the following calculation for combinations:
(nk) = (n!)/(k!(n-k)!)
where n and k are integer values.
the new function should be called from main and will call the factorial function. this new function should return the calculation above to main, and main should output the result. Do NOT use the same identifier (variable name) in more than one function. Make sure you have some test data before you try to write the program.
Hint: Complete this in two steps. In the first step write the new function to do the division
(n)/(k(n-k))
and test it. Once the program is performing this calculation correctly, then modify the numerator and denominator of the fraction to call the factorial function.
here is what I have. the lines beginning with // are what I'm missing.
/*
The function is to calculate and the facorial of an integer number. The main function will
ask the user to input a number, then call the function, and output the factorial.
input: an integer number
output: The factorial of the integer number entered
processing: A function will be called that employs a loop to calculate the running product
for factorial of a number.
*/
#include <iostream>
using namespace std;
int factorial (int); //function prototype or declaration
int main()
{
int somenumber, result;
cout<<"Please enter a positive integer value less than 15. "<<endl;
cin>>somenumber;
while (somenumber < 1 || somenumber >15)
{
cout<<"You entered an incorrect integer value!"<<endl;
cout<<"Please enter a positive integer value less than 15. "<<endl;
cin>>somenumber;
}
result = factorial(somenumber);
cout<< "The factorial of "<<somenumber <<" is "<<result<<"."<<endl;
return 0;
}
//Function to calculate and return the factorial of an integer
//Function requires an integer value from the function call as
//input and will return the integer factorial.
//Function utilizes a loop to calculate the factorial.
//Pre: a positive integer value has been entered and confirmed to
// be greater less than 15
//Post: The calculated factorial is returned to the function call
int factorial(int n)
{
int product = 1;
for(; n > 1; n--)
{
product *= n;
}
return product;
}
THANKS!
(nk) = (n!)/(k!(n-k)!)
where n and k are integer values.
the new function should be called from main and will call the factorial function. this new function should return the calculation above to main, and main should output the result. Do NOT use the same identifier (variable name) in more than one function. Make sure you have some test data before you try to write the program.
Hint: Complete this in two steps. In the first step write the new function to do the division
(n)/(k(n-k))
and test it. Once the program is performing this calculation correctly, then modify the numerator and denominator of the fraction to call the factorial function.
here is what I have. the lines beginning with // are what I'm missing.
/*
The function is to calculate and the facorial of an integer number. The main function will
ask the user to input a number, then call the function, and output the factorial.
input: an integer number
output: The factorial of the integer number entered
processing: A function will be called that employs a loop to calculate the running product
for factorial of a number.
*/
#include <iostream>
using namespace std;
int factorial (int); //function prototype or declaration
int main()
{
int somenumber, result;
cout<<"Please enter a positive integer value less than 15. "<<endl;
cin>>somenumber;
while (somenumber < 1 || somenumber >15)
{
cout<<"You entered an incorrect integer value!"<<endl;
cout<<"Please enter a positive integer value less than 15. "<<endl;
cin>>somenumber;
}
result = factorial(somenumber);
cout<< "The factorial of "<<somenumber <<" is "<<result<<"."<<endl;
return 0;
}
//Function to calculate and return the factorial of an integer
//Function requires an integer value from the function call as
//input and will return the integer factorial.
//Function utilizes a loop to calculate the factorial.
//Pre: a positive integer value has been entered and confirmed to
// be greater less than 15
//Post: The calculated factorial is returned to the function call
int factorial(int n)
{
int product = 1;
for(; n > 1; n--)
{
product *= n;
}
return product;
}
THANKS!