0% found this document useful (0 votes)
3 views17 pages

Programming Study Guide

Uploaded by

201918037
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views17 pages

Programming Study Guide

Uploaded by

201918037
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Programming

Topics Covered
• C basics & data types
• Functions & recursion
• Arrays, strings & star patterns
• Pointers & dynamic memory
• OOP (C++ & Python)
• Data structures (Stack/Queue/LL/BST)
• Sorting algorithms
• Deep learning & YOLO
1. C Fundamentals Data types · Operators · Control flow

1.1 Data types — know sizes and ranges

C is a strongly typed language — every variable must be declared with a type before use.
Type Size Range / Use Typical use in robotics

−2,147,483,648 to
int 4 bytes Loop counters, joint indices
+2,147,483,647

float 4 bytes ±3.4×10³⁸ (6–7 sig. digits) Sensor values, angles

double 8 bytes ±1.7×10³⁰⁸ (15 sig. digits) Kinematics calculations

char 1 byte −128 to 127 (or 0–255 unsigned) Characters, small counts

long 8 bytes Very large integers Timestamps, encoders

void 0 No value Functions returning nothing

Exam tip: Memory formula: total bytes = sizeof(type) × count. E.g. int arr[100] uses 4×100 = 400 bytes.

1.2 Operators — precedence and common traps

Category Operators Key gotcha

Arithmetic + − * / % ++ −− 5/2 = 2 (integer division!); 5.0/2 = 2.5

Relational == != > < >= <= = is assignment; == is comparison. Most common bug in C!

Logical && || ! && short-circuits: if left side is false, right side is NOT evaluated

Bitwise & | ^ ~ << >> x<<1 = x×2; x>>1 = x÷2 (fast multiply/divide by powers of 2)

Ternary condition ? a : b max = (a>b) ? a : b; — compact one-line if-else

Pre-increment vs post-increment (very common output question)

int x = 5;
printf("%d", x++); // prints 5 FIRST, then increments x → x becomes 6
printf("%d", ++x); // increments x FIRST → x becomes 7, then prints 7

int a = 7;
printf("%d", a >> 1); // 7 in binary = 0111, shift right = 0011 = 3

1.3 Control flow

// if-else
if (x > 0) { printf("positive"); }
else if (x < 0) { printf("negative"); }
else { printf("zero"); }

// switch — use for multiple fixed values


switch(grade) {
case 'A': printf("Excellent"); break;
case 'B': printf("Good"); break;
default: printf("Other");
}

// for loop — when count is known


for (int i = 0; i < n; i++) { ... }
// while — when condition-based
while (x > 0) { x /= 2; }

// do-while — executes AT LEAST ONCE (good for input validation)


do { scanf("%d", &n); } while (n < 0);

Example: Trace this — what is the output?


int i=1, sum=0;
while(i <= 5) { sum += i; i++; }
printf("%d", sum);
→ sum = 1+2+3+4+5 = 15. Answer: 15

Topic What Need to Know


Algorithm Step-by-step procedure to solve a problem.
Flowchart Graphical representation of an algorithm.
Compiler Converts entire source code into machine code before execution.
Interpreter Executes code line by line.
Variables Named memory locations used to store data.
Data Types Define the type of data (int, float, char, etc.).
Constants Values that do not change during execution.
Operators Symbols used for calculations and comparisons.
Input/Output Taking user input and displaying output.
if Executes code when a condition is true.
if-else Chooses between two alternatives.
Nested if if statement inside another if statement.
switch-case Multiple-choice decision structure.
for Loop Repeats code for a known number of iterations.
while Loop Repeats while a condition remains true.
do-while Executes at least once before checking condition.
break Terminates loop or switch.
continue Skips current iteration and moves to next.
2. Functions & Recursion Call by value · Call by reference · Factorial · GCD

2.1 Call by value vs call by reference

Call by value: a copy is passed — changes inside the function do NOT affect the original variable.
Call by reference: the memory address is passed — changes DO affect the original.
// Call by value — original unchanged
void doubleVal(int x) { x = x * 2; } // x is a local copy
int a = 5;
doubleVal(a);
printf("%d", a); // still 5!

// Call by reference — original changes


void doubleRef(int *x) { *x = *x * 2; } // dereference pointer
int b = 5;
doubleRef(&b); // pass ADDRESS of b
printf("%d", b); // now 10

