Dynamically allocated arrays This example introduces a simple Array class that manages information about a dynamically allocated array of double-precision floating-point values. It also introduces a simple Pair class used when a function needs to return two indices. Files ===== Array.hpp Contains the declaration and definition of the Array class, together with the declarations of the functions that operate on Array objects. Array.cpp Contains the definitions of the functions declared in Array.hpp. Pair.hpp Contains the declaration and definition of the Pair class. main.cpp Contains examples demonstrating how to initialize, use, reset, reinitialize, and clean up an Array object. The Array class =============== An Array object stores two member variables: capacity_ array_ The first stores the number of entries in the array. The second is a pointer to a dynamically allocated array of double values. For example, Array data{}; creates an Array object whose members are initially zero and nullptr. The call array_init( data, 5 ); then dynamically allocates an array containing five entries. Functions are provided for operations including: - initializing an array, - cleaning up an array, - resetting all entries, - calculating the average, - determining where an array first becomes unsorted, - converting an array to a string, and - finding the indices of the maximum and minimum entries. The Pair class ============== The maxmin(...) function must return two indices: the index of the maximum entry and the index of the minimum entry. A Pair object stores these two indices: first_ second_ For the Pair returned by maxmin(...), first_ is the index of the maximum entry, and second_ is the index of the minimum entry. The C++ standard library provides the more general std::pair class. The simple Pair class used here avoids templates, which will be introduced later. Dynamic memory ============== The function array_init(...) allocates memory using new[], while array_clean_up(...) releases that memory using delete[]. Every successful allocation must eventually have a corresponding deallocation. Failing to release dynamically allocated memory results in a memory leak. After calling array_clean_up(...), the Array object is reset so that capacity_ == 0 and array_ == nullptr Important limitations ===================== This is deliberately a simple implementation for introducing dynamic memory and classes. The programmer is responsible for correctly initializing and cleaning up Array objects. For example, an Array should initially be created using Array data{}; rather than Array data; so that its member variables have appropriate initial values. Later, constructors and destructors can make initialization and cleanup automatic. This will remove much of the responsibility that is currently placed on the programmer and make the class safer and easier to use. Purpose ======= This example demonstrates classes, pointers, dynamic allocation, new[], delete[], references, const references, default arguments, and the separation of declarations and definitions into different files. It also motivates an important idea that will be introduced later: a class can take responsibility for acquiring and releasing its own resources. Compiling ========= Compile both main.cpp and Array.cpp. For example: g++ -std=c++17 -Wall -Wextra -pedantic main.cpp Array.cpp