MODULE 4 NOTES:
Modular Programming and Problem Solving
1. Modular Programming
A programming method where a program is divided into smaller, manageable
modules or functions.
Makes code easy to understand, debug, test, and reuse.
Example:
To calculate the area of a rectangle:
float area(float length, float width) {
return length * width;
2. Top-Down Approach
Start with the main problem and divide it into sub-problems (functions).
Used in structured programming.
3. Bottom-Up Approach
Start by writing small modules and then combine them to form the full program.
Used in object-oriented programming.
4. Recursion
A function that calls itself to solve smaller versions of a problem.
Must have a base case to stop recursion.
Example: Factorial using recursion
int factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n - 1);
}
Problems on Arrays
5. Reading and Writing Array Elements
Example:
int arr[5];
for (int i = 0; i < 5; i++)
scanf("%d", &arr[i]);
for (int i = 0; i < 5; i++)
printf("%d ", arr[i]);
6. Maximum and Minimum
Example:
int max = arr[0], min = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) max = arr[i];
if (arr[i] < min) min = arr[i];
7. Sum and Average
Example:
int sum = 0;
for (int i = 0; i < n; i++)
sum += arr[i];
float avg = (float)sum / n;
8. Median
Sorted array: Middle element (or average of two middle elements).
Example:
if (n % 2 == 0)
median = (arr[n/2 - 1] + arr[n/2]) / 2.0;
else
median = arr[n/2];
9. Mode
Number that occurs most frequently.
Example: (Manual counting method using nested loops)
10. Sequential (Linear) Search
Example:
for (int i = 0; i < n; i++) {
if (arr[i] == key) {
printf("Found at %d", i);
break;
11. Binary Search
Sorted array required.
Divide search space in half.
Example:
int low = 0, high = n - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == key) { printf("Found"); break; }
else if (key < arr[mid]) high = mid - 1;
else low = mid + 1;
12. Any One Sorting Algorithm – Bubble Sort
Example:
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp;
13. Matrix Operations
a. Addition:
for (i = 0; i < rows; i++)
for (j = 0; j < cols; j++)
c[i][j] = a[i][j] + b[i][j];
b. Multiplication:
CopyEdit
for (i = 0; i < r1; i++)
for (j = 0; j < c2; j++)
for (k = 0; k < c1; k++)
c[i][j] += a[i][k] * b[k][j];
C Language Concepts
14. Function Declaration and Definition (Prototype)
Declaration (Prototype):
int add(int, int);
Definition:
int add(int a, int b) {
return a + b;
15. Role of Return Statement
Sends the result back to the calling function.
return value;
16. One-Dimensional Arrays
int a[5] = {1, 2, 3, 4, 5};
17. Two-Dimensional Arrays
int b[2][2] = {{1,2}, {3,4}};
18. String Functions
a. strlen(s): Length of string
b. strcpy(dest, src): Copy string
c. strcat(s1, s2): Concatenate
d. strcmp(s1, s2): Compare
19. Other Operators
Arithmetic: +, -, *, /, %
Relational: ==, !=, <, >, etc.
Logical: &&, ||, !
Bitwise: &, |, ^, ~, <<, >>
Assignment: =, +=, -=, etc.
Conditional (Ternary): ? :
20. Operator Precedence and Associativity
Determines the order of operation.
Example:
int a = 5 + 2 * 3; // * has higher precedence → a = 5 + 6 = 11
21. Debugging
Process of finding and fixing errors (bugs) in a program.
Tips:
Use printf() to trace variables.
Use a debugger tool (like gdb).
Check for syntax, logical, and runtime errors.