Simulation Programs Record
Simulation Programs Record
clrscr();
permutation p;
p.get();
p.disp();
getch();
}
4. DISCRETE UNIFORM
refer to / see sriniza mail
5. Bernouli distribution
refer to / see sriniza mail
6. poission distribution
refer to / see sriniza mail
7. geometric distribution
program code:
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
class geom
{
public:
double x,p,a,b,pdf,expt,var;
void get();
void cal();
void disp();
};
void geom::get()
{
cout<<"enter the range x : ";
cin>>x;
cout<<"\n Enter P value : ";
cin>>p;
}
void geom::cal()
{
if(p>=0 && p<=1)
{
a=x-1;
b=1-p;
pdf=p*pow(b,a);
expt=1/p;
var=(1-p)/(p*p);
}
}
void geom::disp()
{
cout<<"\n pdf =\t\t"<<pdf;
cout<<"\n exptation =\t"<<expt;
cout<<"\n variance =\t"<<var;
}
void main()
{
clrscr();
geom ge;
ge.get();
ge.cal();
ge.disp();
getch();
}
pdf=1/(b-a);
expt=(a+b)/2;
var=pow((b-a),2)/12;
}
}
void cudist::disp()
{
cout<<"\n pdf = \t "<<pdf;
cout<<"\n expectations = "<<expt;
cout<<"\n variance = "<<var;
}
void main()
{
clrscr();
cudist cud;
cud.get();
cud.cal();
cud.disp();
getch();
}
9. normal distribution
program code:
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
class cndist
{
double mu,sg,x,pdf,expt,var,a,b,c,s;
public:
void get();
void cal();
void disp();
};
void cndist::get()
{
cout<<"enter the values of mu, sg, x";
cin>>mu>>sg>>x;
}
void cndist::cal()
{
if(pow(sg,2)>0)
{
s=sqrt(2*x);
a=1/(sg*s);
b=pow(x-mu,2)/(2*sg*sg);
c=exp(-b);
pdf=a*c;
expt=mu;
var=sg*sg;
}
}
void cndist::disp()
{
cout<<"\n pdf = \t "<<pdf;
cout<<"\n expectations = "<<expt;
cout<<"\n variance = "<<var;
}
void main()
{
clrscr();
cndist cnd;
cnd.get();
cnd.cal();
cnd.disp();
getch();
}
for(y=0;y<=1;y++)
{
cout<<"\n "<<x<<" \t"<<y<<" \t"<<x*y;
}
}
}
void ttble::org()
{
cout<<"\n\n Truth Table of OR Gate";
cout<<"\n x \t y \t x+y" ;
cout<<"\n ====================";
for(x=0;x<=1;x++)
{
for(y=0;y<=1;y++)
{
if(x==1 && y==1)
{
z=1;
cout<<"\n "<<x<<" \t"<<y<<" \t"<<z;
}
else
cout<<"\n "<<x<<" \t"<<y<<" \t"<<x+y;
}
}
}
void ttble::notg()
{
cout<<"\n\n Truth Table of NOT Gate";
cout<<"\n x \t z" ;
cout<<"\n ===========";
for(x=0;x<=1;x++)
{
if(x==1)
{
z=0;
cout<<"\n "<<x<<" \t"<<z;
}
else
{
z=1;
cout<<"\n "<<x<<" \t"<<z;
}
}
}
void main()
{
clrscr();
ttble tt;
tt.andg();
tt.org();
tt.notg();
getch();
}
12. t-distribution
refer to / see sriniza mail
for(i=0;i<n;i++)
{
tot=tot+obfq[i];
}
exp=tot/n;
df=n-1;
for(i=0;i<=n;i++)
{
b[i]=obfq[i]-exp;
}
for(i=0;i<n;i++)
{
s=b[i];
c[i]=pow(s,2);
}
for(i=0;i<n;i++)
{
p=c[i];
d[i]=p/exp;
}
a=0;
for(i=0;i<n;i++)
{
a=a+d[i];
}
return a;
}
void main()
{
clrscr();
chisq chi;
chi.get();
chi.cal();
chi.disp();
getch();
}
int i,n;
float a[20],mea,med,mode;
public:
float mean(float[],int);
float median(float[],int);
void get();
void disp();
void cal();
};
void mmm::get()
{
cout<<"Enter No of Elements\n";
cin>>n;
cout<<"Enter Elements\n";
for(i=0;i<n;i++)
cin>>a[i];
}
float mmm::mean(float a[],int n)
{
int i;
float sum=0;
for(i=0;i<n;i++)
sum=sum+a[i];
return (sum/n);
}
float mmm::median(float a[],int n)
{
float temp;
int i,j;
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
if(n%2==0)
return (a[n/2]+a[n/2-1])/2;
else
return a[n/2];
}
void mmm::cal()
{
mea=mean(a,n);
med=median(a,n);
mode=3*med-2*mea;
}
void mmm::disp()
{
cout<<"\n\tMean = "<<mea;
cout<<"\n\tMedian = "<<med;
cout<<"\n\tMode = "<<mode;
}
void main()
{
clrscr();
mmm m;
m.get();
m.cal();
m.disp();
getch();
}
clrscr();
ndiv n;
n.get();
n.cal();
getch();
}
{
cout<<" \n quotient is : "<<c[i];
}
}
void main()
{
clrscr();
div d;
d.get();
d.cal();
d.disp();
getch();
}
{
cout<<"\n"<<r[j]<<"\t\t\t"<<m[j];
}
}
void main()
{
clrscr();
roll k;
k.get();
k. disp();
getch();
}
#include<conio.h>
#include<iostream.h>
class mmm
{
int i,n;
float a[20],mea,med,mode;
public:
float mean(float[],int);
float median(float[],int);
void get();
void disp();
void cal();
};
void mmm::get()
{
cout<<"Enter No of Elements\n";
cin>>n;
cout<<"Enter Elements\n";
for(i=0;i<n;i++)
cin>>a[i];
}
float mmm::mean(float a[],int n)
{
int i;
float sum=0;
for(i=0;i<n;i++)
sum=sum+a[i];
return (sum/n);
}
float mmm::median(float a[],int n)
{
float temp;
int i,j;
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
if(n%2==0)
return (a[n/2]+a[n/2-1])/2;
else
return a[n/2];
}
void mmm::cal()
{
mea=mean(a,n);
med=median(a,n);
mode=3*med-2*mea;
}
void mmm::disp()
{
cout<<"\n\tMean = "<<mea;
cout<<"\n\tMedian = "<<med;
cout<<"\n\tMode = "<<mode;
}
void main()
{
clrscr();
mmm m;
m.get();
m.cal();
m.disp();
getch();
}
21. SUBSET
refer to / see sriniza mail
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int i,n;
float x,y,sx=0,sy=0,sxy=0,sxx=0,syy=0,mx,my,sdx,sdy,cxy,r,vx,vy;
cout<<"\n Enter the no. of pairs : ";
cin>>n;
for(i=1;i<=n;++i)
{
cout<<"\n Enter X"<<i<<" : ";
cin>>x;
sx+=x;
sxx+=(x*x);
cout<<"\n Enter Y "<<i<<":";
cin>>y;
sy+=y;
syy+=(y*y);
sxy+=(x*y);
}
mx=sx/n;
my=sy/n;
vx=(sxx/n)-(mx*mx);
vy=(syy/n)-(my*my);
sdx=sqrt(vx);
sdy=sqrt(vy);
cxy=(sxy/n)-(mx*my);
r=cxy/(sdx*sdy);
cout<<"\n Pearsons correlation coefficient : "<<r;
getch();
}
x1 value : ";
x2 value : ";
y1 value : ";
y2 value : ";
cin>>y2;
m=(y2-y1)/(x2-x1);
cout<<"\n the slope is: "<<m<<"\n";
{
c=y2-(m*x2);
cout<<"\n the intercept c : "<<c<<"\n";
cout<<"\n the line equation is: y="<<m<<"*"<<x2<<"+"<<c;
cout<<"\n\t\t\t y = "<<m*x2+c;
getch();
return 0;
}
}