#include #include /*************************************************** * Copying an array * * Write a function that copies entries from one * array into another. * * The function is given: * * - the source array; * - the number of entries to copy; and * - the destination array. * * You may assume that both arrays contain at least * capacity entries. ***************************************************/ // Function declarations void copy_array( int const array1[], std::size_t const capacity, int array2[] ); void print_array( int const array[], std::size_t const capacity ); int main(); // Function definitions int main() { int array1[4]{ 9, -1, 4, -4 }; int array2[4]{}; int array3[10]{ -3, -2, -9, 1, -1, -8, 3, 0, 2, -2 }; int array4[12]{}; print_array( array2, 4 ); copy_array( array1, 4, array2 ); print_array( array2, 4 ); std::cout << std::endl; print_array( array4, 12 ); copy_array( array3, 10, array4 ); print_array( array4, 12 ); return 0; } /*************************************************** * copy_array * * Copy capacity entries from array1 into array2. * * The entries of array1 are not modified, whereas * the corresponding entries of array2 are. ***************************************************/ void copy_array( int const array1[], std::size_t const capacity, int array2[] ) { // 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 will need a for loop that steps through the // first capacity entries of both arrays. // // For each index k, assign the entry in array1 to // the corresponding entry in array2: // // array2[k] = array1[k]; // // Notice that array1 is declared const because the // function only reads its entries. // // array2 cannot be const because the purpose of the // function is to modify its entries. // // What happens if capacity is zero? // One solution: /* void copy_array( int const array1[], std::size_t const capacity, int array2[] ) { for ( std::size_t k{ 0 }; k < capacity; ++k ) { array2[k] = array1[k]; } } */