#include // The 'Array.hpp' file contains all the class and // function declarations, as well as the class definition. // // The 'Pair.hpp' file contains the Pair class declaration // and definition. // // The 'Array.cpp' file contains all the function // definitions. You can treat all of these as if they // were in this file. // // This example extends the previous Array project. // The primary focus here is operator overloading. #include "Array.hpp" #include "Pair.hpp" // Function declaration (for this file only) int main(); // Function definition (for this file only) int main() { // The Array object must first be initialized so that // capacity_ is zero and array_ is nullptr. Array data{}; // Initialize an array with capacity 5. Since no initial // value is supplied, each entry is initialized to 0.0. array_init( data, 5 ); std::cout << "Initially, data = " << data << std::endl; // Query the user for the five entries. for ( std::size_t k{0}; k < data.capacity_; ++k ) { std::cout << "Enter a number: "; std::cin >> data.array_[k]; } // The overloaded << operator allows an Array object to // be inserted directly into an output stream. std::cout << std::endl << "Output using operator<<:" << std::endl; std::cout << "data = " << data << std::endl; // The to_string(...) function remains available. std::cout << std::endl << "Output using to_string(...):" << std::endl; std::cout << "to_string( data ) = " << to_string( data ) << std::endl; // Determine whether the entries are sorted. std::size_t first_unsorted{ is_sorted( data ) }; std::cout << std::endl << "Testing whether the array is sorted:" << std::endl; if ( first_unsorted == data.capacity_ ) { std::cout << "The entries are sorted." << std::endl; } else { std::cout << "The entries are not sorted." << std::endl; std::cout << "The first out-of-order entries are " << data.array_[first_unsorted - 1] << " and " << data.array_[first_unsorted] << "." << std::endl; } // Calculate the average of the entries. std::cout << std::endl << "The average of the entries is " << average( data ) << "." << std::endl; // Find the indices of the maximum and minimum entries. Pair result{ maxmin( data ) }; std::cout << std::endl << "Maximum and minimum entries:" << std::endl; std::cout << "The maximum value is " << data.array_[result.first_] << " at index " << result.first_ << "." << std::endl; std::cout << "The minimum value is " << data.array_[result.second_] << " at index " << result.second_ << "." << std::endl; // Reset all entries. Since no value is supplied, each // entry is reset to 0.0. array_reset( data ); std::cout << std::endl << "After array_reset( data ):" << std::endl; std::cout << "data = " << data << std::endl; // Reset all entries to an explicitly supplied value. array_reset( data, 3.5 ); std::cout << std::endl << "After array_reset( data, 3.5 ):" << std::endl; std::cout << "data = " << data << std::endl; // Reinitialize the same Array object. The previously // allocated array is first deallocated. array_init( data, 3, -2.5 ); std::cout << std::endl << "After array_init( data, 3, -2.5 ):" << std::endl; std::cout << "data = " << data << std::endl; // Deallocate the dynamically allocated array. array_clean_up( data ); std::cout << std::endl << "After array_clean_up( data ):" << std::endl; std::cout << "data = " << data << std::endl; return 0; }