Sensor Packet Handling

#include <iostream>
using namespace std;

class Packet {
private:
    // Private members as per constraints
    int id;
    int temperature;
    int humidity;

public:
    // Constructor: Sets id and initializes temp/hum to 0
    Packet(int packetId) {
        id = packetId;
        temperature = 0;
        humidity = 0;
    }

    // Method to update sensor values
    void update(int t, int h) {
        temperature = t;
        humidity = h;
    }

    // Method to print the packet in the exact required format
    void print() {
        cout << "ID=" << id 
             << " TEMP=" << temperature 
             << " HUM=" << humidity << endl;
    }
};

int main() {
    int id, t1, h1, t2, h2;
    
    // Read input values
    if (!(cin >> id >> t1 >> h1 >> t2 >> h2)) return 0;

    // Create the Packet object
    Packet pkt(id);
    
    // Perform updates as requested
    pkt.update(t1, h1);
    pkt.update(t2, h2);

    // Display final state
    pkt.print();
    
    return 0;
}

Solving Approach

 

 

 

 

 

Upvote
Downvote
Loading...

Input

7 20 40 25 45

Expected Output

ID=7 TEMP=25 HUM=45