#include "Array.hpp" #include ///////////////////////////////////////////////// // This file contains all function definitions // // - All function declarations are in // // the file Array.hpp // ///////////////////////////////////////////////// // Initialize 'array' with the given capacity, and // initialize every entry to 'initial_value'. void array_init( Array &array, std::size_t const capacity, double const initial_value ) { // Assume that if the array member variable is not // 'nullptr', then memory was previously allocated // and must first be deallocated. // // Note: This assumption is only safe if the user // initially set the member variables appropriately: // // Array array{ 0, nullptr }; // // If 'array_' was never initialized, its value is // indeterminate and testing it against 'nullptr' // results in undefined behavior. if ( array.array_ != nullptr ) { delete[] array.array_; array.array_ = nullptr; array.capacity_ = 0; } // Store the requested capacity and dynamically allocate // sufficient memory for that many double values. array.capacity_ = capacity; array.array_ = new double[array.capacity_]; // Initialize every entry to the requested value. for ( std::size_t k{0}; k < array.capacity_; ++k ) { array.array_[k] = initial_value; } } // Deallocate the dynamically allocated array and reset // the member variables to represent an empty array. void array_clean_up( Array &array ) { // In C++, it is completely acceptable to call 'delete[]' // on a null pointer, so no conditional statement is needed. delete[] array.array_; // The pointer must not continue to refer to memory that // has been deallocated. array.array_ = nullptr; array.capacity_ = 0; } // Reset every entry in 'array' to 'new_value'. void array_reset( Array &array, double const new_value ) { // There is nothing to reset if no array is allocated. if ( array.array_ == nullptr ) { return; } // Assign the new value to every entry. for ( std::size_t k{0}; k < array.capacity_; ++k ) { array.array_[k] = new_value; } } // Calculate the average of the entries in 'array'. double average( Array const &array ) { // There is no average if the array contains no entries. // For this example, return 0.0 in this case. if ( array.capacity_ == 0 ) { return 0.0; } // Add all the entries. double sum{ 0.0 }; for ( std::size_t k{0}; k < array.capacity_; ++k ) { sum += array.array_[k]; } // Divide the sum by the number of entries. return sum/array.capacity_; } // Determine whether the entries in 'array' are sorted // from least to greatest. std::size_t is_sorted( Array const &array ) { // An empty array has no out-of-order entries. if ( array.capacity_ == 0 ) { return 0; } // Compare each entry with the entry immediately before it. // If the current entry is smaller, return its index. for ( std::size_t k{1}; k < array.capacity_; ++k ) { if ( array.array_[k] < array.array_[k - 1] ) { return k; } } // If no out-of-order entry was found, return the capacity. return array.capacity_; } // Return a comma-separated string containing the entries // in 'array', enclosed in braces. std::string to_string( Array const &array ) { // Represent an empty array by a pair of braces. if ( array.capacity_ == 0 ) { return "{}"; } // Add the first entry without a preceding comma. std::string result{ "{" + std::to_string( array.array_[0] ) }; // Add each remaining entry with a preceding comma and space. for ( std::size_t k{1}; k < array.capacity_; ++k ) { result += ", " + std::to_string( array.array_[k] ); } return result + "}"; } // Return the indices of the maximum and minimum entries. // - 'first_' is the index of the maximum entry. // - 'second_' is the index of the minimum entry. Pair maxmin( Array const &array ) { // A maximum and minimum exist only if the array contains // at least one entry. assert( array.capacity_ > 0 ); assert( array.array_ != nullptr ); // Initially, the first entry is both the largest and // smallest entry encountered. std::size_t imax{ 0 }; std::size_t imin{ 0 }; // Examine each remaining entry, updating the appropriate // index whenever a new maximum or minimum is encountered. for ( std::size_t k{1}; k < array.capacity_; ++k ) { if ( array.array_[k] > array.array_[imax] ) { imax = k; } else if ( array.array_[k] < array.array_[imin] ) { imin = k; } } return Pair{ imax, imin }; }