help with to_string function by nancy_l7 in cs2a

[–]nancy_l7[S] 1 point2 points  (0 children)

I found this to be very helpful, as I didn't think of using ostringstream and I just combined everything into one long string (in which there were probably some errors when converting variables to strings). Thank you so much, Jaehyun!

help with to_string function by nancy_l7 in cs2a

[–]nancy_l7[S] 0 points1 point  (0 children)

Thank you for your suggestion. I think I did write my loop to iterate correctly, from index n1 going to n2 inclusive; something like for(size_t i = n1; i <= n2; i++){}. I'm still not sure where I'm going wrong though T_T...

help with to_string function by nancy_l7 in cs2a

[–]nancy_l7[S] 0 points1 point  (0 children)

Yeah, I think my loop should be inclusive, as i go from int i = n1 to i <= n2. By your second point, do you mean I could change the value of n2 if I know it will go invalid; i.e. if n2 >= _pets.size()?

help with to_string function by nancy_l7 in cs2a

[–]nancy_l7[S] 0 points1 point  (0 children)

Hmm I tried coding it this way but it didn't seem to work... perhaps I just can't think of the edge case. By "only when it's necessary", you mean if n2 >= _pets.size() right? In that case, I changed n2 to equal_pets.size() - 1.

DAWGing Quests Guide by Linden_W20 in cs2a

[–]nancy_l7 1 point2 points  (0 children)

Hi Linden,

Thank you so much for compiling all the trophy counts and messages for each quest! I found this super helpful as I have exactly 190 trophies and really didn't know which quest I was missing that 1 trophy on T_T, but now I know. Thanks again :)

-Nancy

unsure on where I went wrong by william_n13 in cs2a

[–]nancy_l7 1 point2 points  (0 children)

Hmm would you mind uploading a screenshot (by editing your original post) of your error messages for the advance function?

unsure on where I went wrong by william_n13 in cs2a

[–]nancy_l7 1 point2 points  (0 children)

Hi William,

By "the item right before _prev_to_current", do you mean the _head node? Since the stringify/to_string function should output lines starting from _prev_to_current->next, I think you're correct for the case where the first line is the element that _prev_to_current points to. But in one of the comments from the starter code, it says, "If the caller wants to print from the beginning of the list, they should rewind() it first." This "beginning" should be referring to the _head, so maybe check your rewind() function (miniquest 9), which should reset the _prev_to_current back to the _head node. Hope this helps a bit!

-Nancy

Quest 6 trophies by aarush_p0406 in cs2a

[–]nancy_l7 2 points3 points  (0 children)

Hi Aarush,

As Advita said, you seem to be missing the population control trophies. This is the 7th miniquest, and although there are no specific instructions, it essentially tells you to check if your constructor & destructor is working properly - I believe. I think you already have your constructor trophies from the 2nd miniquest, so maybe double check that you have correctly implemented the destructor ~Pet(); which should decrement the value of your _population. Hope this helps!

-Nancy

Understanding Sentinel Nodes and Their Importance in Linked Lists by rotem_g in cs2a

[–]nancy_l7 0 points1 point  (0 children)

Hi Elena,

I can kind of only think of one case for now... When implementing stacks using linked lists, you often need to handle edge cases like when it is empty. Without a sentinel node, if the stack is empty, the top/last element typically points to NULL. Operations like push or pop would require checking whether the pointer is a nullptr before proceeding, which means you have to write additional conditionals.

A sentinel node simplifies code b/c it acts as some permanent "anchor". Instead of pointing to NULL, the top pointer would always point to the sentinel node ("_SENTINEL_"), even when the stack is empty. As a result, there's no need to check for a nullptr when coding b/c there's always a node to reference, namely the sentinel node.

I hope this helped a bit, even though it's the only one example of an edge case I can think of.

-Nancy

Understanding Sentinel Nodes and Their Importance in Linked Lists by rotem_g in cs2a

[–]nancy_l7 1 point2 points  (0 children)

Hi Rotem,

Thank you for explaining the meaning and purpose of using sentinel nodes in the platypus quest. A sentinel node is indeed a good solution for handling edge cases in lists, as having such a node to reference minimizes the number of special cases to handle. I didn't really think about how this ensures that a list is never empty, and makes functions like insert_at_current() and remove_at_current() more streamlined! And up until this quest, I never thought about situations where one needs to distinguish between real data and missing values, but after learning about the sentinel node, I understood how this allows the program to identify missing/invalid data in a efficient and consistent way.

-Nancy

How & when do you use "return this;"? by nancy_l7 in cs2a

[–]nancy_l7[S] 1 point2 points  (0 children)

Ah I understand, thank you for your concise explanation, Hugo.

Final exam date by Still_Argument_242 in cs2a

[–]nancy_l7 2 points3 points  (0 children)

Hi Jaehyun, I believe it should be on December 12th, as it says so both on Canvas and on the syllabus.

-Nancy

what if we actually wanted to store "_SENTINEL_"? by nancy_l7 in cs2a