Exam tip: Arrays are ALWAYS passed by reference in C — there is no copy. Modifying array elements inside a function
changes the original array.

2.2 Recursion — the 3 essential ingredients

Every correct recursive function must have: (1) a base case that stops recursion, (2) a recursive case that calls itself
with a smaller input, (3) guaranteed progress toward the base case.
// Factorial: n! = n × (n-1) × ... × 1
int factorial(int n) {
if (n == 0 || n == 1) return 1; // base case
return n * factorial(n - 1); // recursive case
}
// Trace: factorial(4) = 4 × factorial(3)
// = 4 × 3 × factorial(2)
// = 4 × 3 × 2 × factorial(1)
// = 4 × 3 × 2 × 1 = 24

// Fibonacci: 0,1,1,2,3,5,8,13,...
int fib(int n) {
if (n <= 1) return n; // base case
return fib(n-1) + fib(n-2); // recursive case
}

// GCD — Euclidean algorithm


int gcd(int a, int b) {
if (b == 0) return a; // base case
return gcd(b, a % b); // recursive case
}
// Trace: gcd(48,18) = gcd(18,12) = gcd(12,6) = gcd(6,0) = 6

Time complexity:
factorial(n): O(n) — n calls on the call stack
fib(n): O(2ⁿ) — EXPONENTIAL! Each call makes 2 more
gcd(a,b): O(log(min(a,b))) — very fast (halves problem each step)
3. Arrays, Strings & Star Patterns CONFIRMED in previous exam

3.1 1D and 2D arrays

// 1D array — zero-indexed!
int marks[5] = {85, 72, 90, 68, 77};
printf("%d", marks[2]); // 90 (index starts at 0)
printf("%d", marks[5]); // UNDEFINED BEHAVIOUR — out of bounds!

// 2D array — think of it as a table with rows and columns


