From ac9e104567f6989d9959d9cf16019adb926a59c2 Mon Sep 17 00:00:00 2001 From: Connor McDowell Date: Thu, 15 Feb 2024 16:52:41 -0800 Subject: print and double created --- Project1/c_array.cpp | 25 +++++++++++++++++++++++++ Project1/c_array.h | 2 ++ Project1/main.cpp | 7 +++++++ 3 files changed, 34 insertions(+) (limited to 'Project1') diff --git a/Project1/c_array.cpp b/Project1/c_array.cpp index fd64a39..f504839 100644 --- a/Project1/c_array.cpp +++ b/Project1/c_array.cpp @@ -7,3 +7,28 @@ using std::cout; using std::endl; +void DoubleArraySize(int*& array, size_t size) +{ + int* newArray = nullptr; + + newArray = new int[size * 2]; + + for (auto i = 0u; i < size; ++i) + { + newArray[i] = array[i]; + } + delete[] array; + + array = newArray; + + size *= 2; +} + +void PrintArray(int* array, size_t size) +{ + for (auto i = 0u; i < size; i++) + { + cout << "array[" << i << "] = " << array[i] << endl; + } + cout << endl; +} \ No newline at end of file diff --git a/Project1/c_array.h b/Project1/c_array.h index 95a13d4..e4cca9f 100644 --- a/Project1/c_array.h +++ b/Project1/c_array.h @@ -1,7 +1,9 @@ #ifndef C_ARRAY_HEADER #define C_ARRAY_HEADER +void DoubleArraySize(int*& array, size_t size); +void PrintArray(int* array, size_t size); #endif C_ARRAY_HEADER diff --git a/Project1/main.cpp b/Project1/main.cpp index dab64a4..5dd3c7c 100644 --- a/Project1/main.cpp +++ b/Project1/main.cpp @@ -13,8 +13,15 @@ using std::endl; int main() { + constexpr size_t SIZE = 101; + int* array = new int[SIZE]; + PrintArray(array, SIZE); + DoubleArraySize(array, SIZE); + PrintArray(array, SIZE); + + delete[] array; return 0; } \ No newline at end of file -- cgit v1.2.3