GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 24-Mar-2009, 01:13
disk97 disk97 is offline
New Member
 
Join Date: Mar 2009
Posts: 13
disk97 has a little shameless behaviour in the past

Too few parameters in call to 'factorial(int,int)'


Hi, I got problem in my code.

could anyone help me this?

I think this is not that hard for someone but it's hard for me..

my purpose and code below;


<purpose>
The binomial coefficient is defined as n!/(k!(n-k)! when n and k are

nonnegative integers, and 0<=k<=n. If n<0,k<0 or k>n , the binomial

coefficient is considered to be zero. Also, n=0 and k=0 is considered to be 1.

CPP / C++ / C Code:
#include<iostream>
#include<iomanip>

using namespace std;

int factorial(int n,int k);

const double impossible=-9999;

void main()
{
	int n,k;
	cout<<"type in n: ";
	cin>>n;
	cout<<"type in k: ";
	cin>>k;
	char flag = 'y';
	while (flag=='y'||flag == 'Y')
	{
		cout<<"The binomial coefficient C("<<n<<','<<k<<")="<<factorial(n,k);
		cout<<'.'<<endl;
	}
	cout<< "Do you wish to compute another binomial coefficient?\n";
	cout<<"If so, enter 'y' or 'Y', otherwise type anything else.\n";
	cin>>flag;
}


int factorial(int n,int k)
{
	if (0<=k && k<=n)
		return n*factorial(n-1)/k*factorial(k-1)*(n-k)*factorial(n-k-1);
	else if (n<0 && k<0)
		return 0;
	else if (k>n)
		return 0;
	else if (n==0 && k==0)
		return 1;
	else
		return -9999;
}

thank you.
Last edited by LuciWiz : 24-Mar-2009 at 04:29. Reason: Please insert your C++ code between [cpp] & [/cpp] tags
  #2  
Old 24-Mar-2009, 06:07
L7Sqr L7Sqr is offline
Member
 
Join Date: Jul 2005
Location: constant limbo
Posts: 234
L7Sqr is a jewel in the roughL7Sqr is a jewel in the rough

Re: Too few parameters in call to 'factorial(int,int)'


First you declare factorial to take two arguments as described below
Code:
int factorial(int n,int k);
Then you call it recursively with only a single argument
Code:
return n*factorial(n-1)/k*factorial(k-1)*(n-k)*factorial(n-k-1);
Realize that each of those recursive calls only has a single argument.
Either change your declaration (and definition) of factorial to be something like
Code:
int factorial(int x);
or provide the extra parameters to the function that exists now.
__________________
My personal site: Utilities for text processing, debugging, testing and plotting
  #3  
Old 24-Mar-2009, 10:03
disk97 disk97 is offline
New Member
 
Join Date: Mar 2009
Posts: 13
disk97 has a little shameless behaviour in the past

Re: Too few parameters in call to 'factorial(int,int)'


thank you for your answer.
I still have a question that I have to consider about two parameter such as n,k.

That's why I mentioned two parameters in the factorial function.

So, how can I handle two parameters in my code?

Do I have to declare another factorial fuction for k?
  #4  
Old 24-Mar-2009, 12:42
L7Sqr L7Sqr is offline
Member
 
Join Date: Jul 2005
Location: constant limbo
Posts: 234
L7Sqr is a jewel in the roughL7Sqr is a jewel in the rough

Re: Too few parameters in call to 'factorial(int,int)'


A function is non-changing; its definition is compiled to a concrete set of instructions that will always execute the same way given the same parameters. If you wish to have separate behaviors, then you must create separate functions.
For example:
CPP / C++ / C Code:
int foo1(int arg1) {
   int i = 0;
   for (; i < arg1; ++i) {
      printf ("i : %d\n", i);
   }
   return i;
}

Will not do the same thing as
CPP / C++ / C Code:
int foo2 (int arg1, int arg2) {
   int i = 0;
   for (; i < arg1; i += arg2) {
      printf ("i : %d\n", i);
   }
   return i;
}
Nor can you call them the same way
CPP / C++ / C Code:
foo1 (100); /* Ok */
foo1 (100, 10); /* Error, wrong number of arguments */
foo2 (100); /* Error wrong number of arguments */
foo2 (100, 10); /* Ok */
__________________
My personal site: Utilities for text processing, debugging, testing and plotting
  #5  
Old 25-Mar-2009, 13:29
Kimmo Kimmo is offline
Regular Member
 
Join Date: Mar 2007
Location: Finland
Posts: 339
Kimmo is a jewel in the roughKimmo is a jewel in the roughKimmo is a jewel in the rough

Re: Too few parameters in call to 'factorial(int,int)'


I'd just like to point out that your factorial function doesn't actually calculate any factorials, as one would assume from it's name. You would also do well to think about what actually is happening in your recursive calls.

This is what happens (I'll just assume small numbers of n = 3, k = 1. Guess we also have to assume you pass k as the second argument):

Code:
3 * factorial(3 - 1, 1) if (0<=k && k<=n) // true, n = 2, k = 1 2 * factorial(2 - 1, 1) if (0<=k && k<=n) // true, n = 1, k = 1 1 * factorial(1 - 1, 1) else if (k > n) // true, n = 0, k = 1 return 0;
We got a 0. Multiplying 0 by anything yields 0. Dividing 0 by anything yields 0. In fact, from this follows that your program would always tell the answer is 0. Except when k = 0 and k >= n, in which case it crashes because of division by zero error.

The solution is simple: Create a factorial function that actually calculates the factorial and nothing else. Then you can write simply
CPP / C++ / C Code:
factorial(n) / factorial(k) * factorial(n - k);

Your while loop is also infinite, since flag never changes inside it.
  #6  
Old 25-Mar-2009, 21:13
disk97 disk97 is offline
New Member
 
Join Date: Mar 2009
Posts: 13
disk97 has a little shameless behaviour in the past

Re: Too few parameters in call to 'factorial(int,int)'


Thank you very much L7sqr and Kimmo!

I got all of my problems!

 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Can you call a constructor within a destructor, or vise vesa? warrener C++ Forum 0 10-Jun-2008 21:24
Two bugs in my program that just won't go away. randomperson133 Assembly Language 1 23-Mar-2008 08:04
Default parameters ajbharani C++ Forum 13 15-Dec-2007 14:32
Will pay through Paypal if somebody helps me. paritoshcool Assembly Language 0 27-Nov-2007 23:27
Need Help with input files. Efferus C++ Forum 2 24-Nov-2007 17:19

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:39.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.