-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbank_queue
More file actions
415 lines (331 loc) · 11.3 KB
/
bank_queue
File metadata and controls
415 lines (331 loc) · 11.3 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
Bank Queue Simulation Details given as per guidlines -
- 480-minute main loop with a for-loop
- Poisson arrivals per minute
- Dynamic linked-list queue using malloc/free
- Service time 2 or 3 minutes
- Wait times recorded in a dynamic array
- Functions used for major tasks (customer_arrives, assign_tellers, process_services)
- Final report: Mean, Median, Mode, Standard Deviation, Longest Wait.
*/
//----------Standard Libraries----------------------
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
// STRUCTURE DEFINITIONS
// Structure for each customer in queue
typedef struct Customer {
int arrival_time; // When customer arrives at bank
int service_start_time; // When teller starts serving
int wait_time; // Time spent waiting before service
struct Customer* next; // Pointer to next customer (for linked list)
} Customer;
// Structure for queue using linked list
typedef struct {
Customer* front; // First customer in queue
Customer* rear; // Last customer in queue
int size; // Current queue size
} Queue;
// Structure for each teller (bank counter)
typedef struct {
int busy_until; // Minute until which teller is busy
int current_customer_id; // ID or time when current customer arrived (just for tracking)
} Teller;
// Dynamic array to store all wait times for analysis
typedef struct {
int* data; // Array of wait times
int size; // Number of wait times stored
int capacity; // Allocated memory size
} WaitTimeArray;
// ---------------------------
// QUEUE FUNCTIONS
// ---------------------------
// Initialize empty queue
void init_queue(Queue* q) {
q->front = NULL;
q->rear = NULL;
q->size = 0;
}
// Add (enqueue) a new customer to queue
void enqueue(Queue* q, int arrival_time) {
Customer* new_customer = (Customer*)malloc(sizeof(Customer));
new_customer->arrival_time = arrival_time;
new_customer->service_start_time = -1;
new_customer->wait_time = 0;
new_customer->next = NULL;
// If queue is empty, new customer becomes both front and rear
if (q->rear == NULL) {
q->front = q->rear = new_customer;
} else {
q->rear->next = new_customer;
q->rear = new_customer;
}
q->size++;
}
// Remove (dequeue) customer from front of queue
Customer* dequeue(Queue* q) {
if (q->front == NULL) {
return NULL; // Queue empty
}
Customer* temp = q->front;
q->front = q->front->next;
// If queue becomes empty after dequeue
if (q->front == NULL) {
q->rear = NULL;
}
q->size--;
return temp;
}
// Free all memory used by queue
void free_queue(Queue* q) {
while (q->front != NULL) {
Customer* temp = dequeue(q);
free(temp);
}
}
// ---------------------------
// WAIT TIME ARRAY FUNCTIONS
// ---------------------------
// Create dynamic array for storing wait times
WaitTimeArray* create_wait_time_array() {
WaitTimeArray* arr = (WaitTimeArray*)malloc(sizeof(WaitTimeArray));
arr->capacity = 1000;
arr->size = 0;
arr->data = (int*)malloc(arr->capacity * sizeof(int));
return arr;
}
// Add a wait time to the array
void add_wait_time(WaitTimeArray* arr, int wait_time) {
if (arr->size >= arr->capacity) {
// Increase capacity if full
arr->capacity *= 2;
arr->data = (int*)realloc(arr->data, arr->capacity * sizeof(int));
}
arr->data[arr->size++] = wait_time;
}
// Free memory of wait time array
void free_wait_time_array(WaitTimeArray* arr) {
free(arr->data);
free(arr);
}
// ---------------------------
// POISSON FUNCTIONS
// ---------------------------
// Factorial function for Poisson formula
unsigned long long factorial(int n) {
if (n <= 1) return 1;
unsigned long long result = 1;
for (int i = 2; i <= n; i++) {
result *= i;
}
return result;
}
// Poisson probability function
double poisson_probability(double lambda, int k) {
return (pow(lambda, k) * exp(-lambda)) / factorial(k);
}
// Simulate number of customers arriving in a given minute
int customer_arrives(double lambda) {
double random_val = (double)rand() / RAND_MAX; // Random between 0 and 1
double cumulative = 0.0;
// Calculate cumulative probability for 0–9 arrivals
for (int k = 0; k < 10; k++) {
cumulative += poisson_probability(lambda, k);
if (random_val <= cumulative) {
return k; // Return number of arrivals
}
}
return 0;
}
// ---------------------------
// SERVICE LOGIC (TELLERS)
// ---------------------------
// Teller serves next customer if free
int serve_customer(Queue* q, Teller* teller, int current_minute, int service_time) {
// Teller is free and someone is waiting
if (teller->busy_until <= current_minute && q->size > 0) {
Customer* customer = dequeue(q);
// Record time service starts and waiting time
customer->service_start_time = current_minute;
customer->wait_time = current_minute - customer->arrival_time;
// Teller will be busy for 'service_time' minutes
teller->busy_until = current_minute + service_time;
teller->current_customer_id = customer->arrival_time;
int wait = customer->wait_time;
free(customer); // Customer leaves after being served
return wait; // Return how long they waited
}
return -1; // No service happened this minute
}
int compare_ints(const void* a, const void* b) {
return (*(int*)a - *(int*)b);
}
double calculate_mean(int* data, int size) {
if (size == 0) return 0.0;
double sum = 0.0;
for (int i = 0; i < size; i++) {
sum += data[i];
}
return sum / size;
}
double calculate_median(int* data, int size) {
if (size == 0) return 0.0;
// Make a copy to sort
int* sorted = (int*)malloc(size * sizeof(int));
for (int i = 0; i < size; i++) {
sorted[i] = data[i];
}
qsort(sorted, size, sizeof(int), compare_ints);
double median;
if (size % 2 == 0) {
median = (sorted[size/2 - 1] + sorted[size/2]) / 2.0;
} else {
median = sorted[size/2];
}
free(sorted);
return median;
}
// Mode = most frequent wait time
int calculate_mode(int* data, int size) {
if (size == 0) return 0;
int* sorted = (int*)malloc(size * sizeof(int));
for (int i = 0; i < size; i++) {
sorted[i] = data[i];
}
qsort(sorted, size, sizeof(int), compare_ints);
int mode = sorted[0];
int max_count = 1;
int current_count = 1;
for (int i = 1; i < size; i++) {
if (sorted[i] == sorted[i-1]) {
current_count++;
} else {
if (current_count > max_count) {
max_count = current_count;
mode = sorted[i-1];
}
current_count = 1;
}
}
if (current_count > max_count) {
mode = sorted[size-1];
}
free(sorted);
return mode;
}
double calculate_std_dev(int* data, int size, double mean) {
if (size == 0) return 0.0;
double sum_squared_diff = 0.0;
for (int i = 0; i < size; i++) {
double diff = data[i] - mean;
sum_squared_diff += diff * diff;
}
return sqrt(sum_squared_diff / size);
}
int find_max(int* data, int size) {
if (size == 0) return 0;
int max = data[0];
for (int i = 1; i < size; i++) {
if (data[i] > max) {
max = data[i];
}
}
return max;
}
// ---------------------------
// FINAL REPORT
// ---------------------------
void print_final_report(WaitTimeArray* wait_times, int total_arrived, int total_served, int remaining) {
if (wait_times->size > 0) {
double mean = calculate_mean(wait_times->data, wait_times->size);
double median = calculate_median(wait_times->data, wait_times->size);
int mode = calculate_mode(wait_times->data, wait_times->size);
double std_dev = calculate_std_dev(wait_times->data, wait_times->size, mean);
int max_wait = find_max(wait_times->data, wait_times->size);
printf("===== WAIT TIME ANALYSIS =====\n");
printf("Mean wait time: %.2f minutes\n", mean);
printf("Median wait time: %.2f minutes\n", median);
printf("Mode wait time: %d minutes\n", mode);
printf("Standard Deviation: %.2f minutes\n", std_dev);
printf("Longest wait time: %d minutes\n", max_wait);
printf("===============================\n\n");
// Simple suggestion for manager
if (mode > 10) {
printf("RECOMMENDATION: Average wait time exceeds 10 minutes.\n");
printf("Consider hiring additional tellers.\n");
} else {
printf("STATUS: Wait times are acceptable.\n");
}
} else {
printf("No customers were served during the simulation.\n");
}
}
// ---------------------------
// MAIN FUNCTION
// ---------------------------
int main() {
srand(time(NULL)); // Random seed for Poisson arrivals
double lambda; // Average arrivals per minute
int num_tellers;
int service_time;
printf("\n");
printf("========================================\n");
printf(" BANK QUEUE SIMULATOR (8-hour day)\n");
printf("========================================\n\n");
// Take inputs from user
printf("Enter the average number of customers arriving per minute (λ): ");
scanf("%lf", &lambda);
if (lambda <= 0) {
printf("Error: Lambda must be positive.\n");
return 1;
}
printf("Enter the number of tellers: ");
scanf("%d", &num_tellers);
if (num_tellers <= 0) {
printf("Error: Number of tellers must be positive.\n");
return 1;
}
printf("Enter the service time per customer (min.): ");
scanf("%d", &service_time);
if (service_time <= 0) {
printf("Error: Service time must be positive.\n");
return 1;
}
// Initialize data structures
Queue queue;
init_queue(&queue);
Teller* tellers = (Teller*)malloc(num_tellers * sizeof(Teller));
for (int i = 0; i < num_tellers; i++) {
tellers[i].busy_until = 0;
tellers[i].current_customer_id = -1;
}
WaitTimeArray* wait_times = create_wait_time_array();
int total_customers_arrived = 0;
int total_customers_served = 0;
printf("\nSimulating 8-hour day (480 minutes)...\n");
for (int minute = 0; minute < 480; minute++) {
// Determine number of new arrivals this minute using Poisson distribution
int arrivals = customer_arrives(lambda);
total_customers_arrived += arrivals;
// Add each arriving customer to queue
for (int i = 0; i < arrivals; i++) {
enqueue(&queue, minute);
}
// Each teller checks if they can serve someone
for (int t = 0; t < num_tellers; t++) {
int wait = serve_customer(&queue, &tellers[t], minute, service_time);
if (wait >= 0) {
add_wait_time(wait_times, wait);
total_customers_served++;
}
}
}
// Display summary report at the end
print_final_report(wait_times, total_customers_arrived, total_customers_served, queue.size);
// Free all allocated memory
free_queue(&queue);
free(tellers);
free_wait_time_array(wait_times);
return 0;
}