-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
84 lines (73 loc) · 3.02 KB
/
Copy pathmain.c
File metadata and controls
84 lines (73 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Programmed and commented(for better-explanation) by Ajay Kumar Mishra :)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include "queue.h" // contains the Customer and Queue defs
#include "stats.h" // contains the statistical formulae functions
int poisson(double lam){ // Knuth's algo for random variate generation(Poisson)
double L = exp(-lam);
double p = 1.0;
int k = 0;
do{
k++;
double u = rand()/(RAND_MAX + 1.0);
p *= u;
} while(p > L);
return k - 1;
}
int main(){
srand(time(NULL)); // seeding on the basis of current time
double lambda; // lambda(or mean value) as required in the formula of Poisson PMF
printf("Enter avg no. of customers per minutee(ie lambda): "); scanf("%lf", &lambda);
int nextID = 1, tellerBusyTime = 0; // initially, teller's not busy
Queue q; initQueue(&q);
int capacity = 10000; // let us initiate waiting time list as 10^4 for now
double *waitTimes = (double *) malloc(sizeof(double) * capacity); // malloc instead of calloc, more efficient and calloc is not really needed now
int servedCnt = 0; // a no-brainer, initially the teller didn't serve
for(int i = 0; i < (8 * 60); ++i){ // 8 hours * 60 = 480 minutes, ie 480 iterations
int noOfarrivals = poisson(lambda);
for(int j = 0; j < noOfarrivals; ++j){
enqueue(&q, nextID++, i);
}
if(tellerBusyTime > 0){
--tellerBusyTime; // decrease 1 minute(per iteration) is the teller is busy
} else if(q.head != NULL){ // if teller is not busy and there's still someone left in the waiting queue
Customer *c = dequeue(&q);
double w8time = i - c->arrivalTime;
if(servedCnt >= capacity){
capacity *= 2;
waitTimes = realloc(waitTimes, sizeof(double) * capacity); // reallocating if the pre-defined size limit is exceeded
}
waitTimes[servedCnt++] = w8time;
tellerBusyTime = 2 + rand()%2;
free(c);
}
}
if(servedCnt == 0) { // served count is 0, ie no customers served
printf("No customers were served\n");
return 0;
}
// now just applying the functions to get the desired stat-results(I made that up lmao) ->
double avg = mean(waitTimes, servedCnt);
double med = median(waitTimes, servedCnt);
double mod = mode(waitTimes, servedCnt);
double sd = stdDev(waitTimes, servedCnt, avg);
double max = waitTimes[0];
for(int i = 0; i < servedCnt; ++i){
if(waitTimes[i] > max){
max = waitTimes[i];
}
}
printf("\n===== BANK QUEUE SIMULATION REPORT WITH POISSON DISTRIBUTION =====\n");
printf("Total customers served: %d\n", servedCnt);
printf("Average wait time: %.2f minutes\n", avg);
printf("Median wait time: %.2f minutes\n", med);
printf("Mode wait time: %.2f minutes\n", mod);
printf("Standard deviation: %.2f\n", sd);
printf("Longest wait time: %.2f minutes\n", max);
printf("\n===== THANK YOU FOR USING MY SILLY PROGRAM, GET ME A CupOfCoffee ALREADY :) =====\n"); // is my program really THAT silly?
free(waitTimes);
return 0;
}