#include <stdlib.h>
#include <stdio.h>
#include <time.h>
//CISB352 Assignment 1 Part 1
//Leong Wa Chio D-B2-2709-0
main()
{
int i, j, k, rand1, rand2, total, Total_Ser_Time = 0, Total_Free_Time = 0, Line = 0, Max_Line = 0, ans1;
int Int_Time[1000];
int Ser_Time[1000];
int Stay_Time[1000];
int Free_Time[1000];
int Arr_Time[1000];
int Ser_Star[1000];
int Wait_Time[1000];
int Ser_End[1000];
float ans2, ans3, ans4;
//Star simulation
srand((unsigned)(time(NULL)));
for (i = 0; i < 1000; i++){
rand1 = (rand() % (100-1+1))+ 1;
rand2 = (rand() % (100-1+1))+ 1;
if ( i == 0){
Int_Time[i] = 0;
}else{
if (rand1 >= 1 && rand1 <= 25){
Int_Time[i] = 1;
}else if(rand1 >= 26 && rand1 <=65 ){
Int_Time[i] = 2;
}else if(rand1 >= 66 && rand1 <=85 ){
Int_Time[i] = 3;
}else{
Int_Time[i] = 4;
}
}
if (rand2 >= 1 && rand2 <= 30){
Ser_Time[i] = 3;
}else if(rand2 >= 31 && rand2 <=58 ){
Ser_Time[i] = 4;
}else if(rand2 >= 59 && rand2 <=83 ){
Ser_Time[i] = 5;
}else{
Ser_Time[i] = 6;
}
Total_Ser_Time = Total_Ser_Time + Ser_Time[i];
if (i == 0){
Stay_Time[i] = Ser_Time[i];
Free_Time[i] = 0;
Arr_Time[i] = 0;
Ser_Star[i] = 0;
Wait_Time[i] = 0;
Ser_End[i] = Ser_Star[i] + Ser_Time[i];
}else{
Stay_Time[i] = Stay_Time[i-1] - Int_Time[i] + Ser_Time[i];
Arr_Time[i] = Arr_Time[i-1] + Int_Time[i];
Ser_Star[i] = Ser_Star[i-1] + Ser_Time[i-1];
Wait_Time[i] = Ser_Star[i] - Arr_Time[i];
Ser_End[i] = Ser_Star[i] + Ser_Time[i];
if(Int_Time[i] - Stay_Time[i-1] > 0)
Free_Time[i] = Int_Time[i] - Stay_Time[i-1];
else
Free_Time[i] = 0;
}
}
//End simulation
//1.Maximum length of waiting line
k = 0;
for (i = 0; i < 1000; i++){
for (j = k + 1; j < 999 ; j++){
if (Ser_End[i] - Arr_Time[j] >= 0){
Line = Line + 1;
k++;
}else{
if (Line == 0)
k++;
if (Line > Max_Line){
Max_Line = Line;
k++;
Line--;
}
break;
}
}
}
ans1 = Max_Line;
//End Q1
//2.Proportion of time server is busy
for (i = 0; i < 1000; i++){
Total_Free_Time = Total_Free_Time + Free_Time[i];
}
ans2 = 1-((float)Total_Free_Time/((float)Total_Free_Time + (float)Total_Ser_Time));
//End Q2
//3.Proportion of customers who spent 4 or more minutes in the system
total = 0;
for (i = 0; i < 1000; i++){
if (Stay_Time[i] >= 4){
total++;
}
}
ans3 = (float)total/1000;
//End Q3
//4.Average stay time per customer in the system
total = 0;
for (i = 0; i < 1000; i++){
total = total + Stay_Time[i];
}
ans4 = (float)total/1000;
//End Q4
//Show answers
printf("Maximum length of waiting line = %d\n", ans1);
printf("Proportion of time server is busy = %.4f\n", ans2);
printf("Proportion of customers who spent 4 or more minutes in the system = %.4f\n", ans3);
printf("Average stay time per customer in the system = %.4f Minutes\n", ans4);
}