Harvard University CS50

Introduction to Computational Thinking and C Programming

SA
StudyAI Editorial
Reviewed by StudyAI tutors
· Published Updated

From the CS50: Introduction to Computer Science curriculum

TL;DR

Computational thinking is a problem-solving approach that breaks down complex tasks into smaller, manageable steps a computer can understand. We'll explore its key components and then apply them to write your first programs in C, a powerful and fundamental programming language. Learning C will give you a deep understanding of how computers work at a lower level.

1. The Mental Model

Think of computational thinking as learning to speak "computer" to solve problems. C is one of the foundational languages you'll use to tell the computer exactly what to do, step by step.

2. The Core Material

Computational thinking isn't just for programmers; it's a way of thinking that helps you solve problems systematically. It involves four key techniques:

Decomposition

This is about breaking down a complex problem into smaller, more manageable parts. Imagine you want to build a robot. You wouldn't just "build a robot"; you'd break it into "design the body," "program movement," "add sensors," etc. Each part is easier to tackle individually.

Pattern Recognition

Black and white abstract representation of a multimodal model version two, featuring geometric patterns and lines.
Photo by Google DeepMind on Pexels

Once you've decomposed a problem, you might notice similarities or patterns within the smaller problems. Recognizing these patterns helps you find general solutions that can be reused, saving time and effort. For example, if multiple parts of your robot need to move forward, you'd recognize that "moving forward" is a common pattern.

Abstraction

After identifying patterns, abstraction means focusing on the important information and ignoring the irrelevant details. You create a general concept or model. For your robot, you might abstract "moving forward" into a function that takes a distance, without needing to know the exact motor voltage every time.

Algorithms

An algorithm is a step-by-step set of instructions to solve a problem or achieve a goal. It's like a recipe. Once you've decomposed, found patterns, and abstracted, you can design a precise algorithm that a computer can follow.

Here's how these concepts link together:

graph TD
    A["Complex Problem"] --> B["Decomposition (Break Down)"]
    B --> C1["Smaller Problem 1"]
    B --> C2["Smaller Problem 2"]
    B --> C3["Smaller Problem 3"]
    C1 --> D["Pattern Recognition (Find Similarities)"]
    C2 --> D
    C3 --> D
    D --> E["Abstraction (Generalize/Focus on Essentials)"]
    E --> F["Algorithm Design (Step-by-Step Instructions)"]
    F --> G["Solution (Computer Program)"]

Introduction to C Programming

Flat lay of business conference materials including name tags, program, notebook, and pen.
Photo by Pavel Danilyuk on Pexels

C is a powerful, general-purpose programming language. It's often called a "middle-level" language because it offers both high-level control (like other programming languages) and low-level access to memory and hardware. This makes it efficient and widely used for operating systems, embedded systems, and games.

Your first C program will likely be "Hello, World!". It's a simple program that prints the phrase "Hello, World!" to the screen.

#include <stdio.h> // This line includes the standard input/output library

int main(void) { // This is the main function where your program execution begins
    printf("Hello, World!\n"); // This line prints "Hello, World!" to the console
    return 0; // This indicates that the program finished successfully
}

Let's break down this simple program:
* #include <stdio.h>: This is a preprocessor directive. It tells the C compiler to include the contents of the stdio.h file (standard input/output header) before compiling. This file contains functions like printf that we use to display output.
* int main(void): This is the main function. Every C program must have a main function, which is where the program execution starts. int means the function will return an integer value, and void means it takes no arguments.
* { ... }: These curly braces define the "body" of the main function, containing the instructions to be executed.
* printf("Hello, World!\n");: This is a statement. printf is a function from stdio.h that prints text to the console. The text inside the double quotes is called a string literal. \n is an escape sequence that represents a newline character, moving the cursor to the next line.
* return 0;: This statement returns the integer value 0 from the main function. By convention, a return value of 0 indicates that the program executed successfully.

To run this C code, you'd typically save it as a .c file (e.g., hello.c), compile it using a C compiler (like clang or gcc), and then run the resulting executable.

