Skip to the content of the web site.

Project W.1: Zero-entry queries

1. Problem

Given an array, some generic questions that may be asked are:

  1. are all the entries zero,
  2. are all the entries non-zero,
  3. is there at least one entry that is zero, or
  4. is there at least one entry that is non-zero?

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:

  1. one that searches the entries of an array from $m$ to $n - 1$, and
  2. one that searches all entries of an array of a given capacity.

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:

  1. The first requires arrays and loops, and
  2. the second requires arrays, loops, and templates.

2. Integer implementation

Implement the above functions for an array of type int.

2.1 Required background

  • loops
  • arrays

2.2 Function prototypes

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 );

3. Templated implementation

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{}.

3.1 Required background

  • loops
  • arrays
  • templates

3.2 Function prototypes

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 );