#include #include #include /*************************************************** * Inputting array entries * * Write a function that asks the user to enter each * of the entries of an array. * * The function should step through the array and * use std::cin to assign a value to each entry. * * After all the entries have been entered, the * program prints the array. ***************************************************/ // Function declarations void input_array( int array[], std::size_t const capacity ); void print_array( int const array[], std::size_t const capacity ); std::string ordinal( std::size_t const k ); int main(); // Function definitions int main() { int array0[25]{}; input_array( array0, 25 ); print_array( array0, 25 ); return 0; } /*************************************************** * input_array * * Ask the user to enter each of the entries of the * array. ***************************************************/ void input_array( int array[], std::size_t const capacity ) { // 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 all // the entries of the array. // // For each entry, print a message prompting the user // to enter a value and then use std::cin to assign // that value to the appropriate array entry. // // It is probably also a good idea to tell the user // how many entries are expected. // Here is one solution that is way over the top, // but enjoy. /* void input_array( int array[], std::size_t const capacity ) { for ( std::size_t k{ 0 }; k < capacity; ++k ) { std::cout << "Input the " << ordinal( k + 1 ) << " entry of " << capacity << ": "; std::cin >> array[k]; } } */ // The following function is not necessary for the // exercise. It is only here to make the prompts a // little more interesting. // // It converts a positive integer to a string // representing its ordinal: // // 1 -> 1st // 2 -> 2nd // 3 -> 3rd // 4 -> 4th // 11 -> 11th // 12 -> 12th // 13 -> 13th // 21 -> 21st // 22 -> 22nd // 23 -> 23rd // 111 -> 111th // 121 -> 121st std::string ordinal( std::size_t const k ) { std::size_t const last_two{ k % 100 }; std::size_t const last_digit{ k % 10 }; if ( (last_two >= 11) && (last_two <= 13) ) { return std::to_string( k ) + "th"; } else if ( last_digit == 1 ) { return std::to_string( k ) + "st"; } else if ( last_digit == 2 ) { return std::to_string( k ) + "nd"; } else if ( last_digit == 3 ) { return std::to_string( k ) + "rd"; } else { return std::to_string( k ) + "th"; } }