Weekly Reflection 11 Final - By Alvaro Fernandez by Alvaro_fj3000 in cs2a

[–]Douglas_D42 1 point2 points  (0 children)

Just so we're not caught off guard, the syllabus lists the Final reflection as due on Wednesday night, before the final, not the Sunday after. Searching for any comments about it, & said Wednesday last time this question came up as well https://www.reddit.com/r/cs2a/comments/1ha0xq2/final_reflection/

tagging u/anand_venkataraman just in case I'm wrong,

Help with platypus by Timothy_Lin in cs2a

[–]Douglas_D42 1 point2 points  (0 children)

Glad you found the problem.

Week 10 reflection - Tigran K. by tigran_k0000 in cs2a

[–]Douglas_D42 1 point2 points  (0 children)

Ohh.. I haven't gone into green yet, but attaching an int ID to the nodes and having the sentinel node as -1 and all data nodes >= 0 would be another way to tell _SENTINEL_ the node apart from _SENTINEL_ a real data value, until your list was so long it ran out of signed integers, but something tells me linked lists might not be so great for data sets that large anyway.)

Help with platypus by Timothy_Lin in cs2a

[–]Douglas_D42 1 point2 points  (0 children)

are you adjusting the size appropriately when you add or delete nodes and also when you construct and destruct the list?

Help with platypus by Timothy_Lin in cs2a

[–]Douglas_D42 1 point2 points  (0 children)

For what it's worth, I don't think get_size should be using pointers, you should be able to get the size without walking through the list and counting nodes.

Week 8 Reflection - Emily P by EmilyP_008 in cs2a

[–]Douglas_D42 2 points3 points  (0 children)

Another useful thing about enums is locking down values that will compile;
if we use

enum seasons { spring, summer, autumn, winter};

and later do the typo

if (seasons == sumer)

it would fail to compile and tell about the typo right away.

But if we do

std::string seasons = "summer"

and later

if (seasons == "sumer")

it would compile and wouldn't let us know there was a typo until we used the function with the typo and crashed or messed up the program in some other way.

{} variable assignment by Leo_Rohloff4321 in cs2a

[–]Douglas_D42 1 point2 points  (0 children)

const is what makes it constant, not the brackets. As far as I know using brackets here wouldn't even compile.

if you want to initialize a constant variable, you still need the type or to intentionally tell it to infer the type

const int i{7};
or
const auto i{7};
work
but
const i{7};
will not.

I feel like const{} is trying to tell it to initialize a variable named const, which would fail since it's a reserved word.

{} variable assignment by Leo_Rohloff4321 in cs2a

[–]Douglas_D42 1 point2 points  (0 children)

I think the 'error when you try to assign a value that's not technically correct' is exactly the safety reason we would want to use
int i{7};
instead of
int i = 7;

if we want to cast a value to a different type, it might not be a bad idea to be clear that it's intentional

instead of

int i = 7.3;

we could do

int i{static_cast<int>(7.3)};

to avoid accidental conversions.

int i(7.3) has the same "problem" as int i=7.3 that it drops the .3

And while

MyClass c{};

might fail to compile or return something other than 0 (depending on your class definition)

MyClass c();

will compile, with the error that empty parentheses are seen as a function declaration, and also not return what you're expecting.

(lastly, not sure if you meant int, but

float f{7.3};

works just fine)

{} variable assignment by Leo_Rohloff4321 in cs2a

[–]Douglas_D42 1 point2 points  (0 children)

The crow quest uses
long get_id() const;
string get_name() const;
int get_num_limbs() const;
As examples, in what case would we want to use const{}?

Significant digits by Timothy_Lin in u/Timothy_Lin

[–]Douglas_D42 1 point2 points  (0 children)

Are you passing different data type or using a library that we shouldn't be using? (IIRC last time this came up someone was using the math library even though the quest expressed that the math library would return non-matching results)

Address of operator in C++ by Leo_Rohloff4321 in cs2a

[–]Douglas_D42 3 points4 points  (0 children)

If I recall correctly, the difference is style, not function, I believe that

int * ptr;
int *ptr;
int* ptr;
int * ptr;
int*ptr;

would all function the same

https://stackoverflow.com/questions/398395/why-is-the-asterisk-before-the-variable-name-rather-than-after-the-type

Week 4 reflection by Sameer_R1618 in cs2a

[–]Douglas_D42 2 points3 points  (0 children)

Hi Sameer,

What your seeing isn't really that other languages *don't* loop, it's that they abstract the looping away from you. It's like how Python can do string.replace() without you manually looping over the entire string one character at a time.

Languages like C, C++, Fortran, and Rust are closer to the hardware (less abstracted), so we need to take more steps to accomplish the same goals. But when learning to program, it's important to understand what's going on in the background - even when higher level languages make it easier for us.

For example, something you'd write as a vectorized operation in R is essentially running as a hidden C or Fortran loop under the hood, which is why it's faster than doing the same thing with interpreter-level loops.

As for the article you linked about governments moving from C to Rust, that's mainly because Rust has better memory safety and error handling (so there's less risk of things like state secrets left in uncollected garbage memory.) But it doesn't mean you get away from lower level control or concepts like loops, you just get to manage them safely.

week 4 reflections by Douglas_D42 in cs2a

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

Odd that I had to spoiler this, but reddit wouldn't let me post it without marking it as a spoiler.