Professional Documents
Culture Documents
The Fundamentals of Programming
The Fundamentals of Programming
From this statement it is necessary to clarify the meaning given to the word "instructions". In this
context, an instruction should be understood as an expression, usually based on mathematical
models, which tells the computer the operations to be performed with a set of data previously
stored in the RAM.
Such expressions or instructions are organized in successive sequences from a starting point to
an arrival point, so that they solve a problem; This ordered sequence is called a computer program
or algorithm.
Based on these concepts and with those studied in the previous topic (From zero, the basics), let's
see how the computer takes a program and executes it. The idea starts from the fact that the
expressions or instructions are composed of two elements that are separated by the assignment
symbol, that is:
And when giving these instructions to the computer to execute them in sequence, that is, one
after another (according to the execution flow - follow the arrow -), as shown below:
X 12.345
X X + 76.809
Y X - 54.87
Z X/Y*2
Example program 1.
Then you have a program running.
What the expressions do is tell the computer how to handle the data involved in them. In the case
of this example program 1, and in order to understand the way in which the data stored in the
RAM of the computer are manipulated by each one of the expressions. The following is a diagram
in which a graph is placed Associated with each of them, showing the state of how the data is
stored after the computer executes each of the instructions.
RAM MEMORY
X 12.345
RAM MEMORY
X X + 76.809
RAM MEMORY
Y X - 54.87
RAM MEMORY
Z X/Y*2
In order to effectively understand all this, it should not be forgotten that a computer is a machine
that is composed of the CPU (central processing unit), memory (Internal RAM and ROM, External
Hard Disk) and the input devices and the output devices (keyboard, mouse, printer, etc.). What
interests here is to see the effect of the instructions on the handling of the information, since
Teacher: Wilson Pimiento Page 2 of 42
Los Fundamentos de la programación UPC - SAM
they act by handling the data that is involved in the expression and these are always stored inside
The RAM. Therefore, attention should be focused on the management of RAM, rather than on
what happens in other parts of the computer.
Now, an analysis of what each of the instructions performs from the point of view of how each
one manipulates the data requires that the computer's RAM be understood as a large space
composed of boxes in which information is stored. Figure 1 illustrates this fact. The size of this
space depends on the amount of RAM installed on the computer.
All the boxes are organized in such a way that they are contiguous with each other, as if it were a
honeycomb of bees. An image that can be made of how the RAM is formed would be as shown
below in figure 2.
Figure 2 shows a scheme equivalent to that seen in Figure 1. Only that Figure 2 is a 3D diagram.
What you want to show here is that there is a set of boxes that are empty and can be used (filled)
by a program. Each of these boxes can be understood as cells that store or contain information.
An expression tells the computer how to take the data and how to manipulate it. Each data is
stored in a cell and to refer to the data, the cell is assigned a name, and refers to it by means of
this name. Now, if you re-run Example program 1, the results achieved by each of the instructions
on the data you manipulate can be seen in Figure 3 below.
X 12.345
X X + 76.809
Y X - 54.87
Z X/Y*2
Simplifying the graph shown in figure 3d, which corresponds to the final state of the data after
the program is executed, it is clear that only three cells are occupied by all the RAM available to
the program. Figure 4 presents this fact.
Figure 4. Simplified image of the RAM with the result data of the program.
To understand the fact that every program must be defined from the three functions mentioned
above, it must start from the definition of the concept of information system, because in the long
run that is what a program is running on a computer; A system that takes data, processes it and
must respond in a way that can be used.
Fuente: Pimiento C. Wilson M. Fundamentos de lógica para programación de computadores. Universidad Piloto de Colombia. 2009.
It is clear that the system receives data through the input devices. This means that every system
must implement a function that allows it to acquire data (through keyboards, USB sticks, CD-ROM,
DVD-ROM, etc.) from the medium in which it operates. This task is called the input function.
Already with the data supplied and stored in the system RAM, these are passed to the CPU and
there are processed and from them, a response is calculated accordingly. The response is also
stored in the RAM. This process is called the processing function.
Once the response is obtained, output information (displays, printers, etc.) is displayed to the
user of the information system. This latter work is what is known as the output function.
Consequently, with what has been said so far, one can think of simplifying the scheme of Figure
5 by reason of the input, process and output functions from the perspective of RAM management,
which is shown below in the figure 6.
As can be seen in Figure 6, the system will work whenever there is a program that effectively
considers the input data, the processes that are applied to them and the information it gives as a
response. That is to say, the program must have instructions or expressions that allow to put data
inside the RAM (Inputs), instructions or expressions that allow to process the data (saved in the
RAM) to obtain the expected results and, finally, must have instructions or expressions to show
the results, output or remove the calculated information from the RAM.
Returning to the Example program 1, and if one analyzes from the perspective of the functions
that each program must have, one can conclude that it implements, with the instructions that
compose it, only two of them as shown below:
In conclusion, this program is not well built because although it calculates what corresponds, it is
not possible to see the answers because there are no instructions in the program that take the
result information stored in RAM and pass it to a screen or a printer or Some output device. That
is, it does not have or does not implement the output function that must have any algorithm or
computer program that is considered well-constructed.
One way to correct this problem in Example program 1 is to add instructions to the set of
instructions that compose it, telling the computer to show the results obtained. This is
accomplished simply by completing the program as shown below.
3. Finite Program.
With the instructions added to the Example program 1, now the program is complete because it
already implements the three functions that make up a program. But it is still a poorly done
program because it is still necessary to adjust a detail to say that the program is completely
written and well-constructed.
What is missing is to determine the starting point and end point of the program. This is because
the definition that is being taken of reference is the one stated in numeral 2.1 of this document.
In this paragraph, in the first paragraph, says that every program must start from an initial point
and state and arrive at a response at an end point. This implies that the Example program 1 will
have to be added the markers that indicate clearly from where it starts and to where the program
arrives; You simply have to include two instructions that indicate this, as shown below.
With this the Example program 1 has been correctly written. A final presentation of the program
is as shown below.
Start
X 12.345
X X + 76.809
Y X - 54.87
Z X/Y*2
Write X
Write Y
Write Z
End
Example program 1 Finished No. 1.
A detail to be highlighted is the fact that the Start and End instructions should always be run to
the left side in relation to the rest of the program instructions. This rule for the writing of the
programs is made in order that it is very easy to identify or visualize the starting point of the
program and the end point of the program. This technique is called the indentation of algorithm
writing and is fundamentally used to facilitate the reading and compression of the programs when
they are read and interpreted by the people. For the computer this has no effect (González 2008).
Start
X = 12.345
X = X + 76.809
Y = X - 54.87
Z = X/Y*2
Write X
Write Y
Write Z
End
Example program 1 Finished No. 2.
The modification applied to the program clearly implies that the arrow of assignment is equivalent
to the sign equality, but that effectively should not be understood as a mathematical equality,
since it is not. In programming the equality sign is an instruction that generally refers to an
assignment process. It indicates this, that the result that gives on the right side of the equality
sign is stored, stored or assigned to the variable written on the left side of the equality sign.
5. Interactive entries.
Returning on the Example program 1 Finished No. 2, related in the previous section, an
identification number will be introduced to each one of the instructions in order to be able to
make explicit reference to each one of them, being thus.
1 Start
2 X = 12.345
3 X = X + 76.809
4 Y = X - 54.87
5 Z = X/Y*2
6 Write X
7 Write Y
8 Write Z
9 End
Example program 1. Finished No. 2 Modified with number of lines.
The number that was placed in the program is not part of the program, it is only a reference for
each instruction. That the program is this, but without the numbers; The number is an aid to make
easier the explanations that follow from here on. In conclusion, the numbering has a totally
didactic meaning.
Now, returning to the program, and doing an analysis of the behavior of the same with respect to
the instructions that implement the input function (instruction numbered with 2 in the listing of
Example program 1. Finished No. 2 Modified with number of lines), It is seen that the input
information, every time the program is executed, will always be the value 12.345, never changes.
This means that the program has a single input
But how do you do that, when running the program, not only work with the value 12.345, but can
change this value to one that the program user can choose at the time of using the program?
This means that the assignment instruction must be changed for the input function (instruction
2). That is, to change this assignment instruction 2 by an interactive data reading instruction,
which will allow the program user to use an input device, such as a keyboard, so that he can type
a value of his preference. Then the Example program 1. Finished No. 2 Modified with number of
lines with the change of the instruction will be as shown
1 Start
2 Read X
3 X = X + 76.809
4 Y = X - 54.87
5 Z = X/Y*2
6 Write X
7 Write Y
8 Write Z
9 End
Example Program 1. Modified with reading process.
By carefully studying the Example Program 1. Modified with reading process, Instruction 2 is
now no longer an assignment but a read-through data (Read X). All this implies that every time
the program is used or executed, the data for variable X will be a value typed by the keyboard.
From the typed data will have a particular answer for the variables X, Y and Z. This instruction has
a way of working that allows the interaction between the user of the program and the program
itself so that status can be given to the program interactive.
Interactivity is the most frequent way of doing programs since the idea is to implement programs
that receive some data and they respond with results obtained from the data received. If you
think again about what an information system is, then it is precisely this, the input is given, the
process is done and the output information is reported or delivered.
instruction, the assignment, the Write instruction, and the instruction End. The form of
representation that is made of the algorithms by means of the explicit writing through symbols
and rules is called the representation or writing by "pseudocode" (Solis 2012).
Reserved words
Start
End
Read/Input
Write/Print
If – Then
For
While
Repeat
Stop
Until
The first thing to think about is the mathematical processes involved that allow the conversion of
meters to feet and inches. A quick internet query shows that to do this conversion simply apply
the following equivalences:
Based on this, and suppose you want to pass 1.93 meters, to its equivalent value in Feet and
Inches, then what you have to do is see how much is 1.93 meters in inches. This is achieved simply
by applying a simple three rule, using the above equivalences; Which is as follows:
Meters Inches
1 Equals 39.4
1.93 Equals X
Then, the value of 1.93 meters, is equivalent in inches to: X = 76.042 Inches. Then the X inches
should be passed to feet, implying to raise another simple three rule, as follows:
Inches Feet
12 Equals 1
X Equals Y
So:
X Inches * 1 Feet
Y =
12 Inches
If you take the value of X previously calculated, which is 76.042, then you have:
𝒀 = 6.336833333 Feet
The entire part of the value calculated as Y indicates the number of Feet that are equal to 1.93
meters, which can be taken as Z, as follows:
Z = 6 Feet
Now take the decimal part of Y to calculate the inches and thus calculate the missing data to
complete the answer of the problem, that is:
W= 0.336833333 Feet
Then, take W and pass to inches, again using a simple three rule, then it is proposed that:
Inches Feet
12 Equals 1
T Equals 0.336833333
So:
T= 4.042 Inches
With which the conversion is completed and the requested answers of the problem will be:
X = 1.93 Meters
W = 6 Feet
T = 4.042 Inches
Once you have absolutely clear what you have to do to log the conversion you can proceed to
see how to make a program for the computer to do the job. To achieve this, we must start with
the analysis of the mathematical procedures used, then proceed to simplify them in such a way
that a generalized form can be achieved in the application of the process. For the example this
generalization would be:
Meters * 39.4
Y =
12
Feet = Int(Y)
Inches = (Y - Feet) * 12
The previous mathematical procedure says that if you know the number of meters, then you can
determine how many feet and inches are equivalent. Consequently, this generalization can be
taken so that an algorithm can be made to do the work using a computer, as already mentioned.
All computer programming languages have already implemented a series of programs that can
be used whenever they are needed. These programs are those that are responsible for doing
things like the calculation of the sine of an angle, the square root of a value, the value of the Euler
constant, etc. These programs are called internal functions. Example of these functions, is Int ()
used in the example program, this is an internal function that takes the integer part of a value,
which is given to it in parentheses after the function name.
To clarify the above, the scheme that follows to write a program is to determine what inputs or
data to provide to the computer (program input function), what is the process that the computer
follows to get the answer (Processing function) and what information to give as a result (output
function).
Answering these questions allows to establish the instructions that must conform each of the
functions that make up an algorithm. These questions are known as "the guiding questions for
the construction of algorithms or computer programs".
As the idea is to make a program that allows the user to supply a data that corresponds to the
value of the meters that wants to pass to feet and inches, then, you must take the second
instruction, that is to say, you must take the Read instruction. The design of this instruction would
be:
Read Meters
Instruction that, when executed by the computer, will interpret it as having to enable an input
device (such as the keyboard) so that the user of the program can write a value which will be
stored in the RAM and which will refer by means of the name Meters.
In this sense, thinking of the instructions that must be written to do the calculations and
implement the algorithm processing function, is simply to apply the equations of generalization
previously noted, to do the conversion. In other words, write these mathematical formulas so
that the computer can interpret and execute them, so you have to write them as algorithmic
expressions. This is shown below.
Y = Meters * 39.4 / 12
Feet = Int(Y)
Inches = (Y - Feet) * 12
The instructions that must be written to implement the output function of the program are as
shown below and simply consists of using the Write instruction twice, one so that the response
of the number of feet and the other so that the Response of the amount of inches. This is:
Write Feet
Write Inches
Read Meters
Y = Meters * 39.4 / 12
Feet = Int(Y)
Inches = (Y- Feet) * 12
Write Feet
Write Inches
Start
Read Meters
Y = Metros * 39.4 / 12
Feet = Int(Y)
Inches = (Y- Feet) * 12
Write Feet
Write Inches
End
Example Program 2. Conversion of meters to feet and inches
Remember that the Start and End instructions should be placed indented.
Now, applying the test of verification to the program for the conversion of meters to feet and
inches would have to take the list of instructions that comprise it, from the instruction Start to
End and develop manually each instruction. Consequently, and for the purposes of the
explanations that are advanced in this text on how the test should be done, it will take the list of
instructions of the program result in the previous section (numeral 2.6.2.5.) and it will be added
a Number to identify each of the instructions and thus facilitate the reference to each of them
individually. Then, this program would look like this:
1 Start
2 Write Metros
3 Y = Meters * 39.4 / 12
4 Feet = Int(Y)
5 Inches = (Y- Pies) * 12
6 Write Feet
7 Write Inches
8 End
Example program 2. Conversion of meters
to feet and inches with number of lines.
Now, the test will run like this: It all starts when the computer takes the program and executes
the Start command. This is the starting point of the algorithm to be executed. After executing the
Start instruction (Example program 2. Conversion of meters to feet and inches with number of
lines), the instruction number 2 is executed, which consists of reading a value from the keyboard
and passing it to the RAM of the computer.
As the "Read Meters" instruction enables the keyboard to write a value at will, which must be
one that represents the number of meters you want to pass to feet and inches, then, once the
value has been written, the User gives you input by pressing the <ENTER> key and the value for
Teacher: Wilson Pimiento Page 16 of 42
Los Fundamentos de la programación UPC - SAM
RAM and the execution of the instruction is terminated. This will allow the computer to define a
cell in the RAM memory in which the value entered will be stored, which will be called the Meters.
If it is thought that the user has written the value 30.52 as the amount of meters to be converted,
it is possible to graphically understand the effect of this instruction on one of the cells of the RAM
as shown below.
Meters
Already with this information in the memory and finished the work of the instruction 2 of the
program listing, with which the function of entry in the algorithm is fulfilled, it goes to execute
the instruction 3 of the program list, which is in charge of the operations of the calculation
concerning passing the value 30.52 saved in the variable Meters to its equivalent in feet. This is
to leave stored in one of the cells of the RAM the value of doing the multiplication by 39.4, then
divide it by 12 and leave the value in cell or variable named Y. Graphically this would be:
Once the instruction 3 is finished, the instruction 4 is executed, which consists of applying the
internal function Int (Y) to the information in the RAM cell named Y. This consists of taking the
value 100, part Integer of the data in the variable Y. The value 100 passes to another cell of the
RAM named Feet. Graphically the result of instruction 4 would be:
Feet
From instruction 4 it changes to 5 and it performs a subtraction between the value of the variable
Y and the value of the variable Feet. The value of the difference multiplies by 12 and the response
of these calculations saves it in the cell of the RAM or variable named Inches. Graphically the cell
would look like shown below.
Inches
Up to this point of program execution, the RAM will contain the information in four cells or
variables like this: cell or variable Meters with value 30.52, cell or variable Y with a value of
100.20733, cell or variable Feet with 100 y The cell or variable Inches with the value 2.488.
Graphically you can see this:
Content of RAM
With these data in the variables and with the instructions executed so far, the first two functions
of an algorithm have been fulfilled, which consists of data entry and processing of the same to
obtain the desired results. From here onwards in the program will execute the instructions that
give fulfillment to the third and last function of all algorithm, those that are in charge of the exit
of the information; Which corresponds to taking the response information from the RAN and
putting it on the computer screen or printing it on a sheet of paper or sending it over the network
from one computer to another, etc.
From the program list, instructions 6 and 7 are responsible for extracting information from the
variables Feet and Inches to the computer screen. Executed these two instructions and visualized
these two answers you get to instruction 8 which is the end of the program.
In conclusion, if the algorithm is given the value of 30.52 it is responsible for passing this value to
its equivalent of 100 feet and 2.488 inches, correct calculations, which ends the manual program
test.
Keyboard (Input of
Read information to the
program).
If these symbols are used to represent the program for the conversion of meters to feet and
inches would be as shown below.
Start Start
End End
Now, if you add the execution flow to each of the symbols on the right side of the previous table,
you will get the DFD that will represent the algorithm completely, as shown below.
If you take the JAVA programming language, the translation of the algorithm would be as shown
below.
With the algorithm in the programming language you will only have to transcribe it into the
computer by using an IDE and running it.
If we carefully study the algorithm presented above, which is written in the JAVA programming
language, it is clear that each time a variable is used, a word must be prefixed. This word tells the
Teacher: Wilson Pimiento Page 21 of 42
Los Fundamentos de la programación UPC - SAM
computer how it should treat the information stored in the variable. For this case, the word
double has been prefixed to each of the variables of the program (Meters, Y, Feet or Inches). With
this, what is wanted is that the data stored in the variables to which this word is put before them,
will be treated as real or decimals data.
Just as you can work on the computer with real data, you can use in the construction of programs,
data that does not have decimals, that is, whole data. In this case, when the JAVA programming
language is used, an int word must be prefixed to each variable in which you want to save integer
data.
But if the case is to use alphanumeric data, that is, data that refer to text, phrases, words, etc. If
you do not save values or data that represent quantities in the variables, but you want to save
references or words in them, then in JAVA the word that must be prefixed to a variable is String.
These words (double, int, String) as they are written, define, for each variable, the type of data
that will be handled and it must only be done the first time the variable is to be used in the
program. It is what is known in JAVA programming, the definition of the variable and it is strictly
necessary to do. Thing that is not necessary to do when the algorithm or program is implemented
in DFD or pseudocode (Sznajdleder 2017).
So, so far, it can be said that three types of data can be used. One, the real data. Two, the integer
data. And three, the string or alphanumeric data. The real and integer data are used to define
numerical variables in which data are stored with which mathematical operations can be
performed (addition, subtraction, division, etc.). The third, chain type data, used when what you
want to store in the variable are data such as names of people or things, references of product
items in goods inventories, identification numbers of people, etc. Data with which mathematical
calculations should not be made.
Symbol Instruction
IF Condition? Then
Procedure 1
Procedure 2
Procedure 3
Else
Procedure 7
Procedure 8
End IF
The application of the selection process in a specific problem is presented below. The problem is
to develop an algorithm that takes three points (P1 = [X1, Y1], P2 = [X2, Y2], P3 = [X3, Y3], with P1
as the closest point to the origin and P3 the distant from the origin) located in the first quadrant
of the Cartesian plane and from this information determine if these points belong to the same
straight line. If so, calculate the length of the line they form, if the points do not constitute a line,
then calculate the area and perimeter of the triangle they form.
It all starts by looking at the mathematics that must be applied to find the answers that the
problem asks, then, to calculate the length of a straight line that form three given points, since
these points belong to the same straight, simply take the Straight line that forms between P1 and
P2 and the line that is formed by P2 and P3 and determine the slope of each one of them.
Graphically the thing would be as shown in figure 7.
If the slope calculation gives the same value, then the three points are part of the same straight
line. The formula for finding the slope of a line passing through two points (Profesor en línea
2012) is as follows:
Y 2 - Y1
m=
X 2 - X1
If this equation is applied to the line passing through the points P1 and P2 there would be:
Y 2 - Y1
m1 =
X 2 - X1
And the slope for the line passing through P2 and P3 would be:
Y 3 - Y2
m2 =
X 3 - X2
Now, if it turns out that from m1 and m2 the same value is obtained, then the lines are part of
the same line, so the points belong to the same line, then the length of the line from P1 to P3
must be calculated. The above implies that we have to calculate the distance between two points
(Profesor en línea 2012), which can be done by means of the formula:
d = √(X2 - X1 )2 + (Y2 - Y1 )2
Applying the formula to calculate the distance between P1 and P3 according to the problem
would be:
d = √(X3 - X1 )2 + (Y3 - Y1 )2
On the other hand, when it turns out that the value m1 and that of m2 are not equal, then the
perimeter and the area of the triangle having the three given points as vertices will have to be
calculated, figure 8.
In order to calculate the area of the triangle, it is enough to determine the distance between P 1-
P2, P2-P3, P3-P1. The area is calculated using the formula to find the area of a triangle if one knows
the length of its sides (Dictator 2010); Data that are known because the lengths of the lines that
form between the points are known. This is:
a+b+c
P=
2
Triangle area = √P (P - a) (P - b) (P - c)
a = d1,2
b = d2,3
c = d3,1
SO:
d1,2 + d2,3 + d3,1
P =
2
algorithm and for this will have to establish the answers to each of the questions that guide this
one. Therefore, we will have to see, first, the answers to the question ¿What are the entries?
The answers to this question are considering what would be the data needed to start doing the
calculations? These calculations correspond to the coordinates of each of the points. These data
will have to be entered to the computer and based on them, he will have to calculate the answers.
From the perspective of the computer, he will have to read the information that the user of the
program types. Therefore, the design of the instructions will be:
Read X1
Read Y1
Read X2
Read Y2
Read X3
Read Y3
With this instruction the first question is answered because it covers all the information needs
according to the program's approach, since it is only the coordinates of the points what is
required. In conclusion, this will be the instruction that determines the input function of the
program. Now the second question that guides the development of the program must be
answered. This is ¿What are the procedures to be performed?
The answer to this question is determined which are the processes or mathematical
developments that allow to calculate the base values to give the answers that must give the
algorithm. Mathematical processes are merely the use of the formulas described above in this
section. These formulas written as algorithmic expressions will be:
Finally, the answer to the third question must be found. This will determine what is the output
information? The answer will be:
Start
Read X1, Y1, X2, Y2, X3, Y3
m1 = (Y2 – Y1)/(X2 – X1)
m2 = (Y3 – Y2)/(X3 – X2)
If m1 = m2 Then
d13 = ((X3 – X1) ^ 2 + (Y3 – Y1) ^ 2) ^ 0.5
Write “The length of the line is ”, d13
Else
d12 = ((X2 – X1) ^2 + (Y2 – Y1) ^2) ^0.5
d23 = ((X3 – X2) ^2 + (Y3 – Y2) ^2) ^0.5
d31 = ((X1 – X3) ^2 + (Y1 – Y3) ^2) ^0.5
P = (d12 + d23 + d31)/2
A = (P * (P – d12) * (P – d23) *(P – d31)) ^ 0.5
Perimeter = 2 * P
Write “The area of the triangle is ”, A
Write “The perimeter of the triangle is ”, Perimeter
End if
End
Example program 3. Determine whether it is a line or a triangle. No. 4.
It should not be forgotten that the program must be armed using the indentation, here in this
program the use of the rule of indentation is seen with greater clarity. The Start and End
instructions have been placed as reference points to establish where the program begins and
ends and exactly what the instructions are. The instructions that are between the Start and the
End have been tabulated to the right with which gives greater visibility to the two parts of the
program (start-end points and body of the program).
With the If instruction m1 = m2 Then there is another point where the indentation should be
applied since this instruction handles two blocks or two sets of instructions. An instruction block
placed between the instruction If m1 = m2 Then and the instruction Else. The other block that is
placed between the Else and End if instructions.
In general terms, the If ¿Condition? Then instruction is used to select one of two procedures that
must be executed depending on a condition, that is, if the evaluation of the condition is true, the
written block of instructions is executed between If ¿Condition? Then and Else instructions. But
in case the evaluation of the condition is false, it indicates that the instructions that are written
in the block delimited by the instructions Else and End if must be executed.
Since the idea of indentation is to clearly visualize how the program is organized, then this
organization must make it clear what are the instructions or block of instructions that are
executed when the slopes are equal and what is the block of instructions that are executed when
the Slopes are different. Which is clearly visualized with the organization of the program that has
been used.
In conclusion, the instructions that are executed when the slopes are equal will be the instructions
of the calculation of the length of the line from P1 to P3 (d13) and the instruction that reports this
value (Write "The length of the line is" d13). On the other hand, when the slopes are not equal
the instructions that are executed are:
If the program is written as a DFD, the result would be like the one shown below.
What remains to conclude the elaboration of this algorithm is to make a verification test. To do
the test you can take either the program in pseudocode or in DFD and submit it to a manual
execution. Here the program written in pseudocode will be taken, so the following list of
instructions is presented, but with a modification that consists in placing a number that identifies
each of the instructions, as shown below.
1 Start
2 Read X1, Y1, X2, Y2, X3, Y3
3 m1 = (Y2 – Y1)/(X2 – X1)
4 m2 = (Y3 – Y2)/(X3 – X2)
5 If m1 = m2 Then
6 d13 = ((X3 – X1) ^ 2 + (Y3 – Y1) ^ 2) ^ 0.5
7 Write “The length of the line is ”, d13
8 Else
9 d12 = ((X2 – X1) ^2 + (Y2 – Y1) ^2) ^0.5
10 d23 = ((X3 – X2) ^2 + (Y3 – Y2) ^2) ^0.5
11 d31 = ((X1 – X3) ^2 + (Y1 – Y3) ^2) ^0.5
12 P = (d12 + d23 + d31)/2
13 A = (P * (P – d12) * (P – d23) *(P – d31)) ^0.5
14 Perimeter = 2 * P
15 Write “The area of the triangle is ”, A
16 Write “The perimeter of the triangle is ”, Perimeter
17 End if
18 End
Example program 3. Program list with line number for use in verification.
The verification starts by manually executing the program, in this sense the first thing to do is to
execute instruction number 1 of the program listing example 3 used for verification, which is
simply the start of the algorithm. Execution of instruction 1 starts to execute instruction 2, which
consists of an instruction that allows to enter data into the RAM of the computer by means of
some input device (such as a keyboard). Now if it is assumed that data 2, 2, 10, 6, 18, .10 were
entered for each of the points in the Cartesian plane required by the program, they would be
stored in RAM as shown below.
The graph shows that at the moment of the execution of the instruction of line 2 of the program,
the user has taken at will the points P1 = (2,2), P2 = (10,6) and P3 = (18,10) Always with the idea
that they must be points in the first quadrant, P1 the closest to the origin and P3 the furthest from
the origin.
Since the computer has data to work then it is necessary to execute the instruction number 3 of
the program, it is assumed that this instruction is responsible for determining the value of the
slope of the line of P1 - P2. To do this it executes the differences, then the division and the answer
saves it in the variable m1.
Similar situation is what is done when executing instruction 4, only in this case is the slope for the
line P2 - P3. If you think about how the data is in RAM after the first 4 instructions of the program
are executed, graphically you can see below.
As shown in the graph, instructions 3 and 4 have calculated for m1 and m2 the same value, then
from these data and based on the execution of program instruction 5, the computer determines
that the instruction must be executed 6 and then 7, since m1 and m2 are equal. Therefore, d13
will save the value of performing the calculations of statement 6. Statement 7 places this value
on the screen along with a sign that reads "The length of the line is". An inspection of the contents
of the RAM after executing instructions 6 and 7 is shown below.
After executing instruction 7, the corresponding instruction block has been executed with the
decision If m1 = m2 Then when it is true. From there we jump to execute instruction 17 and from
this to 18, which concludes the execution of the program and the test of verification for this
particular case when the input data are of points that belong to the same straight line.
It should be noted that with the data of the assumptions for this verification test, the block of
instructions found from instruction 9 to 16 is not executed since they will only be executed when
the value of m1 and m2 be different.
There will now be another test of program verification, but care will be taken that the data given
as input correspond with points that form a triangle and no longer a straight line. These data must
test the program, but this time taking the corresponding execution path with the instructions for
the calculation of the area and the perimeter of the triangle formed.
This second test of verification is started by taking instruction number 1, which is the start, of this
one goes to execute the instruction 2 that is the one that allows to enter the data (in the case it
is going to suppose that the user of the program will now take the data corresponding to
P1 = (2, 4), P2 = (10, 2) and P3 = (14, 10) respectively), the result of this instruction leaves in the
RAM, within each of the variables X1, Y1, X2, Y2, X3, and Y3), the data corresponding to the
points. Graphically the interpretation of this fact is shown below.
From instruction 2 the instruction 3 is executed and then instruction 4 is used to calculate the
slopes of the lines m1 and m2. The action of these two instructions consists in placing in the
variables m1 and m2 the value of the two slopes. Graphically it will look like this:
As shown in the graph, instructions 3 and 4 have calculated for m1 and m2 different values, then
from these data and based on the execution of program instruction 5, the computer determines
that they must be executed one by one The block of instructions comprised between instruction
9 and 16, since m1 and m2 are NOT equal. Therefore, d12, d23, d31, P, A and Perimeter will be
the variables or cells of the RAM that will contain the values resulting from the calculations
performed according to the formulas. An inspection of the RAM status after you execute the
instructions can be seen below.
Instructions 15 and 16 are now in charge of passing the data from the RAM to the computer
screen so that the value of the area and perimeter of the triangle is visible.
This completes the verification test of the program and demonstrates that effectively given input
data are obtained in response to the length of the line or the area and perimeter of a triangle.
You only have to write the program in JAVA and then transcribe it and run it on your computer.
The written program will be:
Teacher: Wilson Pimiento Page 32 of 42
Los Fundamentos de la programación UPC - SAM
To begin the construction of the algorithm should be done from the questions that guides the
construction of programs. These are:
Making a synthesis of all this process of analysis in the search of the answers to these questions
would be:
X, Y, Z
Now if they are not equal the values will have to be determined:
a. X is greater than Y and if X is greater than Z then greater than X and we will have to
determine if Y is greater than Z and consequently Y will be the value of the mean and
Z is smaller. But if Y is not greater than Z Then Z is the mean value and X is the smallest.
b. In the event that X is not the greater of the three values, then we must see if Y is
greater than X and if Y is greater than Z, then the greater will be Y and we must
determine if X is greater than Z, in which case X is the mean value and Z is the lowest.
But if X is not greater than Z Then Z is the mean value and X is smaller.
c. In the case that X is not the largest of the three values, nor Y is the highest value of all
three, then we must see if Z is greater than X and if Z is greater than Y, then the greater
will be Z and we must determine if X is greater than Y, in which case X is the mean
value and Y is the lowest. But if X is not greater than Y Then Y is the mean value and X
is smaller.
3. The output in the order that corresponds the values entered, or which will be stored in the
variables X, Y and Z.
The program will be put together using the DFD technique. In this sense the first thing to put are
the instructions that implement the input function. For this, it will be necessary to see those
established in number 1 referring to the entries in the previous paragraph. The DFD for this will
be:
Now we will have to see how the instructions are built to implement the program information
processing function. For this, from the approach of numeral 2 in the literal a of the syntheses of
the previous paragraph, it is possible to see that a DFD must be constructed in which a decision
is presented which must contain a condition. The DFD is as shown below.
This decision poses that X > Y and X > Z and if it turns out that if it is true then the instructions
that are placed by the right side of DFD must be executed. For this case and following the planning
described in paragraph 2 literal a of the previous paragraph, it is necessary that the DFD should
remain as shown below.
A similar approach should be made for paragraph b and c of numeral 2 of the corresponding
section with the synthesis of the analysis to find the answers to the questions guide for the
construction of programs. Then the DFD armed with the implementation of the three approaches
of the whole number 2 will be:
It is simply necessary to take into account that the fact that the values must be all different at the
time of the processing of the information by the program should be considered. This fact leads to
the DFD must be added another decision as shown below.
Another aspect that should be highlighted is the fact that the previous DFD besides showing the
processing of the data by means of the decisions, is that also the instructions that implement the
output function are placed. These instructions are those that are implemented with the symbol
shown below.
Indicates that the response information will leave it printed. In this sense, this symbol is
equivalent to
That indicates that the information will be placed on the computer screen. In this text, one of
these symbols will be used indistinctly to indicate the implementation of the output function.
The complete presentation of the algorithm armed as DFD and with the three functions (Inputs,
process and outputs) is presented below.
With this example, the idea is to see how a program that depending on the problem it solves,
includes different things. One of them is the way decisions are organized and the example clearly
shows this fact. It is seen that, from a decision, either by the alternative of true or by the
alternative of false, you can place another or other decisions depending on the complexity raised
by the problem to be solved. These structures placed in this form are what are called nested
decisions.
Finally, the representation of the program, but now written in pseudocode is presented below.
Start
Read X, Y, Z
If X = Y AND Y = Z Then
Write X, “, “, Y, “, ”, Z, “They are equal”
Else
If X > Y AND X > Z Then
If Y > Z Then
Write “Major is ”, X
Write “Medium is ”, Y
Write “Smaller is ”, Z
Else
Write “Major is ”, X
Write “Medium is ”, Z
Write “Smaller is ”, Y
End if
End if
If Y > X AND Y > Z Then
If X > Z Then
Write “Major is ”, Y
Write “Medium is ”, X
Write “Smaller is ”, Z
Else
Write “Major is ”, Y
Write “Medium is ”, Z
Write “Smaller is ”, X
End if
End if
If Z > X AND Z > Y Then
If X > Y Then
Write “Major is ”, Z
Write “Medium is ”, X
Write “Smaller is ”, Y
Else
Write “Major is ”, Z
Write “Medium is ”, Y
Write “Smaller is ”, X
End if
End if
End if
End if
End if
End
Now the same program, but written in the JAVA programming language.
Now you must pass the verification test or desktop test program. This work is left for the reader
to do.
2. References.
André, Michele (2013). Estructuras algorítmicas. Universidad Simón Bolívar. Caracas Venezuela.
Consultado el 14-12-2014 en http://es.slideshare.net/mandre55/tutorial-algoritmo-estructuras-
condicionales-24659874.
Ditutor (2010). Ditutor.com. Malaga España. Adminisyrador por Vitutor, SLU. CIF: B93231215.
Consultado el 15-12-2014 en http://www.ditutor.com/geometria/triangulo.html.
Gonzáles Almirón, Cristóbal (2008). Indentación del código fuente. Consultado el 13-12-2014 en
http://www.adictosaltrabajo.com/tutoriales/tutoriales.php?pagina=indentacion.
Profesor en línea (2012). El profesor en línea. Santiago de Chile. Querelle y Cia Ltda. Consultado
el 14-12-2014 en http://www.profesorenlinea.cl/geometria/Recta_Pendiente.html#.
Solís Galindo, Alonso Ernesto (2012). Diagramas de flujo y seudocódigo. Universidad autónoma
del estado de hidalgo escuela tecnológica de Tizayuca. Consultado el 5-12-2014 en http://www.
uaeh.edu.mx/docencia/P_Presentaciones/tizayuca/ing_computacion/Fundamentos%20de%20P
rogramacion.pdf
Vélez Serrano, José F; Peña Abril, Alberto; Gortázar Bellas Francisco; Sánchez Calle, Ángel (2011).
Diseñar y programar, todo es empezar: Una introducción a la programación orientada a objetos
usando UML y JAVA. Universidad Rey Juan Carlos. Editorial DYKINSON Madrid.