please check my homework
- From: David Marsh <dmarsh@xxxxxxxx>
- Date: Tue, 25 Oct 2005 20:29:12 GMT
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 ---------------------------------------------------------------
#include <stdio.h> #include <stdlib.h> #include <math.h>
long double bidist(long double p, long double n, long double x); long double factorial(long double n);
int main(int argc, char *argv[])
{
long double pp, nn, xx, result;if(argc != 4) exit(EXIT_FAILURE);
pp = strtold(argv[1], NULL);
nn = strtold(argv[2], NULL);
xx = strtold(argv[3], NULL); result = bidist(pp, nn, xx);
printf("%Lf\n", result);return 0; }
/* return the probability of x successes in n events,
given the probability p for success in a single event,
using the formula for the binomial distribution:
n!
f(x) = --------- ( p^x )( (1-p)^(n-x) )
x! (n-x)!
*/
long double bidist(long double p, long double n, long double x)
{
long double fn, fx, fnx, t1, t2, t3; fn = factorial(n);
fx = factorial(x);
fnx = factorial(n-x);
t1 = fn / (fx * fnx);
t2 = (long double)pow(p, x);
t3 = (long double)pow(1.0-p, n-x);return t1 * t2 * t3; }
/* return n! */
long double factorial(long double n)
{
long double f; f = 1.0;
while(n > 1.0)
f *= n--;return f; } .
- Follow-Ups:
- Re: please check my homework
- From: Bill Medland
- Re: please check my homework
- From: Michael Jørgensen
- Re: please check my homework
- Prev by Date: Re: Java or C++?
- Next by Date: Re: Java or C++?
- Previous by thread: Serial port
- Next by thread: Re: please check my homework
- Index(es):