Both structured programming and modular programming are programming paradigms aimed at improving the efficiency, maintainability, and clarity of code. These concepts focus on how programs are organized and written, ensuring that the logic is clear, reusable, and easy to modify.
Let's break down each of these concepts:
Structured programming is a programming paradigm that emphasizes breaking down a program into smaller, manageable components and using control structures to organize the flow of execution. This methodology helps in making code more readable, easier to debug, and simpler to maintain.
Sequential Execution:
int x = 10;
int y = 5;
int sum = x + y;
printf("Sum: %d", sum);
Selection (Conditional Statements):
if, else, switch, etc.int a = 5;
if (a > 0) {
printf("a is positive");
} else {
printf("a is non-positive");
}
Repetition (Loops):
for, while, and do-while.for (int i = 0; i < 5; i++) {
printf("%d ", i);
}
Top-Down Approach:
Avoiding Unstructured Flow (GOTO Statements):
Here's a simple example that sums up an array of numbers using structured programming:
#include <stdio.h>
int main() {
int numbers[] = {1, 2, 3, 4, 5};
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += numbers[i]; // Add each number to sum
}
printf("Sum is: %d\n", sum);
return 0;
}
In this program:
for) helps us repeat the action of adding each number to sum.Modular programming is a programming paradigm that focuses on dividing a program into separate, independent modules (or functions), each of which is designed to perform a specific task. This approach makes code more reusable, easier to maintain, and reduces redundancy.
Modularity:
int add(int x, int y) {
return x + y; // This function adds two numbers
}
Functions (or Procedures):
#include <stdio.h>
// Function to add two numbers
int add(int x, int y) {
return x + y;
}
// Function to subtract two numbers
int subtract(int x, int y) {
return x - y;
}
int main() {
int a = 10, b = 5;
int sum = add(a, b); // Reusing the add function
int diff = subtract(a, b); // Reusing the subtract function
printf("Sum: %d\n", sum);
printf("Difference: %d\n", diff);
return 0;
}
Encapsulation:
Reusability:
add function from the previous example can be reused anywhere else in the program whenever you need to add two numbers.Separation of Concerns:
Structured programming and modular programming complement each other well:
Together, they help in writing clean, efficient, and maintainable code.
Here’s an example of a C program that combines structured and modular programming to calculate the sum and average of an array of numbers:
#include <stdio.h>
int sum(int arr[], int size) {
int total = 0;
for (int i = 0; i < size; i++) {
total += arr[i];
}
return total;
}
float average(int arr[], int size) {
return (float)sum(arr, size) / size;
}
int main() {
int numbers[] = {1, 2, 3, 4, 5};
int size = sizeof(numbers) / sizeof(numbers[0]);
int total = sum(numbers, size); // Using modular functions
float avg = average(numbers, size); // Reusing sum function inside average
printf("Sum: %d\n", total);
printf("Average: %.2f\n", avg);
return 0;
}
In this program:
sum and average).Open this section to load past papers