int matrix[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
printf("%d", matrix[1][2]); // row=1, col=2 → 6

// Sum all elements of a 2D array


int sum = 0;
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
sum += matrix[i][j];

3.2 String operations without library (write from scratch)

In C, a string is a char array terminated by '\0' (null character). The exam may ask you to write strlen, strcpy, strcmp
yourself.
// strlen — count characters until the null terminator
int myStrlen(char s[]) {
int len = 0;
while (s[len] != '\0') len++;
return len;
}

// strcpy — copy src into dest


void myStrcpy(char dest[], char src[]) {
int i = 0;
while (src[i] != '\0') { dest[i] = src[i]; i++; }
dest[i] = '\0'; // always terminate with null!
}

// strcmp — compare two strings; returns 0 if equal


int myStrcmp(char a[], char b[]) {
int i = 0;
while (a[i] != '\0' && a[i] == b[i]) i++;
return a[i] - b[i]; // 0 = equal, negative = a<b, positive = a>b
}

3.3 Star patterns — ALL 5 types (outer loop = rows, inner = columns)

Exam tip: Key formula: row i of the pyramid has (n−i) spaces and (2i−1) stars. Memorise this — examiner may use any n.

// PATTERN 1: Right triangle (n=5 rows)


for(int i=1; i<=5; i++) {
for(int j=1; j<=i; j++) printf("* ");
printf("\n");
}
Output:
*
* *
* * *
* * * *
* * * * *

// PATTERN 2: Inverted triangle


for(int i=5; i>=1; i--) {
for(int j=1; j<=i; j++) printf("* ");
printf("\n");
}

// PATTERN 3: Centred pyramid — spaces THEN stars


for(int i=1; i<=5; i++) {
for(int j=5-i; j>0; j--) printf(" "); // leading spaces = n-i
for(int j=1; j<=2*i-1; j++) printf("*"); // stars = 2*i - 1
printf("\n");
}
Output:
*
***
*****
*******
*********

// PATTERN 4: Diamond = pyramid (rows 1..n) + inverted pyramid (rows n-1..1)

// PATTERN 5: Number triangle


for(int i=1; i<=5; i++) {
for(int j=1; j<=i; j++) printf("%d ", j);
printf("\n");
}
Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Topic What Need to Know


Function Reusable block of code for a specific task.
Declaration Introduces a function before use.
Definition Actual implementation of the function.
Parameters Inputs passed to a function.
Return Value Output returned by a function.
Local Variable Accessible only inside the function.
Global Variable Accessible throughout the program.
Recursion Function calling itself repeatedly.
1D Array Linear collection of same-type elements.
2D Array Array organized in rows and columns.
Traversal Visiting each array element.
Searching Finding a specific element.
Updating Modifying array values.
String Character array ending with null character.
Length Number of characters in a string.
Concatenation Joining two strings.
Comparison Checking whether strings are equal.
Copying Duplicating string contents.
Memory Address Location of data in memory.
Pointer Variable that stores an address.
Address Operator (&) Retrieves memory address.
Dereference Operator (*) Accesses value stored at an address.
Pointer Arithmetic Moving between memory locations.
Array-Pointer Relation Array name acts like a pointer to first element.
Null Pointer Pointer that points to nothing.
4. Pointers & Dynamic Memory Dereferencing · Pointer arithmetic · malloc/calloc/free

4.1 What is a pointer?

A pointer is a variable that stores a memory address instead of a value. The * operator dereferences — it goes to the
value at that address. The & operator takes the address of a variable.
int x = 42;
int *p; // declare pointer to int
p = &x; // p now holds the ADDRESS of x

printf("%d", x); // 42 — value of x


printf("%p", p); // 0x... — address stored in p
printf("%d", *p); // 42 — dereference: value at address p

*p = 100; // change value through pointer


printf("%d", x); // 100 — x is changed!

// Pointer arithmetic — moves by sizeof(type)


int arr[5] = {10,20,30,40,50};
int *q = arr; // q points to arr[0]
q++; // q now points to arr[1]
printf("%d", *(q+2)); // arr[3] = 40

Example: Common exam output trace


int a=5, b=10, *p=&a, *q=&b;
*p = *p + *q; // a = 5 + 10 = 15
p = q; // p now points to b
*p = 20; // b = 20
printf("%d %d", a, b); // Output: 15 20

4.2 Dynamic memory allocation

Function Initialises? Syntax Use when

No (garbage
malloc malloc(n * sizeof(int)) You will fill all values yourself
values)

calloc Yes (all zeros) calloc(n, sizeof(int)) Need zero-initialised array

realloc Preserves old data realloc(ptr, new_bytes) Array needs to grow at runtime

N/A — releases
free free(ptr); ptr=NULL; Always free when done — prevents memory leak
memory

int n = 10;
int *arr = (int*)malloc(n * sizeof(int));
if (arr == NULL) { printf("Allocation failed!"); exit(1); } // always check!

// Use arr like a normal array


for(int i=0; i<n; i++) arr[i] = i*i;

// Grow array to 20 elements


arr = (int*)realloc(arr, 20 * sizeof(int));

free(arr); // release memory when done


arr = NULL; // good practice: prevents dangling pointer
5. Object-Oriented Programming (OOP) Classes, Inheritance, Polymorphism- C++ & Python

5.1 The 4 pillars of OOP — memorise with examples

Pillar One-line definition Robot analogy

Bundle data + methods; hide


Encapsulation Robot motor code is private; you only call public move() method
internals using private/public

Expose only what's necessary; hide


Abstraction You call [Link]() without knowing the FK/IK math inside
implementation complexity

Child class inherits all properties


Inheritance IndustrialRobot inherits from Robot, then adds welding() method
and methods of parent class

Same method name, different


Polymorphism [Link]() spins wheels; [Link]() rotates joints
behaviour per class

5.2 Class and object in C++ (write a full class)

class Robot {
private:
string name; // only accessible inside this class
int dof;

public:
// Constructor — called automatically when object is created
Robot(string n, int d) { name = n; dof = d; }

// Destructor — called when object is destroyed


~Robot() { cout << name << " destroyed\n"; }

// Method (member function)


void display() { cout << name << " has " << dof << " DOF\n"; }

// Getter
int getDOF() { return dof; }
};

// Create objects (instances of the class)


Robot r1("ArmBot", 6);
Robot r2("DeltaBot", 3);
[Link](); // ArmBot has 6 DOF
cout << [Link](); // 3

Python equivalent (for comparison)

class Robot:
def __init__(self, name, dof): # constructor
[Link] = name
[Link] = dof

def display(self):
print(f"{[Link]} has {[Link]} DOF")

r1 = Robot("ArmBot", 6)
[Link]() # ArmBot has 6 DOF
5.3 Inheritance types

// Single: one parent → one child


class Animal { public: void breathe() { cout<<"breathing"; } };
class Dog : public Animal { public: void bark() { cout<<"woof"; } };
Dog d; [Link](); [Link](); // Dog has BOTH methods

// Multilevel: A → B → C (chain)
class Vehicle {};
class Car : public Vehicle {};
class ElectricCar : public Car {}; // inherits from both Vehicle and Car

// Multiple: one child, two parents


class Flyable { public: void fly() {} };
class Swimmable { public: void swim() {} };
class Duck : public Flyable, public Swimmable {};
// Duck can both fly() and swim()

Exam tip: Diamond problem: if B and C both inherit from A, and D inherits from B and C — ambiguity! Solve with virtual
inheritance: class B : virtual public A {}

5.4 Overloading vs Overriding — MUST distinguish

Function Overloading Function Overriding

When resolved Compile-time (static polymorphism) Run-time (dynamic polymorphism)

Where Same class, different parameter lists Parent class + child class, same signature

Keyword needed None virtual in parent (C++)

Example move(int x) and move(int x, int y) Robot::move() and ArmBot::move()

// OVERLOADING — same name, different parameters (compile-time)


void move(int x) { /* move to x */ }
void move(int x, int y) { /* move to (x,y) */ }

// OVERRIDING — virtual function, child redefines (run-time)


class Robot {
public:
virtual void move() { cout << "Generic move\n"; }
};
class ArmBot : public Robot {
public:
void move() override { cout << "Moving arm joints\n"; }
};

Robot *r = new ArmBot();


r->move(); // "Moving arm joints" — dynamic dispatch!

// Pure virtual = abstract class (cannot instantiate)


virtual void move() = 0; // forces every child to define move()
6. Data Structures Stack · Queue · Linked List · BST

6.1 Stack — LIFO (Last In, First Out)

Like a stack of plates: you can only add to and remove from the top.
// Array-based stack
int stack[100], top = -1;

void push(int x) {
if (top == 99) { printf("Stack overflow!"); return; }
stack[++top] = x; // increment top, then store
}
int pop() {
if (top == -1) { printf("Stack underflow!"); return -1; }
return stack[top--]; // return value, then decrement
}
int peek() { return stack[top]; }

// Trace: push(5), push(3), push(8), pop(), peek()


// After pushes: stack=[5,3,8] top=2
// pop() returns 8, top=1
// peek() returns 3 (unchanged)
Real use cases: browser back button | function call stack (recursion) | expression evaluation | undo/redo.

6.2 Queue — FIFO (First In, First Out)

Like a ticket counter queue: first person in is first person served.


int q[100], front=0, rear=-1;

void enqueue(int x) { q[++rear] = x; } // add to rear


int dequeue() { return q[front++]; } // remove from front

// Trace: enqueue(1), enqueue(2), enqueue(3), dequeue()


// Queue: [1,2,3] front=0, rear=2
// dequeue() returns 1, front=1 → Queue: [2,3]
Real use cases: CPU task scheduling | BFS graph traversal | printer jobs | robot sensor data buffer.

6.3 Linked List

A chain of nodes. Each node contains data + a pointer to the next node. Unlike arrays, nodes are NOT contiguous in
memory — they are allocated dynamically.
struct Node {
int data;
struct Node *next;
};

// Create a new node


struct Node* newNode(int data) {
struct Node *n = (struct Node*)malloc(sizeof(struct Node));
n->data = data;
n->next = NULL;
return n;
}

// Insert at head
void insertHead(struct Node **head, int data) {
struct Node *n = newNode(data);
n->next = *head; // new node points to old head
*head = n; // head updated to new node
}
// Traverse and print
void printList(struct Node *head) {
while (head != NULL) {
printf("%d → ", head->data);
head = head->next;
}
printf("NULL\n");
}

Operation Array Linked List

Access by index O(1) — direct calculation O(n) — traverse from head

Insert at middle O(n) — shift all elements right O(1) — just change pointers

Delete from middle O(n) — shift all elements left O(1) — redirect pointers

Memory usage Contiguous, fixed size at creation Non-contiguous, grows dynamically

6.4 Binary Search Tree (BST) + Traversals

BST property: for every node, left child < node < right child. This ordering makes search very fast.
// Insert: 50, 30, 70, 20, 40
// 50
// / \
// 30 70
// / \
// 20 40

// Search: compare target with current node


// - if target < node → go left
// - if target > node → go right
// - if equal → found!
// Time: O(log n) average, O(n) worst (unbalanced tree)

// 3 TRAVERSAL ORDERS — memorise these:


// In-order (Left → Root → Right): gives SORTED output for BST
// Pre-order (Root → Left → Right): used to COPY a tree
// Post-order(Left → Right → Root): used to DELETE a tree

// In-order of above tree: 20, 30, 40, 50, 70 (sorted!)

Exam tip: If you insert already-sorted data (e.g. 10,20,30,40,50) into a BST, it degrades to a linked list with O(n) search.
Balanced BSTs (AVL, Red-Black) fix this — just know the concept.

Topic What Need to Know


Structure User-defined data type containing multiple variables.
Structure Member Individual variable inside structure.
Nested Structure Structure inside another structure.
Array of Structures Collection of similar records.
Structure Pointer Pointer referencing a structure.
typedef Creates custom type names.
Class Blueprint for creating objects.
Object Instance of a class.
Attribute Data associated with an object.
Method Function associated with an object.
Constructor Special method executed during object creation.
Encapsulation Combining data and methods together.
Inheritance Creating new class from existing class.
Polymorphism Same interface, different behavior.
Abstraction Hiding implementation details.
7. Sorting Algorithms Bubble Sort & Heap Sort — CONFIRMED in previous exam

7.1 Bubble Sort — O(n²)

Repeatedly compare adjacent elements and swap if out of order. The largest element 'bubbles up' to its correct
position each pass.
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n-1; i++) { // n-1 passes needed
for (int j = 0; j < n-1-i; j++) { // inner range shrinks each pass
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}

Example: Trace bubble sort on [64, 34, 25, 12, 22]


Pass 1: [34, 25, 12, 22, 64] ← 64 bubbled to end
Pass 2: [25, 12, 22, 34, 64] ← 34 in place
Pass 3: [12, 22, 25, 34, 64] ← 25 in place
Pass 4: [12, 22, 25, 34, 64] ← already sorted
Sorted: [12, 22, 25, 34, 64]

Time complexity:
Best case (already sorted, with flag): O(n)
Average case: O(n²)
Worst case (reverse sorted): O(n²)
Space complexity: O(1) — in-place, no extra memory needed

7.2 Heap Sort — O(n log n) guaranteed

Uses a Max-Heap: a complete binary tree where every parent is >= its children. Stored efficiently in an array.
Max-Heap array indexing rules:
Parent of node i: floor((i-1) / 2)
Left child of i: 2*i + 1
Right child of i: 2*i + 2

Array [16, 14, 10, 8, 7, 9, 3] represents the heap:


16
/ \
14 10
/ \ / \
8 7 9 3 ← valid max-heap (all parents >= children)

void heapify(int arr[], int n, int i) {


int largest = i; // assume root is largest
int left = 2*i + 1;
int right = 2*i + 2;

if (left < n && arr[left] > arr[largest]) largest = left;


if (right < n && arr[right] > arr[largest]) largest = right;

if (largest != i) { // root is NOT the largest


int tmp = arr[i]; arr[i] = arr[largest]; arr[largest] = tmp;
heapify(arr, n, largest); // fix the subtree
}
}

void heapSort(int arr[], int n) {


// Step 1: Build max-heap (start from last non-leaf node)
for (int i = n/2 - 1; i >= 0; i--)
heapify(arr, n, i);

// Step 2: Extract max one by one


for (int i = n-1; i > 0; i--) {
int tmp = arr[0]; arr[0] = arr[i]; arr[i] = tmp; // move max to end
heapify(arr, i, 0); // restore heap on reduced array
}
}

Example: Trace heap sort on [4, 10, 3, 5, 1]


Step 1 — Build max-heap: [10, 5, 3, 4, 1]
Step 2 — Extract max repeatedly:
Swap arr[0]↔arr[4]: [1,5,3,4|10] → heapify → [5,4,3,1|10]
Swap arr[0]↔arr[3]: [1,4,3|5,10] → heapify → [4,1,3|5,10]
Swap arr[0]↔arr[2]: [3,1|4,5,10] → heapify → [3,1|4,5,10]
Swap arr[0]↔arr[1]: [1|3,4,5,10]
Sorted: [1, 3, 4, 5, 10] ✓

7.3 Big-O comparison — ALL sorting algorithms

Algorithm Best case Average case Worst case Space Stable?

Bubble sort O(n) O(n²) O(n²) O(1) Yes

Selection sort O(n²) O(n²) O(n²) O(1) No

Insertion sort O(n) O(n²) O(n²) O(1) Yes

Merge sort O(n log n) O(n log n) O(n log n) O(n) Yes

Quick sort O(n log n) O(n log n) O(n²) O(log n) No

Heap sort O(n log n) O(n log n) O(n log n) O(1) No

Binary search O(1) O(log n) O(log n) O(1) —

Exam tip: Stable sort = equal elements maintain their original relative order. Heap sort and quick sort are NOT stable. Merge
sort always uses O(n) extra space. Heap sort gives O(n log n) in ALL cases with O(1) space — best theoretical choice.

Topic What Need to Know


Stack Linear structure following LIFO principle.
Push Insert element into stack.
Pop Remove top element.
Peek View top element without removing it.
Queue Linear structure following FIFO principle.
Enqueue Insert element at rear.
Dequeue Remove element from front.
Front/Rear First and last positions.
Node Basic unit containing data and link.
Singly Linked List Node points to next node only.
Doubly Linked List Node points forward and backward.
Traversal Moving through nodes sequentially.
Tree Hierarchical data structure.
Root Top node of tree.
Parent/Child Relationship between nodes.
Heap Specialized tree used in priority operations.
Max Heap Parent always larger than children.
Min Heap Parent always smaller than children.
8. Deep Learning, Transformer & YOLO — how ChatGPT/Claude works

8.1 Neural network basics

Layer Role Example

Input layer Receives raw data 784 neurons for 28×28 pixel MNIST digit image

Hidden layers Learn hierarchical features Layer 1: edges → Layer 2: shapes → Layer 3: objects

Output layer Makes final prediction 10 neurons = probabilities for digits 0–9

Neuron output: y = activation( Σ wᵢxᵢ + b )


where:
xᵢ = inputs (from previous layer)
wᵢ = learned weights (strength of each connection)
b = bias term (threshold shift)
activation = non-linear function

Most common activation — ReLU:


ReLU(x) = max(0, x)
If x is negative → output 0
If x is positive → pass through unchanged

Training = adjusting all weights using:


Backpropagation + Gradient Descent → minimise Loss function (error)

8.2 Transformer architecture — how ChatGPT and Claude work

Introduced in the 2017 paper 'Attention is All You Need'. Replaced RNNs with a mechanism called self-attention,
which allows the model to look at the entire input sequence simultaneously.

Component What it does Why it matters

Each word looks at ALL other words 'Bank' in 'river bank' vs 'bank account' — different
Self-attention
to understand context meaning based on context

Encodes the position of each word in


Positional encoding Attention has no built-in concept of order; this adds it
the sequence mathematically

Multiple attention mechanisms run in Each head learns different aspects: syntax, semantics,
Multi-head attention
parallel coreference

Dense neural network applied to


Feed-forward layers Adds expressive power; transforms attended features
each position after attention

Normalises activations after each


Layer normalisation Stabilises training of very deep networks
sub-layer

Attention formula:
Attention(Q, K, V) = softmax( Q × Kᵀ / √dₖ ) × V

where:
Q = Query matrix (What am I looking for?)
K = Key matrix (What does each token contain?)
V = Value matrix (What information do I return?)
dₖ = dimension of key vectors (scaling prevents vanishing gradients)

Simplified: each word QUERIES all others, scores similarity (dot product),
normalises with softmax, then takes a weighted sum of their VALUES.
Example: GPT vs BERT — two types of transformers
GPT (like ChatGPT, Claude):
→ Decoder-only transformer
→ Trained to predict the NEXT word
→ Generates text left-to-right (auto-regressive)
→ Use cases: chatbots, code generation, text completion

BERT (Google Search):


→ Encoder-only transformer
→ Reads the WHOLE sentence at once (bidirectional attention)
→ Use cases: text classification, search ranking, Q&A

8.3 YOLO — You Only Look Once (object detection)

YOLO processes the entire image in a single forward pass of a CNN, outputting bounding boxes and class labels
simultaneously. This makes it extremely fast — critical for real-time robot vision.
How YOLO works (one forward pass):
1. Divide image into S×S grid cells (e.g. 13×13, 26×26, 52×52)
2. Each cell predicts: bounding box (x,y,w,h) + confidence score + class probabilities
3. Non-Maximum Suppression (NMS) removes duplicate detections
4. Output: all objects with class labels, bounding boxes, confidence scores

vs. older R-CNN approach:


R-CNN: region proposals first → classify each region separately → SLOW
YOLO: entire image in ONE pass → ALL detections simultaneously → FAST

Feature YOLO v5 YOLO v6

Released by Ultralytics (2020) Meituan (2022)

Backbone CSPNet EfficientRep

Detection head Anchor-based Anchor-free (simpler, more flexible)

Target use General purpose, research-friendly Industrial deployment, edge hardware

Widely adopted, well-documented, Faster inference, better for


Key advantage
easy to fine-tune production/embedded

Better (anchor-free handles varied sizes


Arbitrary object sizes Good
naturally)

Example: Robotics application connecting Robotics + Vision + Programming


A pick-and-place robot vision pipeline:
1. Camera captures image of conveyor belt
2. YOLO v6 detects objects: class=bottle, bbox=[x,y,w,h], confidence=0.94
3. Bounding box centre → 3D world coordinate (via camera calibration matrix)
4. Robot IK: compute joint angles to reach that 3D position
5. PID-controlled servo motors execute the motion
6. Gripper closes → object picked

This connects: Computer Vision + Programming (CNN/YOLO) + Robotics (IK + PID)


Topic What Need to Know
Linear Search Checks elements one by one.
Binary Search Repeatedly divides sorted data in half.
Bubble Sort Repeatedly swaps adjacent elements.
Selection Sort Selects smallest element each pass.
Insertion Sort Inserts elements into sorted portion.
Merge Sort Divide-and-conquer sorting method.
Heap Sort Uses heap structure for sorting.
Time Complexity Measures execution time growth.
Space Complexity Measures memory usage growth.
Big-O Notation Mathematical representation of efficiency.
O(1) Constant time.
O(log n) Logarithmic growth.
O(n) Linear growth.
O(n log n) Efficient sorting complexity.
O(n²) Quadratic growth, often nested loops.
Pattern Problems Use loops to generate patterns.
Number Problems Prime, palindrome, factorial, etc.
Array Problems Traversal, searching, counting.
String Problems Manipulation and analysis.
Logic Building Breaking problems into smaller steps.
Microcontroller Small programmable computer used in robots.
Sensor Device that collects information from environment.
Actuator Device that produces motion or action.
Embedded System Hardware and software designed for a specific task.
GPIO Pins used for input/output communication.
ADC Converts analog signals into digital data.
DAC Converts digital signals into analog signals.
Control Loop Continuous sensing, decision-making, and action process.
Real-Time System System that must respond within strict timing constraints.
Quick Reference Summary
Topic What to always know Exam importance

Data types Sizes (int=4B, char=1B, double=8B); 5/2=2 integer division ★★☆

Operators x++ vs ++x; &=bitwise AND vs &&=logical AND ★★★

Control flow for/while/do-while differences; switch with break ★★☆

Recursion Factorial, Fibonacci, GCD — write from memory ★★★

Star patterns Right triangle, pyramid, diamond — outer=rows, inner=cols ★★★

Strings myStrlen, myStrcpy, myStrcmp — write without string.h ★★★

& = address, * = dereference; pointer arithmetic steps by


Pointers ★★★
sizeof(type)

Dynamic memory malloc (no init) vs calloc (zeros); always free at end ★★☆

Encapsulation, Abstraction, Inheritance, Polymorphism +


OOP 4 pillars ★★★
example each

Overloading vs overriding Compile-time vs run-time; virtual keyword ★★★

Stack LIFO; push/pop; overflow at top=MAX; underflow at top=-1 ★★☆

Queue FIFO; enqueue at rear/dequeue from front ★★☆

Linked list Node struct; insert at head; traverse to NULL ★★☆

BST traversals In-order=sorted; Pre=copy; Post=delete ★★☆

Bubble sort Adjacent swap; O(n²); stable; inner loop shrinks ★★★

Heap sort Max-heap; heapify; O(n log n) ALL cases; O(1) space ★★★

Transformer Self-attention; Q/K/V formula; GPT=decoder; BERT=encoder ★★★

YOLO v5 vs v6 v5=anchor-based/general; v6=anchor-free/industrial ★★★

You might also like