Given an array, some generic questions that may be asked are:
Rather than authoring such a query each time it is needed, it is easier to simply author the function once and include it in a library of array tools.
In each case, we will be implementing two functions:
The function prototype of each will be
bool query( int const array[], std::size_t m, std::size_t n ); bool query( int const array[], std::size_t capacity );
The second function will always call the first function with the second and third arguments being 0 and capacity.
As none of these functions should modify the entries of the array, the arrays are passed as constant arrays.
There are two implementations:
Implement the above functions for an array of type int.
bool all_zero( int const array[], std::size_t m, std:size_t n ); bool all_zero( int const array[], std::size_t capacity ); bool all_non_zero( int const array[], std::size_t m, std:size_t n ); bool all_non_zero( int const array[], std::size_t capacity ); bool has_zero( int const array[], std::size_t m, std:size_t n ); bool has_zero( int const array[], std::size_t capacity ); bool has_non_zero( int const array[], std::size_t m, std:size_t n ); bool has_non_zero( int const array[], std::size_t capacity );
Implement the above functions for a templated array For templated implementations, you could compare to 0, but it is better to compare to the default value of the templated type: T{}.
template <typename T> bool all_zero( T const array[], std::size_t m, std::size_t n ); template <typename T> bool all_zero( T const array[], std::size_t capacity ); template <typename T> bool all_non_zero( T const array[], std::size_t m, std::size_t n ); template <typename T> bool all_non_zero( T const array[], std::size_t capacity ); template <typename T> bool has_zero( T const array[], std::size_t m, std::size_t n ); template <typename T> bool has_zero( T const array[], std::size_t capacity ); template <typename T> bool has_non_zero( T const array[], std::size_t m, std::size_t n ); template <typename T> bool has_non_zero( T const array[], std::size_t capacity );