From 83a1254b57c55879ac5b5789296f31f940bd6018 Mon Sep 17 00:00:00 2001 From: Yana Blashchishina Date: Thu, 15 Feb 2024 21:49:59 -0800 Subject: one error that I can't fix --- InClassExercise12/InClassExercise12/c_array.cpp | 20 ++++++++++++++++++-- InClassExercise12/InClassExercise12/main.cpp | 16 +++++++++++++++- 2 files changed, 33 insertions(+), 3 deletions(-) (limited to 'InClassExercise12') 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 -#include +#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; -- cgit v1.2.3