Professional Documents
Culture Documents
S.No
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
Name Of Program
Generate Uniformly Distributed Random Numbers (Repetitive or
Non-Repetitive) using Quick and Dirty method.
Generate Uniformly Distributed Random Numbers (Repetitive or
Non-Repetitive) using Power Residue method.
Generate Non Uniformly Distributed Random Numbers from an
Exponential Distribution using Inverse Transformation.
Generate Non Uniformly Distributed Random Numbers from a
Normal Distribution using Box-Muller Transformation.
Generate Non Uniformly Distributed Random Numbers from
Poisson Distribution for any specified lambda.
Generate Non Uniformly Distributed Random Numbers from an
Erlang-m Distribution with a mean time (m*beta) units.
Simulation of Inventory System.
Simulation of a Pure-Pursuit Problem.
Find the Square-Root of 3 using Monte-Carlo Simulation.
To simulate chemical reactor
Simulation of CPM method
Simulation of Second Order Servo System using Runge Kutta
Integration formula
Implement Chi Square test
Simulation of A Single-Server Queuing System
Signature
Program #1
AIM: Generate Uniformly Distributed Random Numbers (Repetitive or Non-Repetitive)
using Quick and Dirty method.
OUTPUT
Options:
1. Repetitive
2. Non Repetitive
3. Exit
Enter your choice
62
34
29
48
30
50
44
47
73
69
36
46
61
73
70
46
Options:
1. Repetitive
2. Non Repetitive
3. Exit
Enter your choice
Program #2
AIM: Generate Uniformly Distributed Random Numbers (Repetitive or Non-Repetitive)
using Power Residue method.
OUTPUT:
Options:
1. Repetitive
2. Non Repetitive
3. Exit
Enter your choice
28
48
28
37
68
72
71
72
41
47
62
53
Options:
1. Repetitive
2. Non Repetitive
3. Exit
Enter your choice
Program #3
AIM: Generate Non Uniformly Distributed Random Numbers from an Exponential
Distribution using Inverse Transformation.
getch();
return 0;
}
OUTPUT:
Enter the required number of random numbers: 10
Enter the value of lambda
3.8
Random Numbers from an Exponential Distribution using Inverse Transformation are:
0.036673
0.627371
0.205916
0.013003
0.073690
0.437890
0.618378
0.194246
0.602118
0.173405
Program #4
AIM: Generate Non Uniformly Distributed Random Numbers from a Normal
Distribution using Box-Muller Transformation.
OUTPUT:
Enter the required number of random numbers: 10
Enter the Expected Value(Mu) and Standard Deviation(Sigma):
35 10
Random Numbers from a Normal Distribution using Box-Muller Transformation are:
38.612061
53.256416
22.906258
38.059597
39.358772
51.233006
13.814940
22.875181
23.960373
44.462170
Program #5
AIM: Generate Non Uniformly Distributed Random Numbers from Poisson Distribution
for any specified lambda.
product *= rand();
}
printf ("%d\n", k);
}
getch();
return 0;
}
OUTPUT:
Enter the required number of random numbers: 10
Enter the value of lambda
6
Random Samples from a Poisson Distribution are:
6
7
8
6
8
4
3
7
2
6
Program #6
AIM: Generate Non Uniformly Distributed Random Numbers from an Erlang-m
Distribution with a mean time (m*beta) units.
OUTPUT:
Enter the required number of random numbers: 10
Enter the number of services and average service time: 3
Random Samples from an Erlang-m Distribution are:
23
13
37
16
11
8
7
29
22
11
Program #7
AIM: Simulation of Inventory System.
INPUT PARAMETERS: (Re-Order Point) point, (Re-Order Quantity) quantity
OUTPUT PARAMETERS: (Total Cost Incurred) cost
ROUTINES USED: rand()
SOURCE CODE:
#include <stdio.h>
#include <math.h>
float seed = 0.7614593;
float rand()
{
int temp;
seed = (seed + 3.14159) * (seed + 3.14159);
temp = seed;
seed = seed - temp;
return (seed);
}
int main()
{
int point, quantity;
int stock, equi_stock, units_due, due_date, demand;
float cost;
int i;
/*initialize values*/
cost = 0;
stock = 115;
units_due = 0;
due_date = 0;
printf ("Enter Re-Order Point & Re-Order Quantity\n");
scanf ("%d%d", &point, &quantity);
/*Simulate for specified number of days(180)*/
for(i=1; i<=180; i++)
{
/* If today(i) is due date..add quantity ordered to stock. No more units due..
*/
if (due_date == i)
{
stock += quantity;
units_due = 0;
}
demand = (int) rand() * 100;
/* If demand is more than stock,, add loss of gud-will cost to cost*/
if (demand > stock)
{
cost += ((demand - stock) * 18.0);
stock = 0;
}
/* else add carrying cost to cost */
else
{
cost += (stock * 0.75);
stock -= demand;
}
equi_stock = stock + units_due;
/* If equivalent stock is less than equal to Reorder Point,, Reorder Quantity
*/
if (equi_stock <= point )
{
units_due = quantity;
due_date += 3;
/* add cost of placing reorder to cost */
cost += 75;
}
}
printf (" Total Cost incurred: %f", cost);
getch();
return 0;
}
OUTPUT:
Enter Re-Order Point & Re-Order Quantity
300 350
Total Cost incurred: 142098.000000
Program #8
AIM: Simulation of a Pure-Pursuit Problem.
INPUT PARAMETERS: (Path of bomber) xb, yb ; (Intial Position of Fighter) xf[0],
yf[0]; (Velocity of Fighter) vf
OUTPUT:
Enter the path of Bomber in xy-coordinates..
At time 0:
80
0
At time 1:
90
-2
At time 2:
99
-5
At time 3:
108 -9
At time 4:
116 -15
At time 5:
125 -18
At time 6:
133 -23
At time 7:
141 -29
At time 8:
151 -28
At time 9:
160 -25
At time 10: 169 -21
At time 11: 179 -20
At time 12: 180 -17
Enter the Initial Position of Fighter in xy-coordinates..
0
50
Enter velocity of Fighter plane..
20
Target caught at time: 10 mins and 2.969 mts apart
Program #9
AIM: Find the Square-Root of 3 using Monte-Carlo Simulation.
INPUT PARAMETERS: (Size of random sample) n
OUTPUT PARAMETERS: (Square-Root of 3) sqrt3
ROUTINES USED: rand()
SOURCE CODE:
#include <stdio.h>
#include <math.h>
float seed = 0.714565;
float rand()
{
int temp;
seed = (seed + 3.14159) * (seed + 3.14159);
temp = seed;
seed = seed - temp;
return (seed);
}
int main()
{
int i,n,p=0;
float random,sqrt3;
clrscr();
printf ("Enter the size of random sample you want to generate..\t");
scanf ("%d", &n);
for (i=0; i<n; i++)
{
random = rand() + 1;
/* Count No. of samples satisfying the boundary condition..*/
if ( (random*random) < 3)
p++;
}
sqrt3 = 1 + (float)p/n;
printf ("\n The Square root of 3 is: %5.3f\n", sqrt3);
getch();
return 0;
}
OUTPUT:
Enter the size of random sample you want to generate.. 1250
The Square root of 3 is: 1.727
Program #10
AIM: To simulate chemical reactor
INPUT PARAMETERS: Initial amounts of a and b
OUTPUT PARAMETERS: The amount of a,b and c at different time instants
SOURCE CODE:
//chemical reactor
void main()
{
int i;
float t=0;
float a[50],b[50],c[50],delta;
float k1,k2;
a[0] = 100.0;
b[0]= 50.0;
c[0] = 0.0;
delta = .1;
k1 = .008;
k2 = .002;
printf("TIME
A[I]
B[I]
C[I]\n");
for( i=0;i<50;i++)
{
printf(" %f %f %f
%f\n",t,a[i],b[i],c[i]);
a[i+1] = a[i] +( k2*c[i] - k1*a[i]*b[i])*delta;
b[i+1] = b[i] +( k2*c[i] - k1*a[i]*b[i])*delta;
c[i+1] = c[i] + (2*(k1*a[i]*b[i]) -k2*c[i])*delta;
t = t+ 0.1;
}
getch();
}
OUTPUT
TIME
0.000000
0.100000
0.200000
0.300000
0.400000
0.500000
0.600000
0.700000
0.800000
0.900000
1.000000
1.100000
A[I]
B[I]
100.000000 50.000000
96.000000 46.000000
92.468803 42.468800
89.330185 39.330181
86.523758 36.523750
84.001015 34.001003
81.722519 31.722511
79.655876 29.655872
77.774208 27.774204
76.055016 26.055008
74.479309 24.479303
73.030968 23.030962
C[I]
0.000000
8.000001
15.064001
21.344252
26.961378
32.012260
36.575649
40.716244
44.487724
47.935013
51.096008
54.002911
1.200000
1.300000
1.400000
1.500000
1.600000
1.700000
1.800000
1.900000
2.000000
2.100000
2.200000
2.300000
2.400000
2.500000
2.600000
2.700000
2.799999
2.899999
2.999999
3.099999
3.199999
3.299999
3.399999
3.499999
3.599999
3.699999
3.799999
3.899998
3.999998
4.099998
4.199998
4.299998
4.399998
4.499998
4.599998
4.699998
4.799998
4.899998
71.696190
70.463097
69.321411
68.262192
67.277618
66.360817
65.505730
64.706985
63.959820
63.259979
62.603653
61.987427
61.408222
60.863258
60.350025
59.866241
59.409828
58.978897
58.571716
58.186707
57.822418
57.477516
57.150772
56.841057
56.547321
56.268604
56.004009
55.752705
55.513920
55.286938
55.071091
54.865757
54.670353
54.484337
54.307201
54.138474
53.977707
53.824482
21.696184
20.463093
19.321411
18.262192
17.277615
16.360811
15.505721
14.706978
13.959812
13.259971
12.603644
11.987417
11.408212
10.863250
10.350017
9.866231
9.409818
8.978886
8.571706
8.186696
7.822406
7.477502
7.150757
6.841040
6.547306
6.268588
6.003992
5.752686
5.513900
5.286917
5.071069
4.865734
4.670330
4.484314
4.307180
4.138452
3.977683
3.824458
56.683270
59.160786
61.455982
63.586712
65.568581
67.415298
69.138962
70.750275
72.258759
73.672897
75.000282
76.247734
77.421394
78.526802
79.568970
80.552452
81.481392
82.359550
83.190384
83.977043
84.722420
85.429169
86.099747
86.736404
87.341217
87.916122
88.462898
88.983200
89.478569
89.950432
90.400116
90.828865
91.237839
91.628120
92.000717
92.356575
92.696587
93.021576
Program #11
AIM: Simulation of CPM method
INPUT PARAMETERS: s[8],f[8],t[8]
OUTPUT PARAMETERS: ENT[6],EST[8],EFT[8],tmin,LNT[6],LST[8],LFT[8]
SOURCE CODE:
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int n=7,m=5;
double s[8],f[8];
double t[8],ENT[6],EST[8],EFT[8],tmin;
double LNT[6],LST[8],LFT[8];
int mark[8];
int snode,fnode,sn,fn;
clrscr();
cout<<"****CRITICAL PATH METHOD****\n\n";
cout<<"Read Starting nodes for each activity"<<endl;
for(int i=1;i<=7;i++)
{
cout<<"Starting node for Activity"<<i<<":";
cin>>s[i];
cout<<"\n";
}
cout<<"\n Read Finishing nodes for each activity"<<endl;
for(i=1;i<=7;i++)
{
cout<<"Finishing node for Activity"<<i<<":";
cin>>f[i];
cout<<"\n";
}
cout<<"\n Read Duration for Activity"<<endl;
for(i=1;i<=7;i++)
{
cout<<"Duration for Activity"<<i<<":";
cin>>t[i];
cout<<"\n";
}
for(i=1;i<=5;i++)
{
ENT[i]=0;
}
int k=1;
while(k<=n)
{
snode=s[k];
fnode=f[k];
EST[k]=ENT[snode];
EFT[k]=EST[k]+t[k];
if(EFT[k]>ENT[fnode])
{
ENT[fnode]=EFT[k];
}
k=k+1;
}
tmin=ENT[m];
cout<<"TMIN"<<":"<<tmin<<endl;
cout<<"EST"<<"=(";
for(int j=1;j<=7;j++)
{
cout<<EST[j]<<";";
}
cout<<")";
cout<<endl<<"EFT"<<"=(";
for(j=1;j<=7;j++)
{
cout<<EFT[j]<<";";
}
cout<<")";
cout<<endl<<"ENT"<<"=(";
for(j=1;j<=5;j++)
{
cout<<ENT[j]<<";";
}
cout<<")";
for(int q=1;q<=5;q++)
{
LNT[q]=tmin;
}
int d=7;
while(d>0)
{
sn=s[d];
fn=f[d];
LFT[d]=LNT[fn];
LST[d]=LFT[d]-t[d];
mark[d]=0;
if(LST[d]<LNT[sn])
{
LNT[sn]=LST[d];
}
d=d-1;
}
cout<<"\n LST"<<"=(";
for(j=1;j<=7;j++)
{
cout<<LST[j]<<";";
}
cout<<")";
cout<<endl<<"LFT"<<"=(";
for(j=1;j<=7;j++)
{
cout<<LFT[j]<<";";
}
cout<<")";
cout<<endl<<"LNT"<<"=(";
for(int l=1;l<=5;l++)
{
cout<<LNT[l]<<";";
}
cout<")";
double h;
cout<<"\n Critical path is"<<endl;
for(int e=1;e<=7;e++)
{
h=abs(LST[e]-EST[e]);
if(h==0)
{
mark[e]=1;
cout<<e;
}
}
getch();
return 0;
}
OUTPUT:
****CRITICAL PATH METHOD****
Read Starting nodes for each activity
Starting node for Activity1:1
Starting node for Activity2:1
Starting node for Activity3:2
Starting node for Activity4:2
Starting node for Activity5:3
Starting node for Activity6:3
Starting node for Activity7:4
Read Finishing nodes for each activity
Finishing node for Activity1:2
Finishing node for Activity2:3
Finishing node for Activity3:4
Finishing node for Activity4:3
Finishing node for Activity5:5
Finishing node for Activity6:4
Finishing node for Activity7:5
Read Duration for Activity
Duration for Activity1:5.1
Duration for Activity2:7.2
Duration for Activity3:6.0
Duration for Activity4:4.5
Duration for Activity5:15.8
Duration for Activity6:0
Program #12
AIM: Simulation of Second Order Servo System using Runge Kutta Integration formula
OUTPUT PARAMETERS: t,y1,y2
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int main()
{
float h,t,y1,y2,n,a;
float u11,u12,u21,u22,u31,u32,u41,u42;
h=0.1;
t=0;
y1=1;
y2=0;
n=20;
a=0.1;
clrscr();
cout<<"****SIMULATION OF SERVO SYSTEM****\n";
cout<<"Time(t)"<<"\t\t"<<"Y1"<<"\t\t"<<"Y2"<<endl;
for(int i=1;i<=n;i++)
{
u11=h*y2;
u12=h*(a*(1-(y1*y1))*y2-y1);
u21=h*(y2+(0.5*u12));
u22=h*(a*(1-(y1+(0.5*u11))*(y1+(0.5*u11)))*(y2+(0.5)*u12)-(y1+(0.5*u11)));
u31=h*(y2+(0.5*u22));
u32=h*(a*(1-(y1+(0.5*u21))*(y1+(0.5*u21)))*(y2+(0.5)*u22)-(y1+(0.5*u21)));
u41=h*(y2+u32);
u42=h*(a*(1-(y1+u31)*(y1+u31))*(y2+u32)-(y1+u31));
y1=y1+((u11+(2*u21)+(2*u31)+u41)/6);
y2=y2+((u12+(2*u22)+(2*u32)+u42)/6);
t=t+h;
printf("%.2f\t\t%.2f\t\t%.2f\n",t,y1,y2);
}
getch();
return 0;
}
OUTPUT:
****SIMULATION OF SERVO SYSTEM****
Time(t)
Y1
Y2
0.00
1.00
0.00
0.10
1.00
-0.10
0.20
0.98
-0.20
0.30
0.96
-0.30
0.40
0.92
-0.39
0.50
0.88
-0.48
0.60
0.82
-0.57
0.70
0.76
-0.65
0.80
0.70
-0.73
0.90
0.62
-0.80
1.00
0.54
-0.86
1.10
0.45
-0.91
1.20
0.35
-0.96
1.30
0.26
-1.00
1.40
0.15
-1.03
1.50
0.05
-1.05
1.60
-0.06
-1.06
1.70
-0.16
-1.06
1.80
-0.27
-1.05
1.90
-0.37
-1.03
Program #13
AIM: Implement Chi Square test
INPUT PARAMETERS: cl,ex[10],ob[10]
OUTPUT PARAMETERS: a[11][2]
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a[11][2]={{99.5,1.73},
{99,2.09},
{95,3.33},
{90,4.17},
{75,5.90},
{50,8.34},
{25,11.4},
{10,14.7},
{5,16.9},
{1,21.7},
{.5,23.6}};
float ob[10]={0},ex[10]={0},chi_sq,temp=0.000000,pj;
int cl,i;
printf("\nenter how many classes are there ");
scanf("%d",&cl);
for(i=0;i<cl;i++)
{
printf("\nenter expected and observed value of class %d ",i+1);
scanf("%f%f",&ex[i],&ob[i]);
}
for(i=0;i<cl;i++)
{ pj=ob[i]-ex[i];
pj=pj*pj;
temp+=pj/ex[i];
}
for(i=0;i<11;i++)
{
if(temp<a[i][1])
{
break;
}
}
printf("\n%f level of confidence that random no's are uniformly distributed ",a[i]
[0]);
getch();
}
OUTPUT:
enter how many classes are there 10
enter expected and observed value of class 1 500
468
enter expected and observed value of class 2 500
519
enter expected and observed value of class 3 500
480
enter expected and observed value of class 4 500
495
enter expected and observed value of class 5 500
508
enter expected and observed value of class 6 500
426
enter expected and observed value of class 7 500
497
enter expected and observed value of class 8 500
515
enter expected and observed value of class 9 500
463
enter expected and observed value of class 10 500
529
99.5 level of confidence that random no's are uniformly distributed
Program #13
AIM: Simulation of A Single-Server Queuing System
OUTPUT PARAMETERS: I,cat[20],cdt[20],ql[20],wt[20],i,id[20]
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
int at[8]={0,10,15,35,30,10,5,5};
int st[8]={20,15,10,5,15,15,10,10};
int i,cdt[20],cat[20],wt[20],id[20],ql[20],dif;
cat[0]=0;
cdt[0]=20;
ql[0]=0;
id[0]=0;
for(i=1;i<=7;i++)
cat[i]=cat[i-1]+at[i];
for(i=1;i<=8;i++)
{
dif=cdt[i-1]-cat[i];
if(dif>0)
{
ql[i]=ql[i-1]+1;
id[i]=0;
wt[i]=dif;
}
else
{
ql[i]=ql[i-1]-1;
id[i]=-dif;
wt[i]=0;
}
cdt[i]=cat[i]+wt[i]+st[i];
}
for(i=0;i<=7;i++)
printf("\ni=%d\tcat=%d\tcdt=%d\tid=%d\tql=%d\twt=
%d",i,cat[i],cdt[i],id[i],ql[i],wt[i]);
getch();
}
OUTPUT:
***** 1 SERVER *****
JOB 1
AT=0 CAT=0 ST=20 CDT=20 IDT=0 QL=0
JOB 2
AT=10 CAT=10 ST=15 CDT=35 IDT=0 QL=1
JOB 3
AT=15 CAT=25 ST=10 CDT=45 IDT=0 QL=1
JOB 4
AT=35 CAT=60 ST=5 CDT=65 IDT=15 QL=0
JOB 5
AT=30 CAT=90 ST=15 CDT=105 IDT=25 QL=0
JOB 6
AT=10 CAT=100 ST=15 CDT=120 IDT=0 QL=1
JOB 7
AT=5 CAT=105 ST=10 CDT=130 IDT=0 QL=1
JOB 8
AT=5 CAT=110 ST=10 CDT=140 IDT=0 QL=2