Thursday, 21 April 2016

Poisson Distribution

#include<stdio.h>
#include<math.h>
#include<dos.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
int a,x,i,j,k,p1[20],f1[20],gd=DETECT,gm;
float p[20],f[20];
char str[20];
int fact(int i)
{ if(i==0)
return(1);
else
return(i*fact(i-1));
}
void display(char str[],float t[],int f1[],int ch)
{ int i,j,k;
float temp[20];
char str1[20];
outtextxy(0,0,str);
for(i=0;i<=13;i++)
{   line(40*i,10,40*(i+1),10);
line(40*i,50,40*(i+1),50);
line(40*i,10,40*i,50);
}
line(40*i,10,40*i,50);
line(0,30,40*14,30);
line(38,10,38,50);
outtextxy(5,20,"x");
if(ch==0)
outtextxy(5,40,"f(x)");
else
outtextxy(5,40,"F(x)");
for(i=0,j=0;i<=12;i++,j++)
{   temp[j]=t[j];
sprintf(str,"%2d",j);
sprintf(str1,"%.2f",t[j]);
outtextxy(40*i+45,20,str);
outtextxy(40*i+45,40,str1);
delay(150);
}
for(i=0,j=0;i<225;i++,j+=2)
{   putpixel(100,getmaxy()-50-i,getmaxcolor());
putpixel(100+j,getmaxy()-50,getmaxcolor());
putpixel(100+j+1,getmaxy()-50,getmaxcolor());
delay(10);
}
for(i=0;i<10;i++)
{   putpixel(550-i,getmaxy()-50-i,getmaxcolor());
putpixel(550-i,getmaxy()-50+i,getmaxcolor());
putpixel(100-i,getmaxy()-275+i,getmaxcolor());
putpixel(100+i,getmaxy()-275+i,getmaxcolor());
delay(25);
}
settextstyle(0,VERT_DIR,1);
if(ch==0)
outtextxy(50,300,"p(x)->");
else
outtextxy(50,300,"F(x)->");
settextstyle(0,HORIZ_DIR,1);
outtextxy(300,getmaxy()-30,"x->");
for(i=0,j=0;i<12;i++,j++)
{ if(j<=10)
{   sprintf(str,"%4.2f",(float)j/10);
putpixel(99,getmaxy()-50-j*20,getmaxcolor());
putpixel(98,getmaxy()-50-j*20,getmaxcolor());
outtextxy(60,getmaxy()-50-j*20,str);
}
//line(100+(i*30),getmaxy()-50-f1[i]*100,100+(i*30),getmaxy()-50-f1[i]*100);
sprintf(str,"%2d",i);
putpixel(100+(i*30),getmaxy()-49,getmaxcolor());
putpixel(100+(i*30),getmaxy()-48,getmaxcolor());
outtextxy(90+(i*30),getmaxy()-40,str);
delay(150);
}
putpixel(100+(i*30),getmaxy()-49,getmaxcolor());
putpixel(100+(i*30),getmaxy()-48,getmaxcolor());
outtextxy(90+(i*30),getmaxy()-40,"12");
}
void main()
{
clrscr();

printf("Enter values of x:");
scanf("%d",&x);
printf("Enter value of alpha:");
scanf("%d",&a);
printf("\nMean = Variance = %d",a);
getch();
for(i=0;i<=12;i++)
{
if(i<=x)
{
p[i]=pow(M_E,-a)*pow(a,i)/fact(i);
}
else
p[i]=0;
p1[i]=p[i];
}
f1[0]=f[0]=p[0];
for(i=1;i<=12;i++)
{ f[i]=f[i-1]+p[i];
f1[i]=f[i];
}
initgraph(&gd,&gm,"c:\\tc\\bgi");
sprintf(str,"The Probability density function is as follows:");
display(str,p,p1,0);
for(i=0;i<=12;i++)
{ setcolor(getmaxcolor());
line(100+(i*30),getmaxy()-50,100+(i*30),getmaxy()-50-(p[i]*200));
}
getch();
cleardevice();
sprintf(str,"The Cummulative density function is as follows:");
f1[0]=f[0]=p[0];
for(i=1;i<=12;i++)
{
f[i]=f[i-1]+p[i];
f1[i]=(f[i]*100);
}
display(str,f,f1,1);
for(i=0;i<12;i++)
{ setcolor(getmaxcolor());
line(100+(i*30),getmaxy()-50-(f[i]*200),100+((i+1)*30),getmaxy()-50-(f[i]*200));
line(130+(i*30),getmaxy()-50-(f[i]*200),130+((i)*30),getmaxy()-50-(f[i+1]*200));
}
getch();
cleardevice();
closegraph();
}

No comments:

Post a Comment