#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#define MAX(X, Y) (((X) > (Y)) ? (X) : (Y))
using namespace std;
double Packetlenght();//to generate exponential packet lenghts
double interarrival(int);//to calculate interarrival time between the packets
double interarrival1(int);//tocalculate interarrival time between observers
void arrival();//to calculate total number of arrivals in the system and number of arrivals prior to current arrival
void departure();//to calculate departures of the packet in the system
void observer();//calculate average number of packets and system idle percentage as seen by the observer
double na=0;//store the value of number of arrivals prior to the current arrival
double n=0;//store total number of packets in the system
double dep=0;//store total number of departures from the system
double p=0;//for calculating percentage of system remains idle
double ooo=0;//store total number of packets in the system seen by observer at the observer's arrival
double arr=0;//store total number of packets that have arrived in the system
double o=0;// store total number of observers arrived in the system
double show;//store the time of last packet arrival in the last iteration
double show1;//store the time of last observer arrival to the system in the last iteration
float averagepckt=0.0;//average packets arrived in the system
float packetarr=0.0;//average packets already in the system seen by current packet arrival
float averageidle=0.000;//store average idle time of the system
float averagetime=0.0;//average time packet spend in the system
int l=35;//average arrival rate of packet
int no;//average observer arrival rate
main(void)
{
const int size=5000,size1=15000;
struct variables
{
double value;//store packet or observer arrival and departure values
char type;//to distinguish whether value is for packet arrival or departure
}ar[size], dp[size], oo[size], m[size1], swap[size1];
int i,j,k,x=1;//couters for loops
no=l;
float value1=1/(float)l;//store exponential interarrival time of the packet
double c=2000000;//system capacity
double value;//store exponentialinterarrival time of the observer
double a;//used as terminating condition upto 33 seconds(packet arrivals)
double bb;//used as terminating condition upto 33 seconds(observer arrival)
double z;//calculate maximum of the last departure or current arrival value
double y;//store packet length
double w;//store service time for every packet
show=0.0;
show1=0.0;
srand(0);
while(x<=303)
{
value=0.0;
a=0;
bb=0;
dp[0].value=0.0;//initializing departure time
ar[0].value=show;//initializing arrival time
oo[0].value=show1;//intializing observer time
for(i=1;a<33.00;i++)//calculating packet arrival and departure times
{
ar[i].value=value1+ar[i-1].value;
ar[i].type='p';
a=a+value1;
y=Packetlenght();
w=y/c;
z=MAX(dp[i-1].value , ar[i].value);
dp[i].value=z+w;
dp[i].type='d';
}
show=ar[i-1].value;//operation to store time of last packet arrival per interation
for(j=1; bb<33.00; j++)//calculating observer arrival time
{
value=interarrival1(no);
oo[j].value=value + oo[j-1].value;
oo[j].type='o';
bb=bb+value;
}
show1=oo[j-1].value;//operation to store time of last observer arrival per iteration
int ii=1, jj=1, cc=0;//counters used in for loops for merging
int aa=(2*i)+j;//total number of values generated for arrivals, departures and observers
for(k=1; k<(aa-2); k++)//merging packet arrival array, packet departure array and observer array
{
if(k<i)
{
m[k].type = ar[k].type;
m[k].value = ar[k].value;
}
else if (k>=i && k<(aa-1-j))
{
m[k].type = dp[ii].type;
m[k].value = dp[ii].value;
ii++;
}
else
{
m[k].type = oo[jj].type;
m[k].value = oo[jj].value;
jj++;
}
cc++;
}
for (ii=0;ii<(cc-1);ii++)//bubble sort on the merged array
{
for (jj=0;jj<(cc-ii-1);jj++)
{
if (m[jj].value>m[jj+1].value)
{
swap[jj].type=m[jj].type;
swap[jj].value=m[jj].value;
m[jj].type=m[jj+1].type;
m[jj].value=m[jj+1].value;
m[jj+1].type=swap[jj].type;
m[jj+1].value=swap[jj].value;
}
}
}
for (ii = 1; ii<=cc; ii++)//event scheduler
{
if (m[ii].type=='p')
{
arrival();
}
else if(m[ii].type=='d')
{
departure();
}
else if(m[ii].type=='o')
{
observer();
}
}
float packetaverage=ooo/o;//store average number of packets in the system observed by the observer per iteration
float pcktarrival=na/arr;//average number of packets seen by current packet on arrival to the system
float systemidle=(p/o)*100;//average system idle time as seen by the observer per iteration
float timespend=packetaverage/l;//average time spent by the packet in the system per iteration
averagepckt=averagepckt+packetaverage;
packetarr=packetarr+pcktarrival;
averageidle=averageidle+systemidle;
averagetime=averagetime+timespend;
cout<<x<<endl;
x++;
cout<<endl;
}
cout<<"Average Number Of Packets in the System:- "<<averagepckt/x<<endl;
cout<<"Packets Already in the system before Next Arrival:- "<<packetarr/x<<endl;
cout<<"System Idle:- "<<averageidle/x<<"%"<<endl;
cout<<"Average time Spend by Packet in the system:- "<<averagetime/x<<endl;
cout<<endl;
system("Pause");
}
double Packetlenght()
{
double c,r;
c=(double)rand()/(double)(RAND_MAX);
if(c!=0)
{
double r=-20000*log(c);
return r;
}
else
{
Packetlenght();
}
}
double interarrival1(int no)
{
double a,r;
a=(double)rand()/(double)(RAND_MAX);
if(a!=0)
{
r=-log(a)/no;
return r;
}
else
{
interarrival1(no);
}
}
void arrival()
{
na=na+n;
n++;
arr++;
}
void departure()
{
if(n>0)
{
n--;
}
dep++;
}
void observer()
{
ooo=ooo+n;
if((arr-dep)==0)
{
p++;
}
o++;
}