CS50 Lecture 0 – Comprehensive Notes

Course Intro & Professor’s Personal Story

  • David J. Malan (a former CS50 student himself, now Senior Lecturer) opens Lecture 0 in Sanders Theatre.
    • Lived in Matthews Hall freshman year, once Matt Damon’s room.
    • Initially majored in Government; feared CS50’s reputation.
    • Sophomore year, followed friends to CS50 (then taught by Brian Kernighan) and “found his people.”
  • Moral: Your starting comfort level with computers does not dictate your final trajectory.

Course Ethos & Structure

  • Majority of students have never taken computer science before.
  • Grading philosophy: measure you “relative to yourself,” not classmates.
  • Support system: Teaching Fellows, Course Assistants, Malan himself.
  • Key Traditions
    • CS50 Puzzle Day (logic-puzzle social kickoff with pizza & prizes).
    • CS50 Hackathon (overnight coding sprint for final projects).
    • CS50 Fair (campus-wide expo; demo something we didn’t explicitly teach you—proof you can learn on your own).
    • “I took CS50” T-shirt.

What Is Computer Science?

  • Study of information: representation & processing.
  • Emphasis on computational thinking → applying CS ideas to fields from arts to natural sciences.
  • Ultimate abstraction: Problem → (processing/algorithm) → Solution.
    • “Input ➔ Black box ➔ Output” mental model.

Representing Information

Binary & Numeric Systems

  • Unary (base-1): count with fingers 1,2,3,4,51,2,3,4,5.
  • Binary (base-2): each finger/light bulb has 2 states (off/on).
    • One hand can show 251=312^5-1 = 31 different numbers (0–31).
    • Demo with 3 light bulbs: 000<em>2=0</em>10,  111<em>2=7</em>10000<em>2 = 0</em>{10}, \; 111<em>2 = 7</em>{10}.
    • Columns carry weights 20,21,22,2^0, 2^1, 2^2,\ldots analogous to decimal’s 100,101,10210^0,10^1,10^2.
  • Nomenclature: binary digit → bit. 8 bits → byte.
    • Highest with one byte: 281=2552^8-1 = 255 (values 0–255 ⇒ 256 possibilities).

ASCII & Unicode

  • ASCII: 7/8-bit standard mapping; e.g.,
    • 651065_{10} → ‘A’, 6666 → ‘B’, 7272 → ‘H’, 7373 → ‘I’, 3333 → ‘!’.
  • Limit: 256256 characters ≠ enough for world languages.
  • Unicode: extended (8-,16-,24-,32-bit) superset; goal: every human symbol + emoji.
    • Example: “😂” (face with tears of joy) = 4036991106104\,036\,991\,106_{10} (32-bit code point).

Colors, Images, Video, Sound

  • RGB: 3 bytes per pixel (red, green, blue).
    • Example pixel [72,73,33][72,73,33] ≈ medium-yellow.
  • Images = grids of pixels ⇒ megabytes.
  • Video = ~24–30 images/second ⇒ gigabytes (motion picture illusion).
  • Sound: store pitch, volume, duration (and instrument) as numbers; MIDI-like.

Algorithms & Problem Solving

Phone-Book Example (Searching for “John Harvard”)

  1. Linear search: page 1 → n (worst-case nn).
  2. Jump 2 pages at a time + back 1 (≈n/2n/2).
  3. Binary Search: open middle, discard half each time (worst-case log2n\log_2 n ≈ 10 steps in 1,000-page book).
Big-O Visualization
  • Linear: O(n)O(n) (red).
  • Half-linear: O(n/2)O(n/2) (yellow).
  • Logarithmic: O(logn)O(\log n) (green; flattest).

Pseudocode Building Blocks

  • Functions (actions/verbs) e.g., pickUpPhone().
  • Conditionals: if / else if / else.
  • Boolean expressions (yes/no): touching? equal? < ?
  • Loops: go back to / repeat / while / forever.
  • Indentation conveys structure; beware infinite loops → must progress.

