Sunday, October 23, 2011

Continuous distribution(weibull,uniform,triangle) implementation

#include<stdio.h>
#include<conio.h>
#include<math.h>
void triangular();
void weibull();
void lognormal();
void uniform();
void main()
{
 int c;
 clrscr();
 printf("choose distribution system:");
 printf("\n1:Triangular distribution");
 printf("\n2:Weibull distribution ");
 printf("\n3:Lognormal distribution");
 printf("\n4:Uniform distribution");
 printf("enter ur choice:");
 scanf("%d",&c);
 clrscr();
 switch(c)
 {
 case 1:
 triangular();
 break;
 case 2:
 weibull();
 break;
 case 3:
 normal();
 break;
 case 4:
 uniform();
 break;
 default:
 printf("invalid selection");
 exit(0);
 }
 getch();
}
void triangular()
{
 double x,a,b,c;
 printf("enter d value of x, a, b & c:  ");
scanf(" %lf %lf %lf %lf",&x, &a, &b, &c);

printf(" f(%.2f) = %f",x,f(x,a,b,c));

printf(" F(%.2f) = %f",x,F(x,a,b,c));
double f(double x, double a, double b, double c)
{


if(a <= x && x <= b)
{

return (2*(x-a))/((b-a)*(c-a));
}
else if( b < x && x <= c)
{
return 2*(c-x)/((c-b)*(c-a));
}
else
return 0;

}

double F(double x, double a, double b, double c)
{
if(x<=a)
return 0;

else if( a{

return ( (x-a)*(x-a)/((b-a)*(c-a)) );

}
else if(b < x && x <= c )
{

return (1 - (c-x)*(c-x)/((c-b)*(c-a)));

}
else if(x>c)
return 1;
}
}

void weibull()
{
int x;
double v,a,b;
printf("enter d value of x, location, shape & scale:  ");
scanf(" %d %lf %lf %lf",&x, &v, &b, &a);

printf(" f(%d) = %f",x,f(x,v,a,b));

printf(" F(%d) = %f",x,F(x,v,a,b));
double f(int x, double v, double a, double b)
{

if(x< v)
return 0;

else
{
return b/a*(pow((x-v)/a,b-1))*exp(-pow((x-v)/a,b));

}

}

double F(int x, double v, double a, double b)
{
if(xreturn 0;

else
{

return (1 - exp(-pow((x-v)/a,b)));

}

}
void lognormal()
{
int sigma,x,u;
printf("enter sigma,x,u");
scanf("%d%d%d",&sigma,&x,&u);
if(x>0)
printf("pdf=%.3lf",(1/(sqrt(2*PI)*sigma*x))*exp(-pow((log(x)-u),2)/(2*sigma*sigma)));
}
void uniform()
{
int a,x,b;
printf("enter the value for a,x,b:");
scanf("%d%d%d",&a,&x,&b);
printf(" f(%d) = %f",x,f(x,a,b));

printf(" F(%d) = %f",x,F(x,a,b));
double f(int x, double v, double a, double b)
{
if(a<=x && x<=b)
return (1/(b-a));
else
return 0;
}
double F(int x, double v, double a, double b)
{
if(xreturn 0;
else if(a<=x && xreturn((x-a)/(b-a));
else if(x>=b)
return 1;
}
}

Thursday, October 20, 2011

Bernouli distribution implementation


void bernouli()
{
double p;
printf("bernouli distribution implementation:\n");
printf("enter success probability");
scanf("%lf",&p);
printf("\nmean =%.3lf",p);
printf("\tvariance =%.3lf",p*(1-p));

}

Poisson distribution implementation


void poisson()
{
int x;
double a;
printf("poisson distribution implementation:\n");
printf("enter x & a:");
scanf("%d%lf",&x,&a);
if(x>0)
printf("pmf=%.3lf",(exp(-a)*pow(a,x))/fact(x));
}

Binomial distribution implementation


void binomial()
{
int n,x;
double p;
printf("binomial distribution implementation:\n");
printf("enter n,x,p");
scanf("%d%d%lf",&n,&x,&p);
if(x>0)
printf("\npmf=%.3lf",(fact(n)/(fact(x)*fact(n-x)))*pow(p,x)*pow((1-p),(n-x)));
printf("\nmean=%.3lf \t variance=%.3lf",n*p,n*p*(1-p));
}
void neg_binomial()
{
int y,k;
double p;
printf("negative binomial distribution:\n");
printf("enter y,k and p for kth success (y>k)");
scanf("%d%d%lf",&y,&k,&p);
if(y>k)
printf("pmf=%.3lf",(fact(y-1)/(fact(k-1)*fact(y-k)))*pow((1-p),(y-k))*pow(p,k));

}

Monday, October 17, 2011

C Graphics : Line clipping of right boundary only using Liang Barsky algo

#include<graphics.h>
int main()
{
int x1,y1,x2,y2,x,y,dx,dy;
int xmin,xmax,ymin,ymax;
float u,p,q;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc");
xmin=ymin=100;
xmax=ymax=300;
x1=150;y1=150; x2=350; y2=200;
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
u=(float)(xmax-x1)/(x2-x1);
dx=x2-x1;
dy=y2-y1;
x=x1+(int)(dx*u);
y=y1+(int)(dy*u);
getch();
cleardevice();
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x,y);
getch();
return 0;
}