š» 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:
#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
chararray 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;āppoints tox*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.
- 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:
š 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)¶
- Familiarization with programming environment
- Arithmetic expression problems
- if-then-else problems
- Iterative problems (sum of series)
- 1D array manipulation (search/sort)
- 2D array & string operations
- Simple functions
- Numerical methods (root finding, differentiation, integration)
- Recursive functions
- Pointers and structures
- 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 |