#include #include /*************************************************** * Copying part of an array * * This is a more general version of the previous * copy_array(...) function. * * Copy the entries of array1 beginning at index * begin1 and ending just before index end1 into * array2 beginning at index begin2. * * Thus, the entries * * array1[begin1], * array1[begin1 + 1], * ... * array1[end1 - 1] * * are copied into * * array2[begin2], * array2[begin2 + 1], * ... * * You may assume that all indices used are valid * for the corresponding arrays. ***************************************************/ // Function declarations void copy_array( int const array1[], std::size_t const begin1, std::size_t const end1, int array2[], std::size_t const begin2 = 0 ); void print_array( int const array[], std::size_t const capacity ); int main(); // Function definitions int main() { int array1[4]{ 10, 11, 12, 13 }; int array2[8]{}; int array3[10]{ 20, 21, 22, 23, 24, 25, 26, 27, 28, 29 }; int array4[14]{}; print_array( array2, 8 ); copy_array( array1, 1, 4, array2, 3 ); print_array( array2, 8 ); std::cout << std::endl; print_array( array4, 14 ); copy_array( array3, 2, 9, array4, 6 ); print_array( array4, 14 ); return 0; } /*************************************************** * copy_array * * Copy the entries of array1 having indices * * begin1, ..., end1 - 1 * * into array2 beginning at index begin2. ***************************************************/ void copy_array( int const array1[], std::size_t const begin1, std::size_t const end1, int array2[], std::size_t const begin2 ) { // Enter your implementation here } /*************************************************** * print_array * * Print the entries of the array separated by * commas and spaces. ***************************************************/ void print_array( int const array[], std::size_t const capacity ) { if ( capacity > 0 ) { std::cout << array[0]; for ( std::size_t k{ 1 }; k < capacity; ++k ) { std::cout << ", " << array[k]; } } std::cout << std::endl; } // Hint: // // You must copy // // array1[begin1] into array2[begin2], // array1[begin1 + 1] into array2[begin2 + 1], // array1[begin1 + 2] into array2[begin2 + 2], // // and so on, stopping before index end1 in array1. // // You can therefore use two indices that are // incremented together. // // // Suppose you wanted to implement the previous // copy_array(...) function by calling this function: // // void copy_array( // int const array1[], // std::size_t const capacity, // int array2[] // ); // // What values should you use for begin1, end1, and // begin2? // // // Finally, notice that begin2 has a default value // of 0. Therefore, if you want the copied entries // to begin at index 0 of array2, you may omit the // final argument. // One solution: /* void copy_array( int const array1[], std::size_t const begin1, std::size_t const end1, int array2[], std::size_t const begin2 ) { std::size_t source{ begin1 }; std::size_t destination{ begin2 }; for ( ; source < end1; ++source, ++destination ) { array2[destination] = array1[source]; } } // The previous copy_array(...) function could now // be implemented as follows: void copy_array( int const array1[], std::size_t const capacity, int array2[] ) { copy_array( array1, 0, capacity, array2 ); } */