sudipto1.c (7423B)
1 /* 2 * Author : Amit Dutta <amitdutta4255@gmail.com> 3 * Date : 12 Dec 2025 4 * Repo : https://github.com/notamitgamer/bsc 5 * License : MIT License (See the LICENSE file for details) 6 */ 7 8 /* A smart home security controller monitors the state of several sensors to decide what action to take. 9 * Each second, the system reads data from sensors that can either be active or inactive. Based on the current 10 * state of all sensors, the controller must perform exactly one action, such as activating a warning, checking 11 * a specific zone, or declaring an intrusion. Write a C program that reads the sensor states as input and 12 * prints the corresponding system action. 13 -- Conditions: 14 * 1. You must determine exactly one action for every possible combination of sensor states. 15 * 2. You are not allowed to use any conditional statements (if, else if, else, or the ternary operator?). 16 * 3. You are not allowed to use logical operators. 17 * 4. You must use a single switch statement to control the program's behaviour. 18 * 5. Loops may only be used for reading input, not for making decisions. 19 * 6. The program should handle all possible combinations of the sensors' active/inactive states and print the appropriate response each time. 20 -- Example (for understanding): 21 * If all sensors are inactive, the system should remain idle. 22 * If some sensors are active, the system should issue warnings or alerts based on the situation. 23 * If all sensors are active, the system should declare a confirmed intrusion. 24 */ 25 26 /* 27 * This program monitors the state of four sensors (Door, Window, Motion, Glass Break) 28 * and determines the appropriate action without using any conditional (if, else) 29 * or logical (&&, ||) operators. 30 * 31 * It works by combining the state of all sensors into a single integer value 32 * using bitmasking. Each bit in the integer represents the state of one sensor 33 * (1 for active, 0 for inactive). This combined state is then used in a single 34 * switch statement to select the correct action for every possible combination. 35 * 36 * Then, enter the state of the four sensors separated by spaces, 37 * for example: 1 0 1 0 38 */ 39 40 41 #include <stdio.h> 42 int main() 43 { 44 45 // variables to hold the state of a sensor (0 or 1) 46 int doorSensorState, windowSensorState, motionSensorState, glassbreakSensorState; 47 48 // variable to check the combined state of all four sensor 49 int combinedState = 0; 50 51 // a invalid flag to check the input are valid or not 52 unsigned int invalidFlag; 53 54 // printing the way to usage the program 55 printf("------ Home Security Controller ------\n"); 56 printf("Enter sensor states (1 for active, 0 for inactive).\n"); 57 printf("Format: [Door] [Window] [Motion] [Glass Break]\n"); 58 printf("Example: 0 1 1 0\n"); 59 printf("Enter states (or press Ctrl+D for MacOS or Linux / Ctrl+Z for Windows to exit): "); 60 61 // doing the main calculation 62 // chacking the combined state and taking a decision 63 while (scanf("%d %d %d %d", &doorSensorState, &windowSensorState, &motionSensorState, &glassbreakSensorState) == 4) 64 { 65 // validating input 66 // The expression (variable & ~1) results in 0 only if 'variable' is 0 or 1. 67 // For any other positive number, it's non-zero. 68 // We combine all checks with a bitwise OR. If any sensor state is invalid, 69 // the flag will become non-zero. 70 invalidFlag = (doorSensorState & ~1) | (windowSensorState & ~1) | (motionSensorState & ~1) | (glassbreakSensorState & ~1); 71 72 // Combine the four sensor states into a single integer using bitwise operations. 73 // This creates a unique number from 0 to 15 for each possible combination. 74 // Bit 0: Door Sensor 75 // Bit 1: Window Sensor 76 // Bit 2: Motion Sensor 77 // Bit 3: Glass Break Sensor 78 combinedState = (glassbreakSensorState << 3) | (motionSensorState << 2) | (windowSensorState << 1) | doorSensorState; 79 80 // If the invalid_input_flag is non-zero, we add 16 to the state. 81 // This pushes it outside the 0-15 range and forces the 'default' case in the switch. 82 // We achieve this by multiplying the flag (which is > 0) by 16. 83 combinedState += invalidFlag * 16; 84 85 // printing the given state 86 printf("\n\nState : [Door : %d, Window : %d, Motion : %d, Glass Break : %d]\nState Id : %d", 87 doorSensorState, windowSensorState, motionSensorState, glassbreakSensorState, combinedState); 88 printf("\nSystem Action : "); 89 90 // taking decision based on combined state 91 // A single switch statement controls the program's behavior. 92 // It handles all 2^4 = 16 possible combinations of sensor states, 93 // ensuring exactly one action is taken for every scenario. 94 switch (combinedState) 95 { 96 case 0: // Binary: 0000 97 printf("System Idle. All sensors inactive.\n"); 98 break; 99 case 1: // Binary: 0001 100 printf("Check front door camera.\n"); 101 break; 102 case 2: // Binary: 0010 103 printf("Check window sensors.\n"); 104 break; 105 case 3: // Binary: 0011 106 printf("Warning: Perimeter breach suspected. Check doors and windows.\n"); 107 break; 108 case 4: // Binary: 0100 109 printf("Check interior cameras for movement.\n"); 110 break; 111 case 5: // Binary: 0101 112 printf("Warning: Potential entry and movement detected. Check front door and interior. Ask for patrol.\n"); 113 break; 114 case 6: // Binary: 0110 115 printf("Warning: Potential entry and movement detected. Check windows and interior. Ask for patrol.\n"); 116 break; 117 case 7: // Binary: 0111 118 printf("Alert: High probability of unauthorized entry. Ask for patrol.\n"); 119 break; 120 case 8: // Binary: 1000 121 printf("Alert: Glass break detected. High-priority event. Ask for patrol.\n"); 122 break; 123 case 9: // Binary: 1001 124 printf("Severe Alert: Forced entry likely (door + glass). Activate Alarm and Contact authorities.\n"); 125 break; 126 case 10: // Binary: 1010 127 printf("Severe Alert: Forced entry likely (window + glass). Activate Alarm and Contact authorities.\n"); 128 break; 129 case 11: // Binary: 1011 130 printf("Intrusion Alert: Multiple perimeter breaches. Activate Alarm and Contact authorities.\n"); 131 break; 132 case 12: // Binary: 1100 133 printf("Intrusion Alert: Confirmed interior presence after breach. Activate Alarm and Contact authorities.\n"); 134 break; 135 case 13: // Binary: 1101 136 printf("INTRUSION CONFIRMED! Activate maximum response protocol. Activate Alarm and Contact authorities.\n"); 137 break; 138 case 14: // Binary: 1110 139 printf("INTRUSION CONFIRMED! Activate maximum response protocol. Activate Alarm and Contact authorities.\n"); 140 break; 141 case 15: // Binary: 1111 142 printf("CATASTROPHIC EVENT! ALL SENSORS TRIGGERED. ACTIVATE ALARM AND CONTACT COPS.\n"); 143 break; 144 default: 145 // This case should not be reached with valid 0/1 input but is included for completeness. 146 printf("Error: Invalid sensor state detected.\n"); 147 break; 148 } 149 150 // discarding extra characters (if any) left 151 while (getchar() != '\n') 152 ; 153 154 // asking for next set of input 155 printf("\nEnter next set of states: "); 156 } 157 printf("\nSecurity system shutting down.\n"); 158 }