CalibTable Constructor Copy

#include <iostream>
using namespace std;

class CalibTable {
private:
    int size;
    int table[10]; // Fixed-size internal storage

public:
    // Constructor handles the clamping logic
    CalibTable(int n, int arr[]) {
        // 1. Clamp the size: Minimum 1, Maximum 10
        if (n < 1) {
            size = 1;
        } else if (n > 10) {
            size = 10;
        } else {
            size = n;
        }

        // 2. Copy the elements from the input array to the internal table
        for (int i = 0; i < size; i++) {
            table[i] = arr[i];
        }
    }

    // Returns the value at index, or 0 if out of bounds
    int get(int index) {
        if (index >= 0 && index < size) {
            return table[index];
        }
        return 0;
    }

    // Prints all stored values separated by a space
    void print() {
        for (int i = 0; i < size; i++) {
            cout << table[i] << (i == size - 1 ? "" : " ");
        }
        cout << endl;
    }
};

int main() {
    int n;
    if (!(cin >> n)) return 0;

    int arr[100];
    
    // Logic as per your requirement: read at least 1 even if n is 0 or negative
    int countToRead = (n < 1 ? 1 : n);

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

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

    return 0;
}

Solving Approach

 

 

 

 

 

Upvote
Downvote
Loading...

Input

5 10 20 30 40 50

Expected Output

10 20 30 40 50