# Compile the code
clang hello.c -o hello

# Run the compiled program
./hello

3. Worked Example

Let's apply computational thinking to calculate the average of three numbers, then write a C program for it.

Problem: Calculate the average of three given numbers.

  1. Decomposition:

    • Get the three numbers.
    • Add them together.
    • Divide the sum by 3.
    • Display the result.
  2. Pattern Recognition: The core pattern is "add numbers, then divide by count." This is the definition of an average.

  3. Abstraction: We can think of a general "average" function that takes a list of numbers and their count, then returns the average. For this simple case, we'll hardcode 3 numbers, but the concept scales.

  4. Algorithm:

    • START
    • Declare three variables for numbers (e.g., num1, num2, num3).
    • Declare a variable for the sum (e.g., sum).
    • Declare a variable for the average (e.g., average).
    • Assign values to num1, num2, num3 (e.g., 10, 20, 30).
    • Calculate sum = num1 + num2 + num3.
    • Calculate average = sum / 3.0. (Using 3.0 ensures floating-point division.)
    • Print average.
    • END

Here's the C code:

#include <stdio.h> // Include standard input/output library for printf

int main(void) {
    // 1. Declare variables and assign values (using 'float' for decimal numbers)
    float num1 = 10.0;
    float num2 = 20.0;
    float num3 = 30.0;
    float sum;
    float average;

    // 2. Calculate the sum
    sum = num1 + num2 + num3;

    // 3. Calculate the average
    average = sum / 3.0; // Use 3.0 to ensure floating-point division

    // 4. Print the result
    printf("The average of %.1f, %.1f, and %.1f is %.2f\n", num1, num2, num3, average);

    return 0; // Indicate successful execution
}

If you compile and run this, the output will be:
The average of 10.0, 20.0, and 30.0 is 20.00

4. Key Takeaways

  • Computational thinking is a problem-solving methodology involving decomposition, pattern recognition, abstraction, and algorithms.
  • C is a foundational programming language that allows low-level control and high efficiency.
  • Every C program starts execution in the main function.
  • The printf() function is used to display output to the console.
  • Statements in C typically end with a semicolon ;.
  • Comments (like // This is a comment) help explain your code but are ignored by the compiler.

Common Mistakes to Avoid:
- Forgetting a semicolon at the end of a statement.
- Mismatched curly braces {} or parentheses ().
- Not including necessary header files (like stdio.h for printf).
- Integer division when you expect a decimal result (e.g., 5 / 2 will be 2, not 2.5; use 5.0 / 2.0 or 5 / 2.0).

5. Now Try It

Modify the "average" C program from the example. Instead of hardcoding the numbers, change the main function so that it calculates the average of your birth year, the current year, and the year you started CS50. Print the result in a sentence like "The average of [birth year], [current year], and [CS50 start year] is [calculated average]."

What success looks like: Your program compiles without errors, and when run, it prints a sentence with the correct average of your three chosen years.

Frequently asked about Introduction to Computational Thinking and C Programming

Computational thinking is a problem-solving approach that breaks down complex tasks into smaller, manageable steps a computer can understand. We'll explore its key components and then apply them to write your first programs in C, a powerful and fundamental programming language. Read the full notes above for the details.

Introduction to Computational Thinking and C Programming is a core topic in CS50: Introduction to Computer Science. Most exam papers test it via a mix of definitions, worked examples, and applied problems. The notes above cover the high-yield sub-topics, common pitfalls, and the kind of questions examiners typically set.

Yes — every note in the StudyAI Campus Hub is free to read in full, right here on this page, with no account needed. If you clone the plan into your own dashboard, the free plan shows a preview of each note there; Basic and above unlock the full notes in your dashboard, along with practice quizzes, flashcards and offline study. You can always come back here to read the complete note for free.
Continue with
Arrays, Memory, and Pointers

Study this next


Get the full CS50: Introduction to Computer Science curriculum

Clone the complete plan to your dashboard for unlimited AI-generated notes, practice quizzes, and a personalised revision schedule.

Create Free Account