Computational Thinking & Object Modeling

প্রোগ্রামারের মতো চিন্তা করা শিখুন

Read: ~35 min Beginner 8 practice problems

1. Think First, Code Last

Most beginners open the editor and start typing. Then they get stuck. Then they Google. Then they copy-paste. Professional engineers do the opposite: they decompose, model, then code.

বেশিরভাগ শিক্ষার্থী সরাসরি কোড এডিটর খুলে টাইপ করতে শুরু করেন এবং সমস্যায় পড়েন। অভিজ্ঞ প্রোগ্রামাররা ঠিক উল্টোটা করেন — আগে সমস্যাটি ভাগ করেন, তারপর model বানান, এরপর কোড।
Rule of this course Don't write code until you can explain your algorithm and your data model in plain English (or Bangla).
কোর্সের নিয়ম: algorithm এবং data model বুঝিয়ে বলতে না পারলে কোড লেখা শুরু করবেন না।

2. The Four Pillars of Computational Thinking

PillarMeaningবাংলায়
DecompositionBreak a big problem into smaller parts.বড় সমস্যাকে ছোট ছোট অংশে ভাগ করা।
AbstractionHide details, expose only what matters.অপ্রয়োজনীয় বিস্তারিত আড়াল করে শুধু গুরুত্বপূর্ণ অংশ দেখানো।
Pattern RecognitionNotice when problems share structure.একই ধরনের গঠন বারবার চিনতে পারা।
Algorithmic DesignWrite step-by-step instructions a machine can follow.মেশিন যে ধাপগুলো অনুসরণ করতে পারে এমন instruction তৈরি করা।

3. Object Modeling — The C++ Way

C++ adds a powerful new way to think: in objects. Before you code, ask: "What real-world things am I modeling?" Each thing becomes a class. Each class has state (data) and behavior (functions).

C++ একটি নতুন চিন্তার পথ দেয় — object হিসেবে চিন্তা করা। কোড লেখার আগে নিজেকে জিজ্ঞাসা করুন: "আমি কোন বাস্তব জিনিসকে model করছি?"। প্রতিটি জিনিস হবে একটি class, যেখানে থাকবে data (state) এবং behavior (functions)।

Example: Modeling a library system:

Decomposition
  • Book: title, author, ISBN, isAvailable()
  • Member: name, id, borrowedBooks
  • Library: collection of Books, list of Members, lend(), return()

4. Pseudocode — Algorithm Before Syntax

Before C++ syntax, write pseudocode. It is language-agnostic.

Pseudocode: find max in a list
input: list L of numbers
max ← L[0]
for each element x in L:
    if x > max:
        max ← x
output: max
            

Now convert to C++:

find_max.cpp
#include <iostream>
#include <vector>

int main() {
    std::vector<int> L = {3, 1, 4, 1, 5, 9, 2, 6};
    int max_val = L[0];
    for (int x : L) {
        if (x > max_val) max_val = x;
    }
    std::cout << "Max = " << max_val << "\n";
    return 0;
}

5. Loop Invariants — The Math of Loops

A loop invariant is a condition that is true before, during, and after each iteration. Stating the invariant is how we prove a loop is correct.

Invariant for find_max above After processing the first i elements, max_val equals the largest of those i elements. This is true at start (i=1, only L[0] seen). If true at step i, it stays true at i+1 (we either keep max or update it). Therefore at the end, max_val = max of all elements.
Loop invariant হলো এমন একটি শর্ত যা loop-এর প্রতিটি iteration-এর আগে এবং পরে সত্য থাকে। এটি ব্যবহার করে আমরা proof দিতে পারি যে আমাদের loop সঠিক ফলাফল দিচ্ছে।

6. Proof by Induction — In Code

Recursion is induction in code. Base case proves the smallest case; recursive case shows: if true for n-1, true for n.

factorial.cpp
#include <iostream>

long long factorial(int n) {
    if (n <= 1) return 1;        // base case
    return n * factorial(n - 1); // inductive step
}

