dinamic

#include <iostream>

class DynamicArray {
private:
    int* arr;
    int size;
public:
    // Constructor to initialize the dynamic array with a given size
    DynamicArray(int initialSize) {
        size = initialSize;
        arr = new int[size];
    }

    // Destructor to free the memory allocated for the dynamic array
    ~DynamicArray() {
        delete[] arr;
    }

    // Function to set the value at a specific index in the array
    void setValue(int index, int value) {
        if (index >= 0 && index < size) {
            arr[index] = value;
        } else {
            std::cout << "Index out of bounds!" << std::endl;
        }
    }

    // Function to get the value at a specific index in the array
    int getValue(int index) const {
        if (index >= 0 && index < size) {
            return arr[index];
        } else {
            std::cout << "Index out of bounds!" << std::endl;
            return -1; // Return a default value (you may choose a different approach)
        }
    }

    // Function to print the elements of the array
    void printArray() const {
        for (int i = 0; i < size; ++i) {
            std::cout << arr[i] << " ";
        }
        std::cout << std::endl;
    }
};

int main() {
    // Create a dynamic array with an initial size of 5
    DynamicArray dynamicArray(5);

    // Set values in the array
    dynamicArray.setValue(0, 10);
    dynamicArray.setValue(1, 20);
    dynamicArray.setValue(2, 30);
    dynamicArray.setValue(3, 40);
    dynamicArray.setValue(4, 50);

    // Print the array
    std::cout << "Array elements: ";
    dynamicArray.printArray();

    // Get and print the value at index 2
    std::cout << "Value at index 2: " << dynamicArray.getValue(2) << std::endl;

    // Attempt to access an out-of-bounds index
    dynamicArray.getValue(10);

    return 0;
}