Computer Algorithms  «Prev  Next»
Lesson 2Computer programs
ObjectiveDefine the elements of computer programs.

Define Computer Programs

Today computers are almost everywhere.
They are in
  1. our cars,
  2. our homes,
  3. our workplaces, and
  4. embedded into our cerebral cortexes.

  • Algorithms
    When we write a computer program, we are generally implementing a method that has been devised previously to solve some problem. This method is often independent of the particular programming language being used, it is likely to be equally appropriate for many computers and many programming languages. It is the method, rather than the computer program itself, that specifies the steps that we can take to solve the problem. The term algorithm is used in computer science to describe a finite, deterministic, and effective problem-solving method suitable for implementation as a computer program. Algorithms are the stuff of computer science: they are central objects of study in the field. We can define an algorithm by describing a procedure for solving a problem in a natural language, or by writing a computer program that implements the procedure, as shown at right for Euclid's algorithm for finding the greatest common divisor of two numbers, a variant of which was devised over 2,300 years ago.

Relationship Between Structured Programs and Algorithms**

Structured programming and algorithms are closely related concepts in computer science and software development. Their relationship can be understood as follows:
  1. Algorithms Define the Logic, Structured Programming Implements It
    • An algorithm is a step-by-step procedure or set of rules to solve a problem. It defines the logic behind solving a task, such as sorting a list or searching for an element.
    • Structured programming provides a way to implement algorithms using well-defined control structures such as sequence, selection (if-else, switch), and iteration (loops).
  2. Structured Programming Provides Clarity and Maintainability
    • Algorithms can be implemented using any programming paradigm, but structured programming ensures that the implementation is readable, maintainable, and efficient.
    • It avoids the use of goto statements and promotes modular programming (functions, procedures, and subroutines).
  3. Algorithms Are Independent of Programming Languages
    • An algorithm can be designed without considering a specific programming language. However, structured programming allows its systematic translation into a program using languages like C, Java, or Python.
  4. Efficiency and Optimization
    • Structured programming helps in writing efficient implementations of algorithms, optimizing their execution in terms of time and space complexity.
  5. Code Reusability and Modularity
    • Using structured programming principles, algorithms can be broken down into reusable functions and modules, making the program easier to debug, modify, and extend.

Example: Sorting Algorithm in Structured Programming**
Consider an algorithm for sorting numbers and its structured programming implementation in C++.
Algorithm (Bubble Sort)**
  1. Compare adjacent elements.
  2. Swap if they are in the wrong order.
  3. Repeat until the list is sorted.

C++ Implementation (Structured Program)**
#include <iostream>
using namespace std;

// Function to implement 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]) { 
                swap(arr[j], arr[j + 1]); 
            }
        }
    }
}

// Function to print an array
void printArray(int arr[], int n) {
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
    cout << endl;
}

int main() {
    int arr[] = {5, 3, 8, 4, 2};
    int n = sizeof(arr) / sizeof(arr[0]);

    cout << "Original Array: ";
    printArray(arr, n);

    bubbleSort(arr, n);

    cout << "Sorted Array: ";
    printArray(arr, n);

    return 0;
}

Conclusion:
  • Algorithm defines the logic (sorting technique).
  • Structured programming organizes the implementation using functions (bubbleSort() and printArray()).
  • This results in a clear, modular, and maintainable program.

Computers fly aircraft, make perfect toast, and balance our checkbooks. There are very few parts of our lives where computers are not in some way useful. Some computers fill an entire room and perform highly complex tasks, such as modeling the Earth's atmosphere, while others fit in your pocket and to calculate compound interest. Despite the tremendous range in capability and responsibilities of modern computers, they all have one thing in common: they need to be told how to do their job. Computers are told how to perform a task or solve a problem through a set of instructions. The set of instructions used in a computer to bring about a specific result is called a computer program; the process of creating a computer program is called computer programming. Computer programs are often referred to as application programs or simply applications. In many respects, a computer program is very much like a recipe. Just as a recipe tells the cook what ingredients and tools to use and what steps to carry out to prepare a dish, a computer program tells the computer what resources to use and what sequence of instructions to execute in order to achieve the objectives of the program.
Now that you have a general understanding of what a computer program is and what it does, continue on to the next lesson to learn the basic components of a computer.

SEMrush Software