Website Summary Files Commits Branches @commit @diff @stats
path: root / Week04 / Exercise02
diff options
context:
space:
mode:
Commite5c06fbc864205a9736f07f9237ace80617ca471 (patch)
Author
 Syndamia <kamen@syndamia.com>
2026-03-16 22:41:31 +0200
Committer
 Syndamia <kamen@syndamia.com>
2026-03-16 22:41:31 +0200
Filesf916efca600b104f3489a8501bffa448f11dfe79 /Week04/Exercise02
oop-2025-solutions-e5c06fbc864205a9736f07f9237ace80617ca471.tar
oop-2025-solutions-e5c06fbc864205a9736f07f9237ace80617ca471.tar.gz
oop-2025-solutions-e5c06fbc864205a9736f07f9237ace80617ca471.zip
Parent5624cd9b63bec13fc464a3d7820d703abd8de113 (diff)
feat(Week04): Add solutions
2 files changed, 71 insertions, 0 deletions
-rw-r--r--Week04/Exercise02/IntArray.cpp27
-rw-r--r--Week04/Exercise02/IntArray.h44
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];
+}
diff --git a/Week04/Exercise02/IntArray.h b/Week04/Exercise02/IntArray.h
new file mode 100644
index 0000000..0ed99c9
--- /dev/null
+++ b/Week04/Exercise02/IntArray.h
@@ -0,0 +1,44 @@
+#ifndef _INTARRAY
+#define _INTARRAY
+
+class IntArray {
+ int* elems;
+ unsigned capacity;
+
+ /* Как трябва да изглежда голяма четворка: */
+
+ void free() {
+ delete[] elems;
+ }
+ void copyFrom(const IntArray& other) {
+ capacity = other.capacity;
+
+ elems = new int[capacity];
+ for (unsigned i = 0; i < capacity; ++i)
+ elems[i] = other.elems[i];
+ }
+public:
+ IntArray() {
+ elems = nullptr;
+ capacity = 0;
+ }
+ ~IntArray() {
+ free();
+ }
+ IntArray(const IntArray& other) {
+ copyFrom(other);
+ }
+ IntArray& operator=(const IntArray& other) {
+ if (this != &other) {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+ }
+
+ int At(unsigned index);
+ void Resize(unsigned capacity);
+ void Concat(const IntArray& endElements);
+};
+
+#endif /* _INTARRAY */