72. CalibTable Constructor Copy

#include <iostream>
using namespace std;

class CalibTable {
private:
    int size;
    int table[10];

public:
    CalibTable(int n, int arr[]) {
        if (n < 1) size = 1;
        else if (n > 10) size = 10;
        else size = n;

        for (int i = 0; i < size; i++) {
            table[i] = arr[i];
        }
    }

    int get(int index) {
        if (index < 0 || index >= size) return 0;
        return table[index];
    }

    void print() {
        for (int i = 0; i < size; i++) {
            cout << table[i];
            if (i + 1 < size) cout << " ";
        }
    }
};

int main() {
    int n;
    cin >> n;

    int arr[100];
    int countToRead = (n < 1 ? 1 : n);

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

    CalibTable t(n, arr);
    t.print();

    return 0;
}

Explanation & Logic Summary:

  • The constructor clamps size to prevent buffer overflow
  • At least one calibration value is always read
  • Only valid elements are copied into the internal table
  • Bounds checking prevents invalid access
  • Behavior is deterministic and safe for embedded systems

Firmware Relevance & Real-World Context:

  • Calibration tables are commonly loaded from flash or EEPROM
  • Defensive size clamping prevents memory corruption
  • Ensuring at least one valid entry avoids boot-time faults
  • Mirrors safe initialization patterns used in embedded drivers

 

 

 

 

Loading...

Input

5 10 20 30 40 50

Expected Output

10 20 30 40 50