Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
44 views

Simulation Programs Record

The document contains program code for generating random numbers, calculating NCR and NPR, implementing discrete and continuous probability distributions like binomial, Poisson, geometric, uniform, and normal. It also includes programs for calculating mean, median, mode of data sets, finding correlation coefficient, generating linear equations, and pseudorandom number generation. Various logical and arithmetic operations are demonstrated through truth tables and dynamic division algorithms.

Uploaded by

mahipal287
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Simulation Programs Record

The document contains program code for generating random numbers, calculating NCR and NPR, implementing discrete and continuous probability distributions like binomial, Poisson, geometric, uniform, and normal. It also includes programs for calculating mean, median, mode of data sets, finding correlation coefficient, generating linear equations, and pseudorandom number generation. Various logical and arithmetic operations are demonstrated through truth tables and dynamic division algorithms.

Uploaded by

mahipal287
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 17

1.

RANDOM NUMBER GENERATION


Programe Code :
refer to / see sriniza mail

2. & 3. NCR and NPR


Program Code:
include<iostream.h>
#include<conio.h>
#include<math.h>
class permutation
{
long n,r,c,p;
public:
long ncr(int,int);
long npr(int,int);
void get();
void disp();
long fact(int);
};
void permutation::get()
{
cin>>n>>r;
}
void permutation::disp()
{
c=ncr(n,r);
p=npr(n,r);
cout<<"ncr= "<<c<<"\n"<<"npr= "<<p;
}
long permutation::ncr(int n,int r)
{
long fout=fact(n)/(fact(r)*fact(n-r));
return fout;
}
long permutation::npr(int n,int r)
{
long fout=fact(n)/fact(n-r);
return fout;
}
long permutation::fact(int x)
{
long f=1;
for(int i=1;i<=x;i++)
f=f*i;
return(f);
}
void main()
{

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();
}

8. Continuous uniform Distribution


program code:
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
class cudist
{
double pdf,expt,var,a,b;
public:
void get();
void cal();
void disp();
};
void cudist::get()
{
cout<<"enter the range of x [a <= x <= b] ie a, b values";
cin>>a>>b;
}
void cudist::cal()
{
if(a<b)
{

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();
}

10. negative exponent


program code:
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
class negexp
{
public:
double x,lam,pdf,expt,var,a;
void get();
void cal();
void disp();
};
void negexp::get()
{
cout<<"enter the range of x: ";
cin>>x;
cout<<" enter lamda value: ";
cin>>lam;
}
void negexp::cal()
{

if(lam>0 && x>=0)


{
a=lam*x;
pdf=lam*exp(-a);
expt=1/lam;
var=1/(lam*lam);
}
}
void negexp::disp()
{
cout<<"\n pdf =\t\t"<<pdf;
cout<<"\n exptation =\t"<<expt;
cout<<"\n variance =\t"<<var;
}
void main()
{
clrscr();
negexp ne;
ne.get();
ne.cal();
ne.disp();
getch();
}

11. truth table


program code:
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
class ttble
{
public:
int x,y,z;
void andg();
void org();
void notg();
};
void ttble::andg()
{
cout<<"\n Truth Table of AND Gate";
cout<<"\n x \t y \t xy";
cout<<"\n ===================";
for(x=0;x<=1;x++)
{

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

13. chi square


program code:
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
class chisq
{
public:
float obfq[20],b[20],c[20],d[20],tot,exp,a,n,s,p,df;
int i;
void get();
double cal();
void disp();
};
void chisq::get()
{
cout<<"enter the total no of frequencies";
cin>>n;
cout<<"enter the observed frequencies";
for(i=0;i<n;i++)
{
cin>>obfq[i];
}
}
void chisq::disp()
{
cout<<"\n observed frequency \t\t expt";
for(i=0;i<n;i++)
{
cout<<"\n"<<obfq[i]<<"\t\t\t\t"<<exp;
}
}
double chisq::cal()
{
tot=0;

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();
}

14. average expected chi-square


Programe Code:
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<stdlib.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();
}

15. normal division


Programe Code:
#include<iostream.h>
#include<conio.h>
#include<math.h>
class ndiv
{
int a,b,c;
public:
void get();
void cal();
};
void ndiv::get()
{
cout<<"\n enter dividend : ";
cin>>a;
cout<<"\n enter divisor : ";
cin>>b;
}
void ndiv::cal()
{
c=a/b;
cout<<"quotient is : "<<c;
}
void main()
{

clrscr();
ndiv n;
n.get();
n.cal();
getch();
}

16. Dynamic Division


program code:
#include<iostream.h>
#include<conio.h>
#include<math.h>
class div
{
int i,n,a[10],b[10],c[10];
public:
void get();
void cal();
void disp();
};
void div::get()
{
cout<<"\n enter no.of values for array : ";
cin>>n;
cout<<"\n enter dividend : ";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"\n enter divisor : ";
for(i=0;i<n;i++)
{
cin>>b[i];
}
}
void div::cal()
{
for(i=0;i<n;i++)
{
c[i]=a[i]/b[i];
}
}
void div::disp()
{
for(i=0;i<n;i++)

{
cout<<" \n quotient is : "<<c[i];
}
}
void main()
{
clrscr();
div d;
d.get();
d.cal();
d.disp();
getch();
}

17. additional modulus


refer to / see sriniza mail

18. multiplication modulus


refer to / see sriniza mail

19. students marks


#include<iostream.h>
#include<conio.h>
#include<math.h>
class roll
{
int r[47],m[100],n;
public:
void get();
void disp();
};
void roll::get()
{
r[0]=0;
cout<<" enter max students: ";
cin>>n;
for(int j=1;j<=n;j++)
{
r[j]=r[j-1]+1;
cout<<"\n enter"<<j<<" student marks: ";
cin>>m[j];
}
}
void roll::disp()
{
cout<<"\n batch no 12 marks";
for(int j=1;j<=n;j++)

{
cout<<"\n"<<r[j]<<"\t\t\t"<<m[j];
}
}
void main()
{
clrscr();
roll k;
k.get();
k. disp();
getch();
}

20. Student Marks - ( MEAN, MEDIAN, MODE)


Programe Code:

#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

22. CORRELATION COEFFICIENT


program code:

#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();
}

23. Linear Equation to find slope y=mx+c


program code:
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
float y1, y2, m, x1, x2, c;
cout<<"enter
cin>>x1;
cout<<"enter
cin>>x2;
cout<<"enter
cin>>y1;
cout<<"enter

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;
}
}

24. pseudorandom number generation


Program Code :
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
int main ()
{
printf ("First number: %d\n", rand()%100);
srand (time(NULL));
printf ("Random number: %d\n", rand()%100);
srand (1);
printf ("Again the first number: %d\n", rand()%100);
getch();
return 0;
}

You might also like