#include // The 'Array.hpp' file contains all the class and // function declarations, as well as the class definition. // // The 'Pair.hpp' file contains the declaration and definition // of the Pair class used by the maxmin(...) function. // Because 'Array.hpp' includes 'Pair.hpp', we do not need // to include 'Pair.hpp' again here. // // The 'Array.cpp' file contains all the function definitions. // You can treat all of these functions as if they were // defined in this file. #include "Array.hpp" // Function declaration (for this file only) int main(); // Function definition (for this file only) int main() { // Initialize all member variables to their default values. // In particular, 'capacity_' is initialized to 0 and // 'array_' is initialized to nullptr. Array data{}; // Allocate an array with capacity 5. Because no initial // value is supplied, the default value 0.0 is used. array_init( data, 5 ); std::cout << "After initializing the array:" << std::endl; std::cout << " capacity = " << data.capacity_ << std::endl; std::cout << " data = " << to_string( data ) << std::endl; // Query the user for each entry in the array. std::cout << std::endl << "Enter " << data.capacity_ << " values:" << std::endl; for ( std::size_t k{0}; k < data.capacity_; ++k ) { std::cout << " data[" << k << "] = "; std::cin >> data.array_[k]; } // Display the resulting array. std::cout << std::endl << "The array you entered:" << std::endl; std::cout << " data = " << to_string( data ) << std::endl; // Test is_sorted(...). // - If the returned index equals the capacity, all // entries are in order. // - Otherwise, the returned index identifies the first // entry that is less than the entry before it. 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 array is sorted." << std::endl; } else { std::cout << " The array is not sorted." << std::endl; std::cout << " The first out-of-order entry is at index " << first_unsorted << "." << std::endl; std::cout << " data[" << first_unsorted - 1 << "] = " << data.array_[first_unsorted - 1] << std::endl; std::cout << " data[" << first_unsorted << "] = " << data.array_[first_unsorted] << std::endl; } // Test average(...). std::cout << std::endl << "Calculating the average:" << std::endl; std::cout << " average = " << average( data ) << std::endl; // Test maxmin(...). // - 'first_' is the index of the maximum entry. // - 'second_' is the index of the minimum entry. Pair result{ maxmin( data ) }; std::cout << std::endl << "Finding the 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; // Test array_reset(...) by resetting every entry to 0.0. // Because no second argument is supplied, the default // value 0.0 is used. std::cout << std::endl << "Resetting all entries to the default value:" << std::endl; array_reset( data ); std::cout << " data = " << to_string( data ) << std::endl; // Test array_reset(...) again, this time explicitly // specifying the value assigned to every entry. std::cout << std::endl << "Resetting all entries to 3.5:" << std::endl; array_reset( data, 3.5 ); std::cout << " data = " << to_string( data ) << std::endl; // Test array_init(...) again. Because 'data' already // refers to a dynamically allocated array, array_init(...) // first deallocates that array before allocating a new one. std::cout << std::endl << "Reinitializing the array:" << std::endl; array_init( data, 3, -2.5 ); std::cout << " capacity = " << data.capacity_ << std::endl; std::cout << " data = " << to_string( data ) << std::endl; // Test array_clean_up(...). // This deallocates the dynamic array and resets the // capacity to 0 and the pointer to nullptr. std::cout << std::endl << "Cleaning up the array:" << std::endl; array_clean_up( data ); std::cout << " capacity = " << data.capacity_ << std::endl; std::cout << " data = " << to_string( data ) << std::endl; return 0; }