Introduction to Algorithms and Programming Concepts
What is an Algorithm?
An algorithm is a finite and ordered set of instructions that lead to the solution of a problem. We encounter algorithms in everyday life. For example, when installing a stereo, we execute the instructions contained in the equipment manual. This instruction set is an algorithm. Another example of a mathematical algorithm is Euclid’s algorithm, used to obtain the greatest common divisor of two numbers.
Pseudocode
Pseudocode is a language used to write algorithms in a way that resembles programming languages but without strict syntax rules. It’s an imitation of programming languages like Pascal, Java, C, and C++. In this context, we’ll focus on pseudocode that resembles Java, C, and C++.
The purpose of pseudocode is to allow the programmer to focus on the logical aspects of the solution, avoiding the strict syntax rules of programming languages.
Variables
A variable is a location in main memory that stores a value that can change during program execution. When a program needs to store data, it needs a variable. Every variable has a name, data type, and a value. Before you can use a variable, it’s necessary to declare it by specifying its name and data type.
Literals
Literals are the values shown in the pseudocode and can include:
- Integer literals
- Real literals
- Character literals
- String literals
- Logical literals
Basic Algorithm Instructions
There are three basic algorithm instructions:
Input
This involves obtaining input from an input device, such as the keyboard, and storing it in a variable. In pseudocode, the action of adding data to a variable is often expressed by the word READ.
Output
This involves displaying the value of a variable on an output device, such as the display. In pseudocode, the action of showing the value of a variable is often expressed by the word WRITE.
Assignment
This involves assigning the value of an expression to a variable. The expression can be a simple variable, a single literal, or a combination of variables, literals, and operators. The assignment is expressed using an equal sign (=).
Rules of Arithmetic Operators
When an arithmetic expression has more than one arithmetic operator, the order of application of the operators follows a precise order determined by the rules of hierarchy of arithmetic operators.
Logical Expressions
A logical expression combines variables, literals, arithmetic operators, relational operators, and logical operators.
Control Structures
Sequential Structure
A sequential structure is one in which the instructions are placed one after the other along a single sequence, without rerouting. The sequential structure has one input and one output.
Selection Structures
Simple IF Structure
The simple selection structure allows you to run an action or set of actions only if a certain condition is met.
IF-THEN-ELSE Structure
The double-selection structure allows you to select one of two possible routes based on the truth or falsity of a condition.
Cascading IF-THEN-ELSE Structure
This structure is formed by cascading several IF-THEN-ELSE structures placed one after another. The conditions are evaluated in descending order. The action corresponding to the first true condition is executed, and the rest of the structure is skipped. If all conditions are false, the action associated with the final ELSE is executed.
CASE or SWITCH Structure
The CASE (or SWITCH) structure allows you to choose one route from among several possible routes, based on the value of a variable that acts as a selector.
Repetitive Structures (Loops)
Repetitive structures are used when you want a set of instructions to execute a certain number of times. There are two main types:
WHILE Loop
This loop repeats the loop body as long as a certain condition is met.
REPEAT-UNTIL Loop
This loop is similar to the WHILE loop, but the condition is checked at the end of the loop body. This means the loop body will always execute at least once.
Counters and Accumulators
Counter
A counter is a variable intended to hold values that are increasing or decreasing each time you run the action that contains it. The increase or decrease is called a step counter and is always constant.
Accumulator
An accumulator is a variable that allows us to store a value that increases or decreases in a non-constant amount during the process.