Website Summary Files Commits Branches @commit @diff @stats
path: root / Week04 / Exercise02 / IntArray.cpp
diff options
context:
space:
mode:
1 files changed, 27 insertions, 0 deletions
-rw-r--r--Week04/Exercise02/IntArray.cpp27
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];
+}