Problem Description

Design a class, Array, that encapsulates a fixed-size dynamic array of signed integers.

Write a program that creates an Array container of size 100 and fills it with random numbers in the range [1..999]. (Use std::rand() with std::srand(1).) When building the array, if the random number is evenly divisible by 3 or 5, store it as a negative number.

Within your pa12.cpp source code file, write a function for each of the following processes.

  • Print the array ten values to a line. Make sure the values are aligned in columns. Each column should have a width of 5.
  • Return the sum of all values in the array.
  • Return the address of the smallest value in the array.

Demonstrate each of the functions above and several of the Array class member functions in your main function.

Array Class Interface

///
/// @file Array.h (Array class interface)
/// @author
/// @date 2020-09-20
///
/// @note DO NOT MODIFY THIS FILE.
///

#ifndef ARRAY_H_
#define ARRAY_H_

#include < cstddef >

class Array {
public:
/// Default constructor. Constructs a new container with 'count' copies of
/// elements with value 'value'.
/// @param count The number of elements.
/// @param value Value to fill each element.
Array(std::size_t count, int value = 0);

/// Copy constructor. Constructs the Array with a copy of the
/// contents of 'other'.
/// @param other Array to copy.
Array(const Array& other);

/// Free the resources used by the container.
virtual ~Array();

/// Returns a reference to the element at specified position 'pos'.
/// @param pos Position of the element to return.
/// @return Reference to the requested element.
int& at(std::size_t pos);
const int& at(std::size_t pos) const;

/// Returns a reference to the first element in the container.
/// Calling front on an empty container is undefined.
/// @note For a container c, the expression 'c.front()' is equivalent
/// to '*c.begin()'.
/// @return Reference to the first element.
int& front();
const int& front() const;

/// Returns a reference to the last element in the container.
/// Calling back on an empty container is undefined.
/// @note For a container c, the expression return 'c.back()' is equivalent
/// to '{ auto tmp = c.end(); --tmp; return *tmp; }'
/// @return Reference to the last element.
int& back();
const int& back() const;

/// Returns a pointer to the first element of the container.
/// If the container is empty, the returned pointer will be equal to 'end()'
/// @return Pointer to the first element.
int* begin() { return m_list; }
const int* begin() const { return m_list; }

/// Returns a pointer to the element following the last element of the
/// container. This element acts as a placeholder; attempting to dereference
/// this pointer is undefined.
/// @return Pointer to the element following the last element.
int* end() { return m_list + size(); }
const int* end() const { return m_list + size(); }

/// Checks if the container has no elements, i.e., whether
/// 'begin() == end()'.
/// @return true if the container is empty, false otherwise.
bool empty() const;

/// Returns the number of elements in the container, i.e., the distance
/// between 'begin()' and 'end()'.
/// @return The number of elements in the container.
std::size_t size() const { return m_size; }

/// Assigns the given value 'value' to all elements in the container.
/// @param value The `value` to assign to the elements.
void fill(int value);

/// Exchanges the contents of this container with another.
/// @param other Container to exchange the contents with.
void swap(Array& other);

protected:
std::size_t m_size; ///< Number of elements allocated.
int* m_list; ///< Pointer to base of array.
};

// Non-Member Function(s)

/// Compares the contents of two arrays. Returns true if the contents of
/// lhs and rhs are equal, that is, whether each element in lhs compares
/// equal with the element in rhs at the same position.
/// Otherwise, returns false.
/// @param lhs Container to compare.
/// @param rhs Container to compare.
/// @return true if lhs and rhs compare equal, otherwise false.
bool equal(const Array& lhs, const Array& rhs);

#endif // ARRAY_H_
/* EOF */

Requirements

You must use the interface provided above. Do not modify any function signatures. Do not use any form of base+offset addressing within repetition structures (i.e., use pointers to traverse the array.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.