#include #include // Function declarations void reverse_array( double array[], std::size_t const capacity ); void print_array( double const array[], std::size_t const capacity ); int main(); // Function definitions int main() { double test5[5]{ 0.5, 1.5, 2.5, 3.5, 4.5 }; double test6[6]{ 0.6, 1.6, 2.6, 3.6, 4.6, 5.6 }; print_array( test5, 5 ); reverse_array( test5, 5 ); print_array( test5, 5 ); std::cout << std::endl; print_array( test6, 6 ); reverse_array( test6, 6 ); print_array( test6, 6 ); std::cout << std::endl; // Check the boundary cases: double test1[1]{ 0.7 }; double test2[2]{ 0.8, 1.8 }; print_array( nullptr, 0 ); reverse_array( nullptr, 0 ); print_array( nullptr, 0 ); std::cout << std::endl; print_array( test1, 1 ); reverse_array( test1, 1 ); print_array( test1, 1 ); std::cout << std::endl; print_array( test2, 2 ); reverse_array( test2, 2 ); print_array( test2, 2 ); return 0; } // Reverse the order of all the entries in the array. // // For example, // // {1.2, 3.4, 5.6, 7.8} // // becomes // // {7.8, 5.6, 3.4, 1.2}. void reverse_array( double array[], std::size_t const capacity ) { // Enter your implementation here. } void print_array( double const array[], std::size_t const capacity ) { std::cout << "{"; if ( capacity != 0 ) { std::cout << array[0]; for ( std::size_t k{ 1 }; k < capacity; ++k ) { std::cout << ", " << array[k]; } } std::cout << "};" << std::endl; } // First, try reversing these two lists: // // 0 1 2 3 4 5 6 7 8 capacity 9 // // 0 1 2 3 4 5 6 7 8 9 capacity 10 // // What do you notice? // If the capacity is odd, we do not need to touch the // middle entry: // // 0 1 2 3 4 5 6 7 8 capacity 9 // 8 7 6 5 4 3 2 1 0 // ^ // // If the capacity is even, there is no middle entry: // // 0 1 2 3 4 5 6 7 8 9 capacity 10 // 9 8 7 6 5 4 3 2 1 0 // // Thus, we need to swap: // // - the first and last entries, // - the second and second-last entries, // - the third and third-last entries, // // and so on, until we reach the middle. // // If the capacity is 9, we swap entries 0 through 3 // with their corresponding entries after the middle. // // If the capacity is 10, we swap entries 0 through 4 // with their corresponding entries in the second half. // // Question: Do you want to use a structure such as // // if ( capacity%2 == 0 ) { // // Swap entries assuming the capacity is even. // } else { // // Swap entries assuming the capacity is odd. // } // // or can you think of a better approach? // If you use separate cases for even and odd capacities, // you will notice that most of the code is identical. // // Duplicate code is undesirable. If a mistake is made in // one copy, it may also occur in the other. Furthermore, // someone fixing one copy may not realize that the same // change must be made elsewhere. // // Instead, consider an array with capacity 9: // // 0 1 2 3 4 5 6 7 8 // 8 7 6 5 4 3 2 1 0 // ^ // // We need: // // for ( std::size_t k{ 0 }; k < 4; ++k ) { // // Swap array[k] and array[capacity - k - 1]. // } // // For an array with capacity 10: // // 0 1 2 3 4 5 6 7 8 9 // 9 8 7 6 5 4 3 2 1 0 // // we need: // // for ( std::size_t k{ 0 }; k < 5; ++k ) { // // Swap array[k] and array[capacity - k - 1]. // } // // With a capacity of 8: // // 0 1 2 3 4 5 6 7 // 7 6 5 4 3 2 1 0 // // we need: // // for ( std::size_t k{ 0 }; k < 4; ++k ) { // // Swap array[k] and array[capacity - k - 1]. // } // // Notice that the number of swaps is always // // capacity/2 // // because integer division discards any remainder. // Here is one implementation. We also write a helper // function that swaps two entries of an array. /* // Add to the function declarations: void swap_array_entries( double array[], std::size_t const m, std::size_t const n ); // Function definitions: void reverse_array( double array[], std::size_t const capacity ) { for ( std::size_t k{ 0 }; k < capacity/2; ++k ) { swap_array_entries( array, k, capacity - k - 1 ); } } void swap_array_entries( double array[], std::size_t const m, std::size_t const n ) { double const tmp{ array[m] }; array[m] = array[n]; array[n] = tmp; } */ // Here is another implementation. // // Rather than initially setting the second index to // // capacity - 1, // // let it initially be one past the last entry. This // also works when capacity is zero. /* void reverse_array( double array[], std::size_t const capacity ) { std::size_t first{ 0 }; std::size_t second{ capacity }; while ( first < second ) { --second; if ( first < second ) { double const tmp{ array[first] }; array[first] = array[second]; array[second] = tmp; ++first; } } } */