This week was all about arrays—a core building block in programming that lets you store and manage lists of data. After learning about basic variables last week, working with arrays felt like opening up a whole new toolbox. Instead of dealing with one value at a time, now I could handle multiple pieces of information in one structure.
Coming into CS50 with some Python experience, I thought arrays in C would be pretty straightforward. In Python, working with lists is second nature:
codenumbers = [1, 2, 3, 4, 5]
for number in numbers:
print(number)
What I Learned
Key concepts from this week:
- Arrays: How to store sequences of elements (like integers, characters, or floats) using one variable name.
- Strings: Realizing that a string is just an array of characters ending with
\0(the null terminator). - Looping through arrays: Using
forloops to access each element by its index. - Caesar and Substitution Ciphers: My first real exposure to cryptography and manipulating characters in C.
Understanding that arrays start at index 0 tripped me up a little, but it makes sense now.
Challenges I Faced
One of the biggest struggles this week was getting used to off-by-one errors. For example, looping through an array from i = 0 to i <= length accidentally made me access elements outside the array’s memory space, which caused bugs or weird outputs.
I also had to be extra careful when working with strings and remembering that they end with the \0 character. Forgetting that detail can lead to strange issues, like printing garbage values from memory.
Key Example: Caesar Cipher
One of the assignments was to build a Caesar Cipher, where each letter in a string is “shifted” by a key number.
Here’s a small piece of the code I wrote:
codefor (int i = 0; i < strlen(plaintext); i++)
{
if (isalpha(plaintext[i]))
{
char base = isupper(plaintext[i]) ? 'A' : 'a';
ciphertext[i] = (plaintext[i] - base + key) % 26 + base;
}
else
{
ciphertext[i] = plaintext[i];
}
}
In Python, I probably would’ve knocked this out in a few lines with slices or built-in functions. But writing it in C forced me to understand every step: how characters are stored, how ASCII values work, and how to loop without stepping out of bounds.
Big Takeaways
- C forces you to slow down and think about what’s happening in memory.
- Coming from Python, I realized I’ve been spoiled by automatic memory management and flexible data structures.
- Modular arithmetic is a key tool, especially when building ciphers and working with letter shifts.
If I had to give tips on how to approach this portion of the course:
- Draw out your array indices on paper to avoid off-by-one mistakes.
- Always remember that C strings end with a
\0—it matters more than you think. - Break big problems (like ciphers) into smaller pieces. It makes the code easier to write and debug.
This week helped me start thinking in patterns—how to process each element of a list one by one and how to manipulate data efficiently. Arrays are a bit tricky at first, but they’re a game-changer once you understand them.
