‹ go back

CS50: Week 2.

Published 09 July 2020 at Yours, Kewbish. Also published on Dev.to. 1,064 words. Subscribe via RSS.


This post is unlisted and has been archived. This doesn't represent my best work; please check out the posts listed here instead.

Introduction

And now, our third week of CS50!1 This week, we went over arrays, but also spent a lot of time on CLI / terminal things, and debugging tools used by CS50. I’m starting to get more familiar with the CS50 system, and how I can do problem sets and debug on my local system, instead of the cloud IDE. (Again, remember how I hate it?) This was our introduction to proper command line arguments, and how to use function arguments in main too.

Notes

Some of this references the video and the problem set, so you might want to check out the notes here.

  • The lecture itself spends a bunch of time going through CLI information
    • check50, debug50, style50 - the whole CS50 family
    • set up check50 and style50 on my local system already through pip, easy install
    • oh, and help50 exists, don’t think I’ve ever used it. Stack Overflow is more useful, I find.
  • How does C work?
    • first, preprocessed to pull in headers
    • compiled to assembly code
    • then assembly to binary
    • linked to final executable file
    • all happens when you run clang or the processor
  • data fits into types
    • each has a finite set amount of memory (except strings but shhh)
    • bool -> 1 byte
    • char -> 1
    • int or float -> 4
    • double or long -> 8
    • string -> ?, because the number of chars in the string varies, and therefore can change the amount of memory assigned
    • each variable is labelled in memory with an address
  • when you define with a const, its value never changes
  • string -> represented as an array of characters
    • ends with a null term byte -> \0
    • escaped with the \
    • this is why you use < strlen instead of <= strlen, so you don’t catch the ending byte
    • string memory used is # of char * 1 byte + 1 byte for ntb
  • ASCII chars can be subtracted and added from each other
    • their # code and their character equivalent can be used interchangeably
    • kind of unintuitive to subtract chars, I prefer numbers
  • The ctype.h has useful functions
    • checks for alphabetic, digits, and all the other type things
    • equivalent of typeof in Python
  • Use *argv when using strlen to make the actual ‘string’ array
    • Otherwise, can’t use strlen, and other things
  • For encryption psets, I prefer using # codes
    • unfamiliar but more intuitive to do distance-from and mod-math
    • most of the psets involve finding a distance, adding a key in some way, and looping back at times
    • to loop back from an alphabet, use modmath!
    • try to use pseudocode values in subtraction equation
  • Return codes exist, why they have int main
    • returns either 0, 1, etc.
    • 1 -> indicate error
    • 0 -> everything’s fine
  • argc -> make sure that the array count is greater than a certain amount
    • if you try to access something that doesn’t exist in memory yet, it will throw a dreaded Segmentation Fault
  • generally, structure validation tests above the main function
    • otherwise, define the prototype function, and put it below the main function

Problem Sets

Last week, I mentioned how I prefer doing both of the ‘more and less’ problems in the problem set for a week, noting that the ‘more’ usually builds off the ’less’. This week was a great example of that.

Readability doesn’t really count, that was more an introduction to looping (? I didn’t use an array at all so I’m not sure what they’re on about), but Caesar and Substitution were super fun. Caesar is, what you might think, a caesar cipher implementation. Substitution implements a simple substitution cipher. While working through Caesar, I found a bunch of information about character codes and validating command line arguments, as well as working with aforementioned character codes and CLI arguments. For example, I was super confused about a Segmentation Fault that was thrown while attempting Caesar because I’d forgotten to check that the argument actually existed. As well, I learned that you could :gasp: subtract characters, instead of just using the character code. These two morsels of information were super helpful in solving Substitution. Both ciphers deal with some sort of ‘distance from something else and then add key’ algorithm, and I found that figuring Caesar out let me speedrun through Substitution so much more quickly.

Setting up CS50 locally

I’ve finally finished setting up CS50’s development tools on my own system - since last week, I’ve been trying to tweak a couple commands and things to make everything just work.

Here’s a small checklist of things that I’d recommend setting up, and some caveats.

  • install CS50.h from their site. I wouldn’t recommend trying to curl things, just install from source. It’s easier, especially if you’re on WSL.
    • No need to set up environment variables - more on that later in the bash bit.
    • if you don’t want to bother adding it to your C source folder, you can just put it somewhere convenient to relative path, and use #include "../cs50.h", for example
  • set up check50 through pip
    • It’s literally just pip install check50.
  • do the same with submit50 and style50.
    • their installations are equally simple - pip install submit50 and pip install style50.
    • As they remind, do this through WSL. Having to switch between WSL and non-WSL command prompts is annoying, even in VSCode.
  • in the WSL .bashrc, add a function that has the following command:
cs50make() {
    # command copied from running make in ide.cs50.io
    # replace the [whatever].c with "$1".c, and same for the output
}
export -f cs50make

which will let you run cs50make in terminal and run the equivalent make command

See - it’s not that hard. Even easier on Linux, probably. And now, you can CS50 in VSCode, and have beautiful syntax highlighting and snippets. God, I hate that IDE.

Conclusion

Apologized for the extremely strange blogging schedule - expect another post on Sunday as well. I’d finished a couple of problem sets and weeks in one week, while my writing schedule was supposed to run at one post per week. I’ve had to spend a bunch of time writing, so please appreciate me-

I’ve been working on some random scripts - nothing on GitHub, just little automation things. Been doing a little maintenance on diveintoht.ml as well. Hopefully I’ll start a bigger project soon, looking forward to the final project of CS50!


  1. Undoubtedly one of the more difficult weeks. ↩︎


‹ go back