Skip to content

šŸ’» Programming for Problem Solving — C (100111)

ā¬…ļø Back to Semester-1 | šŸ  Home

šŸ’” Why this subject? C is the "mother language" — once you know pointers, memory, and functions here, Java, Python, and DSA (Sem 3) become much easier.


šŸ“Œ Unit 1: Introduction to Programming

  • Computer system basics: CPU executes instructions, RAM stores running program/data, disk stores permanently.
  • Algorithm: step-by-step plan to solve a problem (language-independent).
  • Flowchart vs Pseudocode: flowchart = visual diagram; pseudocode = plain-English-like steps.
  • Compiler vs Interpreter: C uses a compiler — converts the whole source code to machine code (.exe) before running.
  • Syntax Error (broken grammar, caught at compile time) vs Logical Error (code runs but gives wrong answer).

šŸ“ Example — Algorithm to find the largest of 2 numbers:

1. Start
2. Read a, b
3. If a > b, print a
4. Else print b
5. Stop

#include <stdio.h>
int main() {
    int a = 10, b = 25;
    if (a > b) printf("%d is largest", a);
    else printf("%d is largest", b);
    return 0;
}

šŸ“Œ Unit 2: Operators, Conditional Branching & Loops

Operator type Examples
Arithmetic + - * / %
Relational == != > < >= <=
Logical && \|\| !
Bitwise & \| ^ ~ << >>
  • if / else if / else: branching based on conditions.
  • Loops: for (known iterations), while (condition-checked first), do-while (runs at least once).

šŸ“ Example — Sum of first N natural numbers:

#include <stdio.h>
int main() {
    int n = 10, sum = 0;
    for (int i = 1; i <= n; i++) {
        sum += i;
    }
    printf("Sum = %d", sum);   // Sum = 55
    return 0;
}

🧠 Quick Recall: && and || short-circuit — if the first condition already decides the result, the second isn't even checked.


šŸ“Œ Unit 3: Arrays and Strings

  • Array: a fixed-size collection of same-type elements stored contiguously in memory.
  • 2D Array: array of arrays — used for matrices/grids.
  • String in C: just a char array ending in '\0' (null character).

šŸ“ Example — Reverse a string:

#include <stdio.h>
#include <string.h>
int main() {
    char str[] = "hello";
    int len = strlen(str);
    for (int i = len - 1; i >= 0; i--)
        printf("%c", str[i]);   // olleh
    return 0;
}

āš ļø Common beginner bug: C does not check array bounds — arr[10] on a 5-element array won't crash immediately, it'll silently corrupt memory. Always track sizes carefully!


šŸ“Œ Unit 4: Functions, Recursion & Pointers

  • Function: reusable named block of code. Call by value: function gets a copy of the argument (original unchanged).
  • Recursion: a function calling itself, with a base case to stop it.
  • Pointer: a variable that stores the memory address of another variable.
  • int *p = &x; → p points to x
  • *p → dereference, gives the value stored at that address
  • Call by reference (via pointers): function can modify the original variable.

šŸ“ Example — Factorial using recursion:

int factorial(int n) {
    if (n == 0) return 1;        // base case
    return n * factorial(n - 1); // recursive case
}
// factorial(5) = 5*4*3*2*1 = 120

šŸ“ Example — Swap using pointers (call by reference):

void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}
// swap(&x, &y) actually changes x and y

🧠 Quick Recall: Pointers are the reason C is fast & low-level — they're literally addresses, like a house number pointing to where data lives.


šŸ“Œ Unit 5: User-Defined Data Types & File Handling

  • struct: groups different data types under one name.
    struct Student {
        char name[20];
        int roll;
        float marks;
    };
    struct Student s1 = {"Raj", 101, 89.5};
    printf("%s scored %.1f", s1.name, s1.marks);
    
  • union: like struct, but all members share the same memory (only one member valid at a time) — saves memory.
  • File Handling: fopen(), fclose(), fprintf(), fscanf(), modes: "r" read, "w" write, "a" append.

šŸ“ Example — Writing to a file:

FILE *fp = fopen("data.txt", "w");
fprintf(fp, "Hello File!");
fclose(fp);


šŸ“Œ Unit 6: Basic Algorithms

  • Linear Search: check each element one by one — O(n).
  • Bubble Sort: repeatedly swap adjacent elements if out of order — simple but slow O(n²).
  • Insertion Sort: build the sorted array one element at a time.
  • Selection Sort: repeatedly pick the minimum and place it at the front.

šŸ“ Example — Bubble Sort:

void bubbleSort(int arr[], int n) {
    for (int i = 0; i < n - 1; i++)
        for (int j = 0; j < n - i - 1; j++)
            if (arr[j] > arr[j + 1]) {
                int t = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = t;
            }
}


šŸ”¬ Lab Programs (Programming for Problem Solving Lab)

  1. Familiarization with programming environment
  2. Arithmetic expression problems
  3. if-then-else problems
  4. Iterative problems (sum of series)
  5. 1D array manipulation (search/sort)
  6. 2D array & string operations
  7. Simple functions
  8. Numerical methods (root finding, differentiation, integration)
  9. Recursive functions
  10. Pointers and structures
  11. File operations

āœ… Quick Revision Table

Topic One-line memory hook
Compiler Translates whole code before running
Array Fixed size, no bounds checking in C
String char array ending in '\0'
Recursion Needs a base case or it never stops
Pointer Stores an address, not a value
struct Different types together, separate memory
union Different types together, shared memory
Bubble Sort O(n²) — swap adjacent if out of order