Basic Fundamentals of Algorithms
Basic Fundamentals of Algorithms
School of Engineering
Professional School of Systems Engineering Algorithms and Programming
A computer does not have the capacity to solve problems except when it is given
they provide the successive steps to be taken, for this we prepared an algorithm.
What is an algorithm?
It is a method for solving a problem through a sequence of instructions, each one of
which specifies the operations that the computer must perform.
Characteristics of an algorithm
The fundamental characteristics that every algorithm must fulfill are:
The algorithm serves as the basis for generating a program, but it is not the program itself.
The algorithm is not directly intelligible by the computer.
The same algorithm can be implemented differently in various programs, it is
Say, two programmers can obtain different source codes from the same one.
algorithm.
Entry Exit
Process
The information provided to the algorithm constitutes its input, the procedure for the
The solution to the problem constitutes its subprocess and the information produced by the algorithm.
constitutes its exit.
Example 01:
Read the base and height of a parallelogram. Calculate its area.
1
Catholic University of the Angels of Chimbote
School of Engineering
Professional School of Systems Engineering Algorithms and Programming
Example 02:
Read the radius of a circle. Calculate its area and circumference.
The most common way to check an algorithm is by manually executing it using data.
significant that encompass the entire possible range of values and noting it down on a sheet of paper the
values that are taken in the different phases, the input or auxiliary data and, finally the
values of the results. This process is known as algorithm testing.
desk.
The basic elements that are an essential or fundamental part of an algorithm or program are:
Identifiers.
Variables.
Constants.
Instructions.
Reserved words.
Identifiers
These are names used to identify objects and other elements of a program: variables,
constants, operators, functions, procedures, etc. There are rules for building
valid identifiers are as follows:
2
Catholic University of the Angels of Chimbote
Faculty of Engineering
Professional School of Systems Engineering Algorithms and Programming
Variables
A variable is an object to which a value is assigned and whose value can change during the
development of the algorithm or execution of the program. If a variable is composed of two
words, these can be together or connected by the underscore, hyphen, or subhyphen '_'.
Note:
Reserved words of the language should not be used as variable names (identifiers).
algorithm or programming language.
Examples:
● Note ● Last_name ● prices
● Name ● mother's last name ● hour
● Surnames ● Note 1 ● prom
● oral note ● Counter1 ● pi
The names of the chosen variables must be meaningful and related to the object that
they represent, such as the following cases:
Constants
A constant takes or receives a value that does not change during the development of the algorithm or the
execution of the program.
Instructions
The instructions specify the operations or actions that must be performed or executed.
Reserved words
Words used by algorithms or programming languages to represent statements,
orders, etc.
Examples:
● start.
● end.
● to do.
● if–then–else.
● while.
● since.
3
Catholic University of the Angels of Chimbote
Faculty of Engineering
Professional School of Systems Engineering Algorithms and Programming
Data Types
The primary objective of any computer is the management of information or data. This data
They can be the sales figures of a winery, tickets for a sports event, daily income.
of a company or the grades of a classroom. The algorithms and programs
corresponding operate on data.
Data types define a set of values that a variable can store, along with a
set of operations that can be performed on that variable.
Numerical data
The numeric type is the set of numeric values. These can be represented in two
different forms:
Integer type
It is the finite subset of integers, which do not have fractional or decimal components.
and can be negative or positive.
Examples:
6 -9
-20 11
5 19
15 -1
16 0
Real types
Subset of real numbers, always has a decimal point and can be positive or
negatives. They consist of an integer and a decimal part.
Examples:
9.9 12.24
-123.11 0.89
1470.274 -0.99
54.77 3.0
Logical data
It is that data that can only take one of two values:
true y false
Example:
When asked if an integer is even, the answer will be true or false, depending on whether it is odd.
or the part.
4
Catholic University of the Angels of Chimbote
School of Engineering
School of Professional Systems Engineering Subject: Algorithms and Programming
The characters recognized by different computers are not standard, however the
majority recognizes the following alphabetical, numerical, and special characters:
From Example 01, it is concluded that the existing data types are:
base
From Example 02, it is concluded that the existing data types are:
whole :radio.
real:area, length and pi.
367520100000000000000
367,520,100,000,000,000
3.675201 x 1020
Examples:
5
Catholic University of the Angels of Chimbote
Faculty of Engineering
Professional School of Systems Engineering Subject: Algorithms and Programming
Expressions
They are combinations of constants, variables, operation symbols, parentheses, and names of
special functions that meet specific rules.
Examples:
.a+(b*3)/c
.5*a*b/(c+d)
1. ARITHMETIC EXPRESSIONS
They are analogous to mathematical formulas. The variables and constants are numerical (real or
whole) and the operations are the arithmetic ones.
Arithmetic Operators
Meaning Algebraic Operator Algorithmic Operator
Sum + +
Stay - -
Multiplication x *
Real division / /
Integer division / div
Exponentiation **,^
Modulo (remainder or mod
residue)
Table 1: Arithmetic Operators
The following will show the equivalences you should know between algebraic expressions and
arithmetic expressions.
Examples:
7 ( a + b )
b) 8x + 9y + 3z
c) a2+ 2a - 3
d) x2- y2
e) p + q
r+s
t
f) a (cd)
bb
6
Catholic University of the Angels of Chimbote
School of Engineering
Professional School of Systems Engineering Algorithms and Programming
Solution:
7 ( a + b ) 7 * (a + b)
b) 8x + 9y + 3z 8*x+9*y+3*z
c) a2+ 2a - 3 a squared plus 2 times a minus 3
e) p + q (p + q) / (r + s / t)
r+s
t
f) a (cd) a/b*c*d
b
a) 15 + 9 * 2 – 6 * 2 ^ 2
b) -8 / 4 * 6 + 3 ^ 2 * (10 / 5)
Note: To execute the steps on how to solve these arithmetic expressions, we must first
know the rules of priority
Priority rules
Expressions that have two or more operands in an expression are generally evaluated according to
the following order.
1. The operations that are enclosed in parentheses are evaluated first. If there are
different nested parentheses (one inside the other), the innermost expressions
they evaluate first.
2. Arithmetic operations within an expression usually follow the following order of
priority.
Solution to exercise 2:
a) 15 + 18 - 24 b) -8 / 4 * 6 + 3 ^ 2 * (10 / 5)
15 + 9 * 2 – 6 * 4 -8 / 4 * 6 + 3 ^ 2 * 2
15 + 18 24 = 9 -8 / 4 * 6 + 9 2
-2 * 6 + 18 = 6
7
Catholic University of the Angels of Chimbote
Faculty of Engineering
Professional School of Systems Engineering Algorithms and Programming
Example: 9 divided by 2 = 4
Dividend Divisor
9 2
84
1
Residue Quotient
Examples:
a. 15 divided by 6 equals 2 b. 14 divided by 2 = 7 c. 0 divided by 3 = 0
d. 7 divided by 9 = 0 e. 3 divided by 3 = 1 f. 2 divided by 5 = 0
operands
Residual (mod)
The modulus operator is used to obtain the remainder of an integer division. Just like for the
operator div, in order to find the remainder of a division using the operator mod, the
Operands must be exclusively integers.
Rules of the residue:
Residue
E mod E = E
E mod R = Not Valid
R mod E = Not Valid
R mod R = Not Valid
Examples:
15 mod 6 = 3 b. 14 mod 2 = 0 c. 19 mod 3 = 1
d. 7 mod 9 = 7 e. 3 mod 3 = 0 f. 2 mod 5 = 2
Internal functions
The operations required in the programs often demand, in addition to
the basic arithmetic operations, already addressed, a certain number of operators
special functions that are called internal, embedded or standard functions.
For example, the function raiz2 calculates the square root of a positive number, the function abs
returns the absolute value of a number, the square function returns the value of a number.
squared. There are other functions that are used to determine the functions
trigonometric.
8
Catholic University of the Angels of Chimbote
Faculty of Engineering
Professional School of Systems Engineering Algorithms and Programming
The following table collects the most common internal functions in algorithms, with 'x' being the
function argument.
The functions accept real or integer arguments and their results depend on the task that
perform the function:
Examples:
Table 5: Examples