Value Returning Factorial Functions

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)!)

Thanks!!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
using namespace 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()

{
	using namespace 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
{
	using namespace 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
{
	using namespace 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
{
	using namespace std;
	int fact_nkminus = 1;
	for (int i = 1; i <= nk_minus; i++)
		fact_nkminus = fact_nkminus * nk_minus;
	return fact_nkminus;
}
closed account (48T7M4Gy)
Your factorial functions are all the same. Delete all but one including the prototypes. Test first by just commenting them out.
closed account (48T7M4Gy)
Declare variables inside main.
kemort, thanks for looking at this!

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".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
using namespace 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;
}
closed account (48T7M4Gy)
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.
Last edited on
I took the liberty to add some notations to your code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
using namespace 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;
}
Last edited on
Great, thanks I will implement these fixes over the weekend!
Topic archived. No new replies allowed.