18. GPIO Pin Toggle

#include <iostream>
using namespace std;

// Inline function that toggles a pin state
inline int togglePin(int currentState) {
    return currentState == 0 ? 1 : 0;
}

int main() {
    int state[10];

    for (int i = 0; i < 10; i++) {
        cin >> state[i];
    }

    int n;
    cin >> n;

    for (int i = 0; i < n; i++) {
        int pin;
        cin >> pin;
        state[pin] = togglePin(state[pin]);
    }

    for (int i = 0; i < 10; i++) {
        cout << state[i];
        if (i < 9) cout << " ";
    }

    return 0;
}

Explanation & Logic Summary:

  • The inline function performs a simple state inversion.
  • Each toggle request updates the pin immediately.
  • Repeated indices cause repeated toggles.
  • Inline functions reduce call overhead in tight loops.

Firmware Relevance & Real-World Context:

  • GPIO toggling is fundamental in embedded systems (LEDs, diagnostics, timing).
  • Inline functions are commonly used in HAL and driver layers.
  • Reinforces safe array indexing assumptions typical in MCU firmware.
  • Represents simplified behavior of memory-mapped GPIO registers.

 

 

 

 

Loading...

Input

0 0 0 0 0 0 0 0 0 0 1 5

Expected Output

0 0 0 0 0 1 0 0 0 0