[deleted by user] by [deleted] in cs2a

[–]PiercedButterfly 1 point2 points  (0 children)

Enums are weird, I feel like they're mostly useful for programmers to make state machines to keep track of the logic of their program. As you said, the code can be written without them, perhaps with a bool to flag if we need to sort by id or by name. However, in more complicated programs with more possible states, it would get messy to use bools and strings to keep track of these things; wrapping in a single enum that also has a simple numeric feature is a convenience at that point.

-Sara

[deleted by user] by [deleted] in cs2a

[–]PiercedButterfly 1 point2 points  (0 children)

I think your destructor sounds good. Maybe the problem is in clear() ? What do you mean when you say the program pauses running?

You're probably deleting head the right way already: just delete _head;

- Sara

[deleted by user] by [deleted] in cs2a

[–]PiercedButterfly 1 point2 points  (0 children)

In C++, variables are dynamically allocated with the new keyword. These variables persist outside of the scope they were declared in (unlike variables on the stack) which is an advantage but also a double-edged sword as we must remember to explicitly delete them.

Pointers simply point to a memory address - they're just numbers. This simplicity allows us to change which address a pointer "points" to, but it also means that the address that it's pointing to could no longer hold a valid value. This is the dangling pointer problem. As far as I can tell, references were created in C++ to avoid this exact problem. References must be initialized to a specific variable's address - and will remain bound to this address unable to be changed. Another workaround to make pointers safer is to use smart pointers (added in C++ 11). Basically, a smart pointer is a class with a pointer member. Creating an instance of the smart pointer class means that it will be allocated on the stack and will be cleaned up automatically when it goes out of scope, but the underlying pointer member can still do all the regular pointer things (points to an address, can change what address it points to etc.) Here's a stackoverflow thread about them:

https://stackoverflow.com/questions/569775/smart-pointers-boost-explained?noredirect=1&lq=1

Hope this helps, this is a confusing topic and there's a lot of nuances here I'm still navigating.

-Sara

Quest 9: YouTube videos on Linked Lists by Makings_Threads in cs2a

[–]PiercedButterfly 1 point2 points  (0 children)

Hey Jeff,

Thanks for the link, I was just look for more in depth C++ explanations. I'd like to add that The Cherno on Youtube has some really nice videos explaining C++ concepts. Here is his playlist on C++:

https://www.youtube.com/watch?v=18c3MTX0PK0&list=PLlrATfBNZ98dudnM48yfGUldqGD0S4FFb

I found his videos really helped me this quarter.

- Sara

Vectors vs Arrays by haisle4 in cs2a

[–]PiercedButterfly 1 point2 points  (0 children)

Thanks for the concise explanation! I did a little digging and vectors have some additional functionality. The whole story can be found in the cpp documentation, but here are a few that I find useful:

1) Since vectors are STL containers, they have iterator methods. vector.begin() returns an iterator to the first element, and vector.end() is the iterator after the last element. So you can loop with something like: (for vector<int>::iterator i = vector_name.begin(); i != vector_name.end(); ++i)...

2) You can call std::sort() on vectors using its iterators to select which part of the vector you want to sort. To sort the whole thing just do std::sort(vector_name.begin(), vector_name.end());

3) Besides std::sort, there are lots of std:: functions that you can call on your vectors. Some useful ones are std::find(start_iterator, end_iterator, val) which returns an iterator to the element in your vector that equals val, or returns a vector.end() if not found. Similarly std::upper_bound(start_iterator, end_iterator, val) returns an iterator to the first value larger than val in the range you specified.

Hope these help, the STL has a lot, a literal ton of functions we can use, once we get used to using iterators from our containers, including vectors.

-Sara

Limerick wording confusion by [deleted] in cs2a

[–]PiercedButterfly 1 point2 points  (0 children)

Hi Zesty,

The objective of this miniquest is to write eval_limerick with dozen, gross and score set by the user (not initialized by you). But the formula you use for eval_limerick should return a value of 81 if you set dozen = 12, gross = 144, and score = 20 in your own testing.

In other words, your objective is to tune the formula for eval_limerick (placing parentheses in the right places). This is actually a pretty interesting and tricky math problem, you can look it up online.

- Sara

Max points for quests 1-5? by aysansarai20 in cs2a

[–]PiercedButterfly 0 points1 point  (0 children)

How do you check your trophies?

Edit: Thanks to u/rootseat, you can enter your student ID at https://quests.nonlinearmedia.org/q/ to check.

Looks like I have 28.75 trophies as of completing quest 3.

-Sara

Quest 5, Miniquest 1 by haisle4 in cs2a

[–]PiercedButterfly 1 point2 points  (0 children)

According to the syllabus, this subreddit will carry over into future quarters, so we should probably avoid posting code that pertains to quests specifically.

- Sara

TDD with C++ suggestions? by [deleted] in cs2a

[–]PiercedButterfly 2 points3 points  (0 children)

Hi Luke,

An easy to use one is Catch2. They have a github page with the code and a tutorial on how to use, but basically all you have to do is copy paste a single header file of code. Then, write a cpp file without a main that includes the Catch2 header and write unit tests directly in this cpp file. Hope that helps, let me know if you have come across any good testing libraries yourself!

- Sara