Friday, July 15, 2011

Program to implement Cafeteria problem



#include<iostream.h>
#include<stdlib.h>
#include<math.h>
#include<conio.h>

#define seed 12345
void main()
{
clrscr();
int i,j,k,kk,run,counter;
int n=6,m= 5,s=4;
float se[10];
float r,iat,st,nat,wt,cwt;
float p[]={0.0,0.05,0.4,0.65,0.8,0.9,0.97,1.0};
float at[]={0,0.5,1,1.5,2,2.5,3};
float f[]={0,0.05,0.3,0.65,0.85,1.0};
float t[]={1,2,3,4,5};
cout&lt;&lt;"Enter the value of run:";
cin&gt;&gt;run;
for(j=0;j&lt;=s;++j)
{
srand(seed);
counter=0;
nat=0;
cwt=0;
for(i=1;i&lt;=j;++i)
{
se[i]=0;
}
cwt=0;
counter=0;
for(counter=1;counter&lt;=run;++counter)
{
r=rand()/32768.0;
for(i=0;i&lt;=n;++i)
{
if(r&gt;p[i] &amp;&amp; r&lt;=p[i+1])
iat=at[i];
}
r=rand()/32768.0;
for(i=0;i&lt;=m;++i)
{
if(r&gt;f[i]  &amp;&amp; r&lt;=f[i+1])
st=t[i];
}
nat=nat+iat;
float min=99.9;
for(i=1;i&lt;=j;++i)
{
if(se[i]&lt;=min)
{
min=se[i];
k=i;
}
}
if(nat&lt;=min )
{
se[k]=min+st;
wt=min-nat;
}
else
se[k]=nat+st;
cwt=cwt+wt;
}
cout&lt;&lt;"server\t  Total arrival\t  Average waiting time  "&lt;&lt;"\n";
cout&lt;
if((cwt/run)&lt;=2)
break;
}
getch();
}
/*  OUTPUT
Enter the value of run:30
server    Total arrival   Average waiting time
0               31              80.583336
server    Total arrival   Average waiting time
1               31              30.213335
server    Total arrival   Average waiting time
2               31              8.183333
server    Total arrival   Average waiting time
3               31              1.866667


*/

No comments:

Post a Comment