ProblemsModern C++Array algebra
IntermediateModern C++

Array algebra

Context

A container exposes its editing operations through operators, each with a fixed meaning: append, prepend, repeat, remove. The catch is the int on the LEFT side — 0 + a cannot be a member function, so half the operators must live outside the class.

Task

Implement Array with operator algebra: a + 1 appends, 0 + a prepends, 2 * a repeats the content twice, a - 1 removes every occurrence of 1. operator[] reads elements for the checks.

Constraints

  • a + x appends x; x + a prepends x
  • n * a repeats the current content n times (2 * a doubles it)
  • a - x removes ALL occurrences of x, keeping the order of the rest
  • operator reads elements; copies are independent

Before you code

  • Why must x + a (int on the left) be a free function?
  • What does 0 * a naturally produce under the repeat rule?
  • Why return a new Array from each operator instead of mutating?

Tests

  • #1The full operator sequence
  • #2Removal keeps order; copies are independent

Hints

Hint 1

A member operator+ only covers a + x. For x + a write a free function (friend) taking (int, const Array&).

Hint 2

Build each result in a fresh Array — the checks reassign a = ... every line, so value semantics keep it simple.

Editorarray-algebra.cpp
Results

Hit Submit (or ⌘/Ctrl + ↵) — test results will show up here.