From f5533654fa8d51786c665b0668b958e14d2c4b1b Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Mon, 25 Sep 2023 16:13:50 -0400 Subject: [PATCH] adding lab 5 --- labs/05_vectors/README.md | 123 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 labs/05_vectors/README.md diff --git a/labs/05_vectors/README.md b/labs/05_vectors/README.md new file mode 100644 index 0000000..297a6bf --- /dev/null +++ b/labs/05_vectors/README.md @@ -0,0 +1,123 @@ +# Lab 5 — Vec Implementation + +## Checkpoint 1 +*estimate: 20 minutes* + +- Team up with one student in your lab section. MEET SOMEONE NEW! You may not work +with someone who was on your team for Lab 4. Ask a TA or mentor to help you find a partner. +If the number of students in the room is not even, the graduate TA will approve a single team with 3 +members. +- Introduce yourself to your teammate. Ask them to share something about themselves (e.g. hobbies, +sports, favorite music, etc.) Learn something new about your teammate (even if you already know +them). + +For each function below, assign different letters to each of the data sizes that at first glance might have impact +on the running time of the function. Be sure to consider integer value, size of vector, and length of string. +Then give the big O notation of the function in terms of those variables. + +```cpp +int foobar (const std::vector &a, int b) { + int answer = 0; + for (int i = 0; i < a.size(); i+=b) { + answer++; + } + return answer; +} +``` + +```cpp +void foo2 (const std::vector &a, std::string &b) { + b.clear(); + for (int i = 0; i < a.size(); i++) + { + if (a[i] > 0) + b.push_back('+'); + else + b.push_back('-'); + } +} +``` + +```cpp +std::vector foo3 (const std::vector &a, const std::string &b) { + return std::vector(b.size(),a.size()); +} +``` + +```cpp +int foo3 (const std::vector &a, const std::string& b) { + int ret = 0; + for (int i=0; i foo4 (const std::vector &a) { + std::vector answer = a; + for (int i = 0; i < a.size(); i++) { + if(a[i] < (a[a.size()-1]*a[a.size()-1])){ + answer.erase(answer.end()-1); + } + } + return answer; +} +``` + +```cpp +std::vector foo5 (const std::vector &a, int b) { + std::vector ret; + for(int i=0; i and an element of type T, and returns the number of elements that matched the +argument and were successfully removed from the vector. The order of the other elements should stay +the same. For example, if v, a Vec object contains 6 elements: 11 22 33 11 55 22 and you call +remove_matching_elements(v,11), that call should return 2, and v should now contain: 22 33 55 22. +You should not create a new vector in your function. +Add several test cases to test_vec.cpp to show that the function works as expected. What is the order +notation of your solution in terms of n the size of the vector, and e the number of occurences of the input +element in the vector? + +**To complete this checkpoint**, show a TA your debugged solution for remove_matching_elements and +be prepared to discuss the order notation of the function. + +## Checkpoint 3 +*estimate: TBD* + +Add a print member function to Vec to aid in debugging. (Note, neither remove_matching_elements nor +print are not part of the STL standard for vector). You should print the current information stored in the +variables m_alloc, m_size, and m_data. Use the print function to confirm your remove_matching_elements +function is debugged. Also, write a test case that calls push_back many, many times (hint, use a for loop!) +and observe how infrequently re-allocation of the m_data array is necessary. +To verify your code does not contain memory errors or memory leaks, use Valgrind and/or Dr. Memory on +your local machine – see instructions on the course webpage: Memory Debugging. Also, submit your code +to the homework server (in the practice space for lab 4), which is configured to run the memory debuggers +for this exercise. To verify that you understand the output from Valgrind and/or Dr. Memory, temporarily +add a simple bug into your implementation to cause a memory error or memory leak. + +**To complete this checkpoint**, show a TA your tested & debugged program. Be prepared to demo and +discuss the Valgrind and/or Dr. Memory output: with and without memory errors and memory leaks AND +on your local machine and on the homework server.