From e5c06fbc864205a9736f07f9237ace80617ca471 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Mon, 16 Mar 2026 22:41:31 +0200 Subject: feat(Week04): Add solutions --- Week04/Exercise02/IntArray.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Week04/Exercise02/IntArray.cpp (limited to 'Week04/Exercise02/IntArray.cpp') diff --git a/Week04/Exercise02/IntArray.cpp b/Week04/Exercise02/IntArray.cpp new file mode 100644 index 0000000..2dc0521 --- /dev/null +++ b/Week04/Exercise02/IntArray.cpp @@ -0,0 +1,27 @@ +#include "IntArray.h" + +int IntArray::At(unsigned index) { + if (index < capacity) + return elems[index]; + return 0; +} + +void IntArray::Resize(unsigned capacity) { + int* newElems = new int[capacity]; + + unsigned copyCount = this->capacity < capacity ? this->capacity : capacity; + for (unsigned i = 0; i < copyCount; ++i) + newElems[i] = elems[i]; + + delete[] elems; + elems = newElems; + this->capacity = capacity; +} + +void IntArray::Concat(const IntArray& endElements) { + unsigned oldCapacity = capacity; + Resize(capacity + endElements.capacity); + + for (unsigned i = oldCapacity; i < capacity; ++i) + elems[i] = endElements.elems[i - oldCapacity]; +} -- cgit v1.3.1-23-gd82d