AI & Large Language Models

  • Hand-coding every chatbot answer is infeasible → train on data.
  • Neural networks: layers of “neurons,” probabilities decide output.
  • CS50’s mascot: a giant rubber duck (debugging tradition: explain problem aloud).
    • cs50.ai: course-specific LLM assistant (allowed; ChatGPT & generic models disallowed).

Tools & Support

  • IDE: Cloud version of Visual Studio Code (vscode.dev + CS50 add-ons).
  • Built-in Duck assistant and other course plug-ins.

Programming Basics with Scratch

Environment Overview

  • Palette of colored blocks (Motion blue, Looks purple, Sound pink, Control orange, Events yellow, etc.).
  • Sprites = movable characters (default: orange cat).
  • Stage (x,y): center (0,0)(0,0), corners x=±240,y=±180x=\pm240, y=\pm180.

Blocks as Functions, Inputs & Outputs

  • Example: ‘say “Hello World”’: input = text; side effect = speech bubble.
  • Mapping to paradigm:
    • Input: “Hello World”
    • Algorithm: say block
    • Output: visible bubble.

Variables & Return Values

  • ask “name?” + wait → returns answer (stored value).
    • join concatenates "Hello, " + answer.

Loops, Conditionals, Events

  • repeat n, forever.
  • if then …
  • Event blocks: when green-flag-clicked, when sprite clicked, when key pressed, when video motion > x, etc.

Custom Blocks (User-Defined Functions)

  • ‘make a block’: define meow n times with parameter n.
  • Reuse without duplicating code → easier maintenance.

Practical Demos

  1. Hello, World! speech bubble.
  2. Ask → Say personalized greeting.
  3. Meowing cat:
    • Sound block in loop; added wait; replaced with custom block meow n times.
  4. Event-driven “pet the cat”:
    • Forever → if touching mouse → meow.
  5. Video sensing: webcam motion triggers meow (demonstrated live).

Building Larger Projects in Scratch

Incremental Development Philosophy

  • Build feature 0 → test → add feature 1, etc. Avoid overwhelming complexity.
Example 1: “Oscar Time” (Malan’s childhood-song game)
  • Sprites: Oscar, trash can lid, multiple trash objects, lamppost background.
  • Mechanics
    • Trash set to random x, start y = 180, draggable, falls y=y1y=y-1 per frame.
    • If touching Oscar → score++
    • Variables: score displayed on stage.
    • Sound layer: Sesame-Street “I Love Trash.”
    • Added sprites & complexity gradually: +1 trash, +score, +music, +lid animation.
Example 2: “Ivy’s Hardest Game” (former student)
  • Levels escalate: walls, Yale sprite bouncing horizontally, MIT sprite chasing player, random Princeton dangers.
  • Techniques used:
    • Custom blocks: listenForKeyboard, feelForWalls.
    • Harvard sprite
    • If arrow key pressed → change x/y by ±1\pm1.
    • If touching wall → negate movement.
    • Yale sprite
    • Start centered, forever move, if wall touch → turn 180180^{\circ}.
    • MIT sprite
    • Forever point toward Harvard, move step size s (difficulty via s).
    • Level progression via touching goal sprite.

Tools for Better Code

  • Don’t Repeat Yourself (DRY): avoid copy-pasting; abstract with loops & functions.
  • Modularity: hide “implementation details” under custom blocks; simplifies main script.

Key Takeaways & Mindset

  • Computers understand only 00 and 11; layers of abstraction let us think in letters, images, music, and games.
  • Algorithms matter: choosing O(logn)O(\log n) over O(n)O(n) can be life-changing at scale.
  • Programming primitives (functions, variables, conditionals, loops) exist in every language; syntax differs.
  • Approach large projects incrementally; test early and often.
  • Embrace debugging: explain problems to a rubber duck (or cs50.ai) and identify your own mistakes.
  • CS50’s goal: by term’s end, remove the “training wheels” so you can teach yourself any new tech.