← CS50: Introduction to Computer Science

Week 2: Arrays

From typed source code to a running program, plus the first real data structure: the array, and the strings and ciphers built on top of it.

8 topics · 30 questions

Each topic is a path, not a menu: work down it in order and every question builds on the one above. Click any of them to watch the answer draw itself.

From Source Code to Executable

  1. How it worksHow does the preprocessor expand every hash-include before a single line of your code is even checked?
  2. How it worksHow do compiling, assembling, and linking turn readable source code into pure machine instructions?
  3. Worked exampleWalk me through a one-line C program passing through all four stages: preprocessing, compiling, assembling, linking.
  4. Common mistakeWhy isn't compiling a program the same step as running it?
  5. Going furtherWhat happens at the linking stage when your program calls a function like printf that you never wrote?
Your turn1 of 2
A build processes 15 separate .c files, and each file passes through 3 stages (preprocessing, compiling, assembling) independently before linking combines all the results into 1 executable. How many total stage-runs happen across preprocessing, compiling, and assembling for all files?

Debugging Strategies

  1. Worked exampleWalk me through using a debugger to step line by line through a loop that is off by one.
  2. Worked exampleWalk me through using print statements alone to narrow down which of three functions contains a bug.
  3. Common mistakeWhy isn't a program with no compiler errors the same thing as a program with no bugs?
  4. Going furtherWhat happens to your search for a bug once you add a print statement right before the line that misbehaves?
Your turn1 of 2
A program's 218 lines are split evenly across 3 functions. A student adds print statements to check each function's output separately, narrowing the bug down to just one function. How many lines are inside that one guilty function?

Arrays in Memory

  1. How it worksHow does an array store all of its elements as one unbroken block of memory, back to back?
  2. Common mistakeWhy doesn't an array in C know its own length once it has been created?
  3. Worked exampleFind the memory address of the third element of an int array starting at address 1000, where each int takes 4 bytes.
  4. Going furtherWhat happens in memory the moment you access index 10 of an array that only has 5 elements?
Your turn1 of 2
An int array starts at memory address 3786, and each int takes 4 bytes. What is the memory address of the array's element at index 13?

Strings as Arrays of Characters

  1. How it worksHow does a C string end up as an array of characters with a hidden null terminator marking where it stops?
  2. Worked exampleWalk me through how the string cat is stored as four bytes in memory, including its null terminator.
  3. Common mistakeWhy isn't a string a real, built-in type in C the way int and float are?
Your turn1 of 2
A string has 21 visible characters, stored in memory including its null terminator. How many total bytes does it occupy?

Comparing and Copying Strings

  1. Worked exampleWalk me through why comparing two strings with == in C compares their addresses, not their letters.
  2. Common mistakeWhy doesn't == ever compare the actual characters sitting inside two C strings?
  3. Going furtherWhat happens when you copy a string by copying its pointer instead of copying its characters?
Your turn1 of 2
Comparing two strings character by character, a mismatch is found at position 14 (counting from position 0). How many characters were compared, including the mismatching one, before the comparison stops?

Command-Line Arguments

  1. How it worksHow do argc and argv hand a program the words typed after its name on the command line?
  2. Worked exampleWalk me through what argc and argv[1] hold when a program is run as ./greet David.
  3. Going furtherWhat happens to argc when a program is run with no extra words after its name at all?
Your turn1 of 2
A program is run with its name followed by 14 extra words on the command line. What value does argc hold?

The Caesar Cipher

  1. How it worksHow does a Caesar cipher shift every letter of a message forward by the same fixed number of positions?
  2. How it worksHow does the alphabet wrap from Z back around to A when a shift pushes a letter past the end?
  3. Worked exampleWalk me through encrypting the word HI with a Caesar cipher using a key of 3.
  4. Common mistakeWhy isn't a Caesar cipher safe just because there are 25 possible shifts to try?
Your turn1 of 2
In a Caesar cipher with key 7, a letter sits at position 10 in the alphabet (A=0). Assuming no wraparound is needed, what position does it shift to?

The Substitution Cipher

  1. How it worksHow does a substitution cipher map every letter of the alphabet to one other letter, all at once?
  2. Worked exampleWalk me through encrypting the word DOG using a substitution key that maps D to Q, O to W, and G to E.
  3. Going furtherWhat happens to a substitution cipher's security once an attacker can count how often each letter appears?
  4. Ties togetherHow does a substitution cipher's letter-frequency weakness connect to why modern cryptography avoids letter maps entirely?
Your turn1 of 2
A substitution cipher maps each of the 26 letters to a unique other letter. If 18 of the 26 mappings have already been figured out by an attacker using letter-frequency analysis, how many mappings are still unknown?