bsc

Comprehensive codebase and cou...
Log | Files | Refs | Activity | README | LICENSE

root / semester_1 / temp / sudipto1.c

sudipto1.c (7232B)


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