#include #include #include /*************************************************** * Checking count, first, and last * * Write a function that independently checks the * results returned by count(...), first(...), and * last(...). * * Use assertions to verify that the returned values * agree with the contents of the array. ***************************************************/ // Function declarations void check( int const n, int const array[], std::size_t const capacity ); std::size_t count( int const n, int const array[], std::size_t const capacity ); std::size_t first( int const n, int const array[], std::size_t const capacity ); std::size_t last( int const n, int const array[], std::size_t const capacity ); int main(); // Function definitions int main() { int array0[20]{ 6, 8, 4, 11, 4, 5, 3, 5, 4, 8, 9, 11, 8, 0, 3, 6, 8, 8, 7, 4 }; for ( int n{ 0 }; n <= 12; ++n ) { check( n, array0, 20 ); } return 0; } /*************************************************** * check * * Independently check that the values returned by * count(...), first(...), and last(...) agree with * the contents of the array. ***************************************************/ void check( int const n, int const array[], std::size_t const capacity ) { // Your implementation here } std::size_t count( int const n, int const array[], std::size_t const capacity ) { std::size_t n_count{ 0 }; for ( std::size_t k{ 0 }; k < capacity; ++k ) { if ( array[k] == n ) { ++n_count; } } return n_count; } std::size_t first( 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 k; } } return capacity; } std::size_t last( int const n, int const array[], std::size_t const capacity ) { std::size_t index{ capacity }; for ( std::size_t k{ 0 }; k < capacity; ++k ) { if ( array[k] == n ) { index = k; } } return index; } // Hints: // // First, call count(...), first(...), and last(...) // and store the three values they return. // // // If the count is zero: // // - first(...) should return capacity; // - last(...) should return capacity; and // - every entry of the array should differ from n. // // // If the count is not zero: // // - first(...) and last(...) must return valid // indices; // // - the entry at the first index must equal n; // // - the entry at the last index must equal n; // // - exactly count(...) entries from the first // through the last index must equal n; // // - every entry before the first must differ // from n; and // // - every entry after the last must differ // from n. // // Use assertions to check each of these properties. // One solution: /* void check( int const n, int const array[], std::size_t const capacity ) { std::size_t const n_count{ count( n, array, capacity ) }; std::size_t const n_first{ first( n, array, capacity ) }; std::size_t const n_last{ last( n, array, capacity ) }; if ( n_count == 0 ) { assert( n_first == capacity ); assert( n_last == capacity ); for ( std::size_t k{ 0 }; k < capacity; ++k ) { assert( array[k] != n ); } } else { // Before using n_first and n_last as indices, // ensure that they are valid. assert( n_first < capacity ); assert( n_last < capacity ); assert( n_first <= n_last ); assert( array[n_first] == n ); assert( array[n_last] == n ); // Check that there are exactly n_count // occurrences of n from n_first through // n_last. std::size_t check_count{ 0 }; for ( std::size_t k{ n_first }; k <= n_last; ++k ) { if ( array[k] == n ) { ++check_count; } } assert( check_count == n_count ); // No entry before n_first may equal n. for ( std::size_t k{ 0 }; k < n_first; ++k ) { assert( array[k] != n ); } // No entry after n_last may equal n. for ( std::size_t k{ n_last + 1 }; k < capacity; ++k ) { assert( array[k] != n ); } } } */