I have written the following code in order to find the multinomial coefficient, however the same answer, 2.122e-314, always comes out. I've been sitting on this for a while now, and can't find what is missing. We are supposed to do this using recursions. My code is as follows:
#include<iostream>
#include<vector>
#include<cerrno>
#include<cstring>
using namespace std;
double binom(int n,int a)
{
double b;
double i;
for (b = 1, i = 1;i <= a; ++i, --n)
b *= n/i;
return b;
}
double multi(int n, vector<int> a)
{
double b;
int s1 = n;
for ( int s1 = n, b = 1, i = 0; i <= a.size(); ++i, s1 = s1 - a[i-1] )
b *= binom(s1, a[i]);
return b;
}
int main()
{
int n, k;
cout << "dimension k: ";
cin >> k;
vector<int> a(k);
cout << "n: ";
cin >> n;
cout << "a[0],...,a[k-1]: ";
for (int i = 0; i < k; ++i)
cin >> a[i];
cout << "Multinomialcoefficient: " << multi(n,a) << endl;
return 0;
}
Any help is greatly appreciated.