I have an assignment due at 11:59PM. Here are the directions:
Goals: Using functions
Problem: On the site you will find a template that contains a programmer-built function for factorial. You will modify this program by adding another function that will perform the following calculation for combinations: n!/(k!(n-k)!)
where n and k are integer values.
Your new function should be called from main and will call the factorial function. Your 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 assignment in two steps. In the first step write the new function to do the division n/k(n-k))
and test it. Once your program is performing this calculation correctly, then modify the numerator and denominator of the fraction to call the factorial function.
Things to think about….
• Why was the limit of 15 imposed for factorial of a number? (Hint: see what would happen if you removed the check and used larger numbers.)
• How could the program be modified to calculate correctly the factorial of larger numbers?
I'm not really sure how to use functions at the moment. I'm also not sure where the value calculated will be output. Would they be to the screen or two a file or is it my choice?
I will try to post some code in a few minutes but it will probably will not be good.
I was told there should be no if statements, only while loops for error checking.
int factorial(int n)
{
int product = 1;
for(; n > 1; n--)
{
product *= n;
}
return product;
}
I'm just not sure where to even start. I'm not sure how to incorporate the given function into my own function to calculate n!/(k!(n-k)!). I was told "Factorial only takes in one number. The number is k. Leave factorial alone and create a new function to do the calculations."
I would appreciate any and all help since time is quickly running out.
I assume your professor means in YOUR new function you call the other programmers function.
so...
1 2 3 4 5 6 7 8
int myFunction(int x, int y)
{
//n!/(k!(n-k)!)
//so we are looking for n factorial divided by k factorial times n-k factorial,
// fortunately the other programmer gave
//us a factorial function.... if we let x = n and y = k
return factorial(x)/((factorial(y)*(factorial(x-y));
}
You create something like that and call it from your main function.
I thought that was how to call the given function I was just not sure if the syntax. I was told error checking should be done in the main function.
In the declaration for my created function does the type need to be written twice if it is the same for both arguments?
How does the return value work. When the function is called the value is calculated, then return is true, and the value is displayed wherever the function is called?
#include <iostream>
usingnamespace 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;
}
I guess the the main function I create will look similar to that main?
#include <iostream>
usingnamespace std;
int factorial (int); //function prototype or declaration
int calculation (int);
int main ()
{
int answer(0), number;
cout<<"Please enter a positive integer value less than 15. \n";
cin >> number;
while (number < 1 || number > 15)
{
cout << "You entered an incorrect integer.";
cout << "Please enter a positive integer value less than 15. \n";
cin >> number;
}
answer = calculation(number);
cout << "The factorial of " << number << "is" << answer << ".\n";
}
int calculation(int n, int k)
{
return factorial(n)/((factorial(k)*(factorial(n-k))));
}
int factorial(int n)
{
int product = 1;
for(; n > 1; n--)
{
product *= n;
}
return product;
}
That looks wrong and the main function looks basically like copy and paste but I'm not sure how to syntactically do this.
Yes the type needs to be written twice, along with a name for each type.[in the function definition, the names can be excluded in the function prototype so it can look like int myFunction(int, int) ]
The return value returns the functions return type(in this case that type is 'int'), the way I typed it is a bit shorthand, this should make it a bit clearer for you.
1 2 3 4 5 6 7 8 9 10
int myFunction(int x, int y)
{
//lets make an int variable
int temp;
//lets assign the int variable the value (n!/k!(n-k!))
temp = factorial(x)/((factorial(y)*(factorial(x-y)); //caling the other programmers function
return temp; //return temp means it is sending the value
//stored in temp back to the function that called it(main() in this case)
}
you are going to call myFunction from main - so you will have to get two numbers from the user. you can keep your professors' error checking all you will have to do is get a "somenumber" and a "someothernumber" to pass to the myFunction function and then output the value myFunction returns to the console.
I have to create my own error checking in main I think though. Why will I have to get two numbers? I'm assuming a number for n and a number for k? Sorry if I'm missing basic stuff. I can never find time to read the theory behind what the assignment calls for before the assignment is due.
Yeah you will have to make your own error checking because the code your teacher provided only checks for one input, you're checking for two (yes you are assigning a number for n and k)
int calculation(int n, int k)
{
int calc;
calc = factorial(n)/(factorial(k)*(factorial(n-k)));
return calc;
}
int factorial(int n)
{
int product = 1;
for(; n > 1; n--)
{
product *= n;
}
return product;
}
no - when you're professor said an "identifier" - in this context, meant a "variable name".
the function int factorial(int n) is just that, a function.
The program logic should have looked like this :
program starts(main) -> declare 3 variables(two for input, one to hold the return value) -> get input for those variables -> call calculation(input variable1, input variable2) and assign that value to your 3rd declared variable -> output to console.