/** * @file main.cpp * @brief Example usage of the Array class */ #include #include /** * @class Array * @brief A dynamic array class with resize and index bounds checking */ class Array { private: double *mem; /**< Pointer to the memory block */ int size; /**< Current size of the array */ public: /** * @brief Constructor * @param size Initial size of the array */ Array(int size){ this->size = size; mem = new double[size]; } /** * @brief Resize the array * @param newSize New size of the array */ void resize(int newSize){ double *newMem = new double[newSize]; for (int i=0;i=size){ resize(index+1000); } mem[index]=data; } /** * @brief Read data from a specific index of the array * @param index Index of the array * @return Data at the specified index, or 0 if index is out of bounds */ double read(int index){ if (index>=size){ return 0; } return mem[index]; } /** * @brief Operator overload for array indexing * @param index Index of the array * @return Reference to the element at the specified index, with index bounds checking */ double &operator[](const int index){ if (index<0){ printf("Array index (%d) out of bounds\n",index); exit(0); } if (index>=size){ resize(index+1000); } return mem[index]; } }; /** * @brief Main function * @param argc Number of command-line arguments * @param argv Command-line argument values * @return 0 on success */ int main(int argc, char *argv[]){ Array myArray(40); myArray.write(55,3.1415); printf("%f\n",myArray.read(55)); myArray[56] = 1.23456; printf("array[56] = %f\n",myArray[56]); return 0; }