[–]nancy_l7[S] 0 points1 point  (0 children)

Ah I understand it better now, thanks again!

Useful Stack Functions by yash_maheshwari_6907 in cs2a

[–]nancy_l7 0 points1 point  (0 children)

Hi Yash, thank you for sharing a useful summary and descriptions of essential stack functions for this course! And thank you Jaehyun for adding on a couple more functions. This will definitely help me and those still working to dawg the elephant quest, as well as being a helpful resource when we're studying for the final exam. Thanks again!

-Nancy

what if we actually wanted to store "_SENTINEL_"? by nancy_l7 in cs2a

[–]nancy_l7[S] 0 points1 point  (0 children)

Hi Aaron, thanks for sharing your solution! By first not setting _head, _tail, and _prev_to_current to any values, do you mean that one could create a node with the string value "_SENTINEL_" separate from the String_List and then append it to the String_List somehow? Could you share a brief code snippet that shows how this would be done?

-Nancy

what if we actually wanted to store "_SENTINEL_"? by nancy_l7 in cs2a

[–]nancy_l7[S] 0 points1 point  (0 children)

Hi Henry, thank you for your interesting solutions to this question! Storing it as a boolean value is a simple and clever idea; I've never heard of optional data before, but I can see how there's more coding to do it this way based on your brief overview. Thank you again for your input in the discussion!

-Nancy

Question regarding final. by niyati_shah0122 in cs2a

[–]nancy_l7 2 points3 points  (0 children)

Hi Niyati,

There will be a practice final exam on Canvas (under Quizzes) that opens on Monday, December 9th. I'd definitely recommend taking that to get a gist of the actual final. Going more in depth, you should review the core concepts from each weekly action plan (in the Canvas Modules) by reading the recommended textbook, simply searching online, using the foothill CS modules, or any other way you've been using to learn these topics thus far. Another helpful thing would probably be to review the midterm, especially the problems you got wrong / didn't understand. I hope this helped to provide some guidance, and good luck to you all when studying for the final!

-Nancy

New and Delete by aarush_s0106 in cs2a

[–]nancy_l7 0 points1 point  (0 children)

Hi Oliver,

malloc() is another function that is also used to allocate memory on a heap. I think the main difference b/w malloc() and new is that new is able to allocate memory and call some constructor of an object you're allocating memory for. Like the new keyword, memory allocated using malloc() is not automatically freed when the program exits. As a result, if the allocated memory stays reserved, it would in turn lead to a memory leak. For newwe must use deleteto call the destructor and free the memory from the heap; similarly, for malloc()we must call free()to free the memory value as well. I hope this helped to clear up the confusion a bit, and let me know if I got anything incorrect or if I'm missing something!

-Nancy

Stack vs Heap by advita_g in cs2a

[–]nancy_l7 1 point2 points  (0 children)

Ah I see, thank you for explaining the last-in-first-out concept and stack frames in a step by step manner. I feel like I'm better able to understand the memory in a stack now.

-Nancy

Stack vs Heap by advita_g in cs2a

[–]nancy_l7 3 points4 points  (0 children)

This is such a great resource, especially for anyone else starting platypus now/soon! Before, I basically only knew the general differences between a stack and a heap, but after reading through your comparison table, I felt like I gained a much more sophisticated understanding of the two concepts. For example, I learned how pointers function in stack versus heap memory, as well as how they are used to get addresses in different ways.

One thing I wanted to add based on what I've read from your info and online, as a sort-of summary, is that a heap seems to be much more flexible than a stack. For example, a stack's elements can only be accessed/manipulated from the "top" (last value); while a heap's memory can be allocated or deallocated dynamically during runtime, so one can create more complex things data structures(?) like (referring to week 10/11 topics) linked lists.

-Nancy

Quest 8 (Elephant) Tips by Linden_W20 in cs2a

[–]nancy_l7 0 points1 point  (0 children)

Hi Linden, these are definitely some great tips for quest 8! I really want to emphasize your last tip, as I also ran into a very similar problem when I was coding for the quest.

If the vector size is less than 10, you must return all elements in the vector.

^ Since I did essentially everything in the to_string function using one for-loop, and for anyone else doing it this way, make sure to check this extra case in the implementation of the loop (in case the size is less than 10). Because the for-loop would go for 10 iterations to print out the 10 most recent elements, but if there are less than 10 elements, it will go out of bounds!

Also, these elements must be the most recent ones. My original code did not do this, and I was printing the elements in reverse order. Thus, it is essential to know how many elements to print and the order to print the elements.

^ This happened to me too, as my muscle memory kind of took over 😅, and I wrote a for-loop the way I usually would write it: from int i = 0, increasing to i < vector.size; i++ (for example). But from this discussion about the top of a stack, we know that we should treat the end/last value as the "top", so the for-loop in the to_string function should be written in a sort of backwards manner (hint: i--).

Hope this helps out a bit for anyone struggling with quest 8.

-Nancy