#include <stdio.h> #include <unistd.h> // For sleep function #define BUCKET_CAPACITY 10 // Maximum capacity of the bucket #define LEAK_RATE 1 // Amount of water that leaks per second // Struct to represent the bucket typedef struct { int currentWater; // Current amount of water in the bucket } LeakyBucket; // Initialize the bucket by setting its water level to zero void initBucket(LeakyBucket *bucket) { bucket->currentWater = 0; } // Function to add water to the bucket void addWater(LeakyBucket *bucket, int water) { // Check if adding water would exceed the bucket's capacity if (bucket->currentWater + water > BUCKET_CAPACITY) { printf("Bucket overflow! Discarding excess water.\n"); } else { bucket->currentWater += water; printf("Added %d units of water. Current level: %d\n", water, bucket->currentWater); } } // Function to leak water from the bucket void leakWater(LeakyBucket *bucket) { // Only leak if there is water in the bucket if (bucket->currentWater > 0) { bucket->currentWater -= LEAK_RATE; if (bucket->currentWater < 0) { bucket->currentWater = 0; // Make sure water level doesn’t go negative } printf("Leaked 1 unit of water. Current level: %d\n", bucket->currentWater); } else { printf("Bucket is empty. No water to leak.\n"); } } // Main function to demonstrate adding and leaking water int main() { LeakyBucket bucket; // Create a bucket initBucket(&bucket); // Initialize the bucket // Simulate adding and leaking water in a simple loop for (int i = 0; i < 5; i++) { // Run 5 cycles printf("\nCycle %d:\n", i + 1); addWater(&bucket, 3); // Add 3 units of water each cycle leakWater(&bucket); // Leak 1 unit of water each cycle sleep(1); // Wait for 1 second to simulate time passing } return 0; }