Re: please check my homework
- From: David Marsh <dmarsh@xxxxxxxx>
- Date: Mon, 31 Oct 2005 18:42:46 GMT
Michael Jørgensen wrote:
"David Marsh" <dmarsh@xxxxxxxx> wrote in message news:sAw7f.306880$tl2.244927@xxxxxxxxxxx
This is a homework assignment for extra credit. The statistical material has not been covered in class. Is my bidist() function correct? It seems to work with the examples I have tried, but I would appreciate your comments and suggestions. PLEASE don't write any code. Error checking deliberately omitted for now and I am aware of the potential for factorials to exceed LDBL_MAX.
David ---------------------------------------------------------------
<snip code>
Write also a test function that calculates: f(0) + f(1) + f(2) + ... + f(n-1) + f(n). This sum should be exactly 1.0 for all valid values of n and p.
/* test function -- when plotted, the intermediate values produce the curve of the binomial distribution */ void bidist_test(long double p, long double n, long double x) { long double fn, fx, fnx, t1, t2, t3; long double sum, xx, ival;
sum = 0.0;
for(xx = 0.0; xx <= n; xx++)
{
fn = factorial(n);
fx = factorial(xx);
fnx = factorial(n-xx);
t1 = fn / (fx * fnx);
t2 = (long double)pow(p, xx);
t3 = (long double)pow(1.0-p, n-xx);
ival = t1 * t2 * t3;
sum += ival;
printf("%Lf\n", ival);
}
printf("\n%Lf\n", sum);
return;
}sample output: p=.5, n=12, x=5
0.000244 0.002929 0.016113 0.053710 0.120850 0.193359 0.225586 0.193359 0.120850 0.053710 0.016113 0.002929 0.000244
1.000000
David .
- References:
- please check my homework
- From: David Marsh
- Re: please check my homework
- From: Michael Jørgensen
- please check my homework
- Prev by Date: Re: Java or C++?
- Next by Date: Re: Java or C++?
- Previous by thread: Re: please check my homework
- Next by thread: Re: please check my homework
- Index(es):
Relevant Pages
|