Your programming assistant #5: understanding algorithm and their uses
We now get how control flow work in programming languages, which enables us to read, write and understand chunks of code. But when we talk about writting software, specially complex ones, it is not just a bunch of code randomly written into files. There are some structures that have been standardized over the years, which makes it simple to create implementations that are both dynamic and reusable. To understand those structures, first we need to understand algorithms.
Definition
Algorithms are a set of instructions or rules used to solve a specific problem or perform a particular task. In programming languages, algorithms are used to describe the logic behind a program’s functionality.
An algorithm can be implemented using any programming language, and the language used often depends on the nature of the problem being solved. Some programming languages are better suited to specific types of algorithms or tasks.
Algorithms are often expressed using pseudocode, which is a high-level description of the steps involved in solving a problem. Pseudocode is not tied to any specific programming language and is used to express the general logic of an algorithm.
What is a pseudo code?
Pseudocode is a way of expressing the logic or steps involved in solving a problem or performing a task in a high-level, informal way. It is not tied to any specific programming language syntax, but instead uses plain English or other natural language to describe the steps of an algorithm.
They are very useful to teach and practice programming logic without having to deal with language specificities at the same time.
Pseudocode typically consists of a sequence of steps or instructions, each of which describes an action or decision that needs to be taken in order to achieve a specific goal. These steps can include loops, conditionals, and other programming constructs, but they are expressed in a way that is easily understandable by both technical and non-technical individuals.
Here is an example of pseudocode for a simple program that adds two numbers:
BEGIN
GET first number from user
GET second number from user
SET sum = first number + second number
PRINT "The sum is " + sum
END
As you can see, this pseudocode is not tied to any specific programming language syntax, but it clearly outlines the steps involved in adding two numbers and displaying the result to the user.
To exemplify that, let's see this implementation in two different languages (Python and Javascript):
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
sum = num1 + num2
print("The sum is", sum)
const num1 = parseInt(prompt("Enter the first number: "));
const num2 = parseInt(prompt("Enter the second number: "));
const sum = num1 + num2;
console.log(`The sum is ${sum}`);
In both implementations, the code starts by getting two numbers from the user as input. In Python, the built-in input
function is used to read user input as a string, and the int
function is used to convert the input to an integer. In JavaScript, the prompt
function is used to display a prompt to the user and read their input as a string, and the parseInt
function is used to convert the input to an integer.
Next, the code calculates the sum of the two numbers by simply adding them together, and stores the result in a variable called sum
.
Finally, the code displays the result to the user using the print
function in Python, and the console.log
function in JavaScript.
How are algorithms represented in programming languages?
Algorithms can be represented in programming languages using a variety of techniques and structures. Some common ways of representing algorithms in programming languages include:
- Functions and procedures: Algorithms can be encapsulated within functions or procedures, which are reusable blocks of code that perform a specific task. Functions and procedures can be called from other parts of the program as needed, making them a convenient way to organize and modularize code.
- Control structures: Control structures such as loops and conditional statements are used to control the flow of execution in an algorithm. For example, a loop can be used to iterate through a set of data, while a conditional statement can be used to test for a particular condition and execute different code depending on the result.
- Data structures: Algorithms often involve the manipulation and processing of data, and data structures such as arrays, linked lists, and trees can be used to represent and organize this data. Data structures provide a way to store and access data in a structured and efficient manner, which is often essential for implementing complex algorithms.
- Recursion: Recursion is a technique that involves solving a problem by breaking it down into smaller subproblems and then solving those subproblems recursively. Recursion can be a powerful tool for solving complex problems, but it requires careful consideration of the base case and termination conditions to avoid infinite loops.
- Object-oriented programming: Object-oriented programming is a programming paradigm that emphasizes the use of objects, which are instances of classes that encapsulate data and behavior. Algorithms can be implemented using object-oriented programming techniques, with objects representing the data and operations involved in the algorithm.
Overall, algorithms can be represented in programming languages using a wide variety of techniques and structures, depending on the specific needs and requirements of the algorithm and the programming language being used.
In the next steps we will understand how to use and get the benefits from writing algorithms while programming.