From 0f4cef06fef4bb2368185a2ffff949e4e8a3987f Mon Sep 17 00:00:00 2001 From: Ben Morgan Date: Wed, 16 Sep 2020 21:14:24 -0600 Subject: [PATCH] day3 code --- main.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 main.cpp diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..fbafdbe --- /dev/null +++ b/main.cpp @@ -0,0 +1,45 @@ +#include +#include + +int main() { + int *p = new int[1]; + int len = 1; + + std::vector v; + int num; + + std::cout << "Please enter a number: "; + std::cin >> num; + + v.push_back(num); //vector append + p[0] = num; + + while (num != 0) { + std::cout << "Please enter a number: "; + std::cin >> num; + + v.push_back(num); //vector append + + // realloc + int* temp = new int[len + 1]; //create a new temporary array to "transport values" + //std::copy(p, p + len, temp); + for (int i = 0; i < len; i++) temp[i] = p[i]; //copy contents of current array to temp arr + temp[len] = num; //add new number to end of temp array + delete [] p; //delete (free the memory) array p in preparation for resize + p = temp; //set old array to the temp array + len++; + } + + std::cout << "Vector: " << std::endl; + for (int i : v) { + std::cout << i << " " << std::endl; + } + + std::cout << std::endl << "Array: " << std::endl; + for (int i = 0; i < len; i++) { + std::cout << p[i] << " " << std::endl; + } + + delete [] p; + return 0; +}