diff options
| author | Yana Blashchishina <[email protected]> | 2024-02-15 21:49:59 -0800 |
|---|---|---|
| committer | Yana Blashchishina <[email protected]> | 2024-02-15 21:49:59 -0800 |
| commit | 83a1254b57c55879ac5b5789296f31f940bd6018 (patch) | |
| tree | a9f72f24f0509db240e9d7aa298e678089a6bf83 | |
| parent | array.h & array.cpp (diff) | |
| download | in-class-exercise-12-yanablash-83a1254b57c55879ac5b5789296f31f940bd6018.tar.xz in-class-exercise-12-yanablash-83a1254b57c55879ac5b5789296f31f940bd6018.zip | |
| -rw-r--r-- | InClassExercise12/InClassExercise12/c_array.cpp | 20 | ||||
| -rw-r--r-- | InClassExercise12/InClassExercise12/main.cpp | 16 |
2 files changed, 33 insertions, 3 deletions
diff --git a/InClassExercise12/InClassExercise12/c_array.cpp b/InClassExercise12/InClassExercise12/c_array.cpp index e068dd5..777f6ea 100644 --- a/InClassExercise12/InClassExercise12/c_array.cpp +++ b/InClassExercise12/InClassExercise12/c_array.cpp @@ -5,13 +5,29 @@ void DoubleArraySize(int*& array, size_t& size) { + size_t newSize = size * 2; + int* newArray = new int[newSize]; + for (size_t i = 0; i < size; ++i) { + newArray[i] = array[i]; + } + + for (size_t i = size; i < newSize; ++i) { + newArray[i] = 0; + } + + delete[] array; + + array = newArray; + size = newSize; } void PrintArray(int* array, size_t size) { - - + for (size_t i = 0; i < size; ++i) { + std::cout << array[i] << " "; + } + std::cout << std::endl; }
\ No newline at end of file diff --git a/InClassExercise12/InClassExercise12/main.cpp b/InClassExercise12/InClassExercise12/main.cpp index 9792b80..36d2033 100644 --- a/InClassExercise12/InClassExercise12/main.cpp +++ b/InClassExercise12/InClassExercise12/main.cpp @@ -4,15 +4,29 @@ // Assignment: In Class Exercise 12 #include <iostream> -#include <ostream> +#include "c_array.h" +using std::cout; +using std::endl; int main() { + const size_t initialSize = 101; + int* myArray = new int[initialSize]; + for (size_t i = 0; i < initialSize; ++i) { + myArray[i] = i + 1; + } + cout << " Initial Array: " << endl; + PrintArray(myArray, initialSize); + DoubleArraySize(myArray, initialSize); + cout << " Array after being doubled in size: " << endl; + PrintArray(myArray, initialSize * 2); + + delete[]myArray; return 0; |