int main() {
    for (int i = 0; i <= 10; ++i) {
        std::cout << i << "! = " << factorial(i) << "\n";
    }
    return 0;
}

7. Practice Problems

  1. Decompose: list the classes you'd design for a ride-sharing app. Mention 3 classes with their state and one behavior each.
    একটি ride-sharing app-এর জন্য ৩টি class ডিজাইন করুন। প্রতিটির state এবং একটি behavior লিখুন।
    ✨ Show Answer

    Rider: state — name, location, paymentMethod; behavior — requestRide().
    Driver: state — name, vehicle, isAvailable; behavior — acceptRide().
    Trip: state — pickup, dropoff, fare, status; behavior — calculateFare().

  2. Write pseudocode to count even numbers in a list, then translate to C++.
    একটি লিস্টে কয়টি জোড় সংখ্যা আছে — তা গণনার pseudocode লিখুন এবং C++-এ অনুবাদ করুন।
    ✨ Show Answer
    count_even.cpp
    #include <iostream>
    #include <vector>
    
    int main() {
        std::vector<int> v = {2, 3, 4, 7, 10, 11};
        int count = 0;
        for (int x : v) if (x % 2 == 0) ++count;
        std::cout << "Even count = " << count << "\n";
    }
  3. Write the loop invariant for a function that sums a list.
    একটি লিস্টের যোগফল গণনার ফাংশনের loop invariant লিখুন।
    ✨ Show Answer

    Invariant: After iteration i, the variable sum equals the sum of the first i elements of the list. At i=0, sum=0 (empty sum). At i=N, sum equals the total sum. ✓

  4. Write a recursive function to compute the sum 1+2+...+n in C++.
    1 থেকে n পর্যন্ত যোগফল recursive ফাংশন দিয়ে বের করুন।
    ✨ Show Answer
    sum_n.cpp
    #include <iostream>
    
    int sum(int n) {
        if (n <= 0) return 0;
        return n + sum(n - 1);
    }
    
    int main() {
        std::cout << sum(100) << "\n"; // 5050
    }
  5. Identify pattern: what do "find max", "find min", and "find sum" have in common?
    "find max", "find min", "find sum" — এই তিনটির মধ্যে মিল কী?
    ✨ Show Answer

    All three are fold/reduce operations. They start with an initial accumulator and combine each element with it: max uses max(acc, x), min uses min(acc, x), sum uses acc + x. STL has std::accumulate for exactly this pattern.

  6. Write a program that prints all even numbers from 1 to 20.
    ১ থেকে ২০ পর্যন্ত সব জোড় সংখ্যা প্রিন্ট করুন।
    ✨ Show Answer
    even.cpp
    #include <iostream>
    
    int main() {
        for (int i = 2; i <= 20; i += 2) {
            std::cout << i << " ";
        }
        std::cout << "\n";
    }
  7. Why is decomposition important when designing software?
    সফটওয়্যার ডিজাইনে decomposition কেন গুরুত্বপূর্ণ?
    ✨ Show Answer

    Decomposition lets you focus on one small problem at a time, makes code reusable, and allows multiple engineers to work in parallel on different parts. Without it, large systems become unmaintainable.

  8. Compute factorial(15) using the program above. Why might it overflow with int?
    factorial(15) উপরের প্রোগ্রাম দিয়ে বের করুন। int overflow কেন হতে পারে?
    ✨ Show Answer

    15! = 1,307,674,368,000 — this exceeds the max int value (~2.1 billion on 32-bit). Use long long (as in the example) which holds up to ~9.2 × 1018. Beyond 20! you need big-integer libraries.

Summary — Module 02

Computational thinking has four pillars: decomposition, abstraction, pattern recognition, and algorithmic design. In C++, abstraction takes the form of objects — classes that bundle state and behavior. Always think first, write pseudocode, prove correctness with invariants, and only then translate to syntax.

Next Module → The Toolchain: Compiler, Linker & Build Systems.