#include #include /*************************************************** * Determining if a value is in an array * * Write a function that determines whether a given * integer appears as an entry in an array. * * If the integer is found, return true; otherwise, * return false. ***************************************************/ // Function declarations bool is_in_array( int const n, int const array[], std::size_t const capacity ); int main(); // Function definitions int main() { int array1[8]{ 34, 15, 65, 59, 69, 42, 40, 80 }; int array2[27]{ 50, 65, 23, 46, 57, 3, 29, 22, 99, 73, 44, 2, 48, 88, 98, 38, 17, 46, 19, 34, 93, 88, 46, 52, 69, 38, 79 }; std::cout << is_in_array( 42, array1, 8 ) << std::endl; std::cout << is_in_array( 43, array1, 8 ) << std::endl; std::cout << is_in_array( 43, array2, 27 ) << std::endl; std::cout << is_in_array( 44, array2, 27 ) << std::endl; return 0; } /*************************************************** * is_in_array * * Return true if n appears as an entry in the * array, and return false otherwise. ***************************************************/ bool is_in_array( int const n, int const array[], std::size_t const capacity ) { return false; } // Hint: // // You will need a for loop that steps through the // array. Because capacity has type std::size_t, the // loop variable should also have type std::size_t. // // As soon as you find an entry equal to n, you know // the answer and can immediately return true. // // If the loop finishes without finding n, you then // know that n does not appear in the array, so you // can safely return false. // // Look below for a solution after you have tried // this yourself. // One solution: /* bool is_in_array( int const n, int const array[], std::size_t const capacity ) { for ( std::size_t k{ 0 }; k < capacity; ++k ) { if ( array[k] == n ) { return true; } } return false; } */