My program won't run b/c I'm getting an error that "function 'int factorial(int)' already has a body". Also, I'm not sure if I declared
the variables in their rightful places and maybe that has something to do with the error message I'm getting.
Making Function to output the number of distinct ways in which you can pick k objects out of a set of n objects
(both n and k should be positive integers). This number is given by the following formula:
C(n, k) = n!/(k! * (n - k)!)
#include <iostream>
usingnamespace std;
//global variables
int n, k;
int nk_minus = (n - k);
int distinct;
int fact_n, fact_k, fact_nkminus;
//declarations
int factorial(int n);//returns factorial of n
int factorial(int k);//returns factorial of k
int factorial(int nk_minus);//returns factorial of nk_minus
int main()
{
usingnamespace std;
cout << "Enter a number for n." << endl;
cin >> n;
cout << "Enter a number for k." << endl;
cin >> k;
while//Return invalid messages
(n <= 0); (k <= 0); (k > n);
{
cout << "That value is invalid." << endl;
}
//function calls
fact_n = factorial(n);
fact_k = factorial(k);
fact_nkminus = fact_nkminus * nk_minus;
//loop to test different values of n and k 5 times.
int distinct = 1;
for (int i = 1; i <= 5; i++)
distinct = fact_n / (fact_k * (fact_nkminus));//C(n, k) = n!/ (k!* (n - k)!)
return distinct;//outputs number of distinct ways
}
//definitions
int factorial(int n)//returns factorial of n
{
usingnamespace std;
int fact_n = 1;
for (int i = 1; i <= n; i++)
fact_n = fact_n * n;
return fact_n;
}
int factorial(int k)//returns factorial of k
{
usingnamespace std;
int fact_k = 1;
for (int i = 1; i <= k; i++)
fact_k = fact_k * k;
return fact_k;
}
int factorial(int nk_minus)//returns factorial of nk_minus
{
usingnamespace std;
int fact_nkminus = 1;
for (int i = 1; i <= nk_minus; i++)
fact_nkminus = fact_nkminus * nk_minus;
return fact_nkminus;
}
What do you mean by "Delete all but one including the prototypes"?
I made some edits and showed it to a friend of mine and he says the source code looks good so he couldn't see why it wouldn't run. But it still won't run and I get the error that "function 'int factorial(int)' already has a body".
#include <iostream>
usingnamespace std;
//declarations
int factorial(int n);//returns factorial of n
int factorial(int k);//returns factorial of k
int factorial(int nk_minus);//returns factorial of nk_minus
int main()
{
//variables
int n, k;
int nk_minus = (n - k);
int distinct;
int fact_n, fact_k, fact_nkminus;
cout << "Enter a number for n that is greater than or equal to zero." << endl;
cin >> n;
cout << "Enter a number for k that is greater than or equal to zero. It must also be greater than n" << endl;
cin >> k;
while//Return invalid messages
(n <= 0 || k <= 0 || k > n)
{
cout << "That value is invalid." << endl;
}
//loop to test different values of n and k 5 times.
for (int i = 1; i <= 5; i++)
distinct = fact_n / (fact_k * (fact_nkminus));//C(n, k) = n!/ (k!* (n - k)!)
//function calls
fact_n = factorial(n);
fact_k = factorial(k);
fact_nkminus = fact_nkminus * nk_minus;
return 0;//outputs number of distinct ways
}
//definitions
int factorial(int n)//returns n!
{
int fact_n = 1;
for (int i = 1; i <= n; i++)
fact_n = fact_n * n;
return fact_n;
}
int factorial(int k)//returns k!
{
int fact_k = 1;
for (int i = 1; i <= k; i++)
fact_k = fact_k * k;
return fact_k;
}
int factorial(int nk_minus)//returns (n-k)!
{
int fact_nkminus = 1;
for (int i = 1; i <= nk_minus; i++)
fact_nkminus = fact_nkminus * nk_minus;
return fact_nkminus;
}
lines 5 and 6 are effectively the same because reference to n and k in the prototype are ignored by the compiler. Delete one of the lines and that should solve the error.
Actually 5, 6 and 7 are the same. You will need to delete two lines.
#include <iostream>
usingnamespace std;
//declarations
int factorial(int n);//returns factorial of n
// next two not needed - one procedure will do the whole thing.
int factorial(int k);//returns factorial of k
int factorial(int nk_minus);//returns factorial of nk_minus
int main()
{
//variables
int n, k;
int nk_minus = (n - k);
// you won't know what nk_minus is until after the user has entered n and k
int distinct;
int fact_n, fact_k, fact_nkminus;
cout << "Enter a number for n that is greater than or equal to zero." << endl;
cin >> n;
cout << "Enter a number for k that is greater than or equal to zero. It must also be greater than n" << endl;
cin >> k;
while//Return invalid messages where should these lines be to give the user an opportunity to enter valid numbers?
(n <= 0 || k <= 0 || k > n)
// if an invalid n or k is entered, what will happen? Think "Infinite loop"
{
cout << "That value is invalid." << endl;
}
//loop to test different values of n and k 5 times.
for (int i = 1; i <= 5; i++) // what is this for?
distinct = fact_n / (fact_k * (fact_nkminus));
// = n! / ( k! * (n - k)! )
//each of the ! represents a need for a call to factorial(int n), and
// your user entered n and k for you. . . can't you use them here?
//function calls - where and how will you use these values? They can't be used in line 34, right?
fact_n = factorial(n);
fact_k = factorial(k);
fact_nkminus = fact_nkminus * nk_minus;
return 0;//outputs number of distinct ways - actually ouputs nothing to the user
// how can you report the value of distinct before exiting the program?
}
//definitions
int factorial(int n)//returns n!
{
int fact_n = 1;
for (int i = 1; i <= n; i++)
fact_n = fact_n * n; // what is the multiplier here? Does it change in this loop?
return fact_n;
}
int factorial(int k)//returns k!
{
int fact_k = 1;
for (int i = 1; i <= k; i++)
fact_k = fact_k * k;
return fact_k;
}
int factorial(int nk_minus)//returns (n-k)!
{
int fact_nkminus = 1;
for (int i = 1; i <= nk_minus; i++)
fact_nkminus = fact_nkminus * nk_minus;
return fact_nkminus;
}