You are on page 1of 12

Programming – 2021-2022

Introduction to Flow Diagrams

Bachelor in Data Science and Engineering


Dual Bachelor in Data Science and Engineering and Telecommunication Technologies Engineering
Programming 2021-2022 Exercises Week 2

Index
1 General rules ............................................................................................................................................... 3
2 Second practical exercise ............................................................................................................................ 3
3 Introduction ................................................................................................................................................. 3
4 Flow diagrams.............................................................................................................................................. 3
4.1 Start and end of the program .............................................................................................................. 4
4.2 Definition of variables ......................................................................................................................... 4
4.3 Instructions .......................................................................................................................................... 4
4.4 Input and output ................................................................................................................................. 5
4.5 Control flow control statements: conditional ..................................................................................... 6
4.6 Control flow statements: loops ........................................................................................................... 8
5 Final example: factorial ............................................................................................................................. 11
6 Exercises .................................................................................................................................................... 11
7 Delivery rules ............................................................................................................................................. 12

Universidad Carlos III de Madrid 2


Programming 2021-2022 Exercises Week 2

1 General rules
Students are requested to perform and deliver exercises on a weekly basis.

The delivery of the exercises will account for a maximum of 0.5 points over the final mark. In order to be
marked, at least 80% of the weekly exercises must be delivered.

Every week, the next lab class after the delivery of weekly exercises, random students will be asked to orally
present the solution to the delivered exercises. A maximum of 0.5 points will be assigned to this item.

Delivery will be individual.

Even if the marks assigned to the practical exercises can seem small and they are not compulsory,
performing them is essential to gain the knowledge needed to pass the subject. It is highly recommended to
perform and deliver the weekly exercises on time. The exercises of a week will be the basis for the new
concepts that will be explained the following week.

2 Second practical exercise


The goal of this practical exercise is to introduce the student to Programming Methodologies and specifically
with the high level design of algorithms using flow diagrams.

3 Introduction
Programming requires analyzing a problem, determining which actions are required to solve it (the algorithm
to follow), decomposing the algorithm into the basic instructions a computer can execute and coding those
instructions into a programming language.

Quite often, the decomposing of the algorithm into basic instructions is neglected and the programmer
starts directly by coding the solution after a brief analysis has been done. This yields to programs that,
despite working properly most of the times, are very difficult to modify or debug; what is commonly known
as “spaghetti code”.

This second practical exercise will focus on thinking on the algorithm before coding it. To do that, we will use
flow diagrams. Flow diagrams allow to graphically depicting an algorithm, in such a way that it is easily
understandable by humans. Using flow diagrams is a good exercise for beginners, as they ease to think “as a
computer”, but it is also widely performed by expert programmers, particularly when they work on big
projects involving several people.

4 Flow diagrams
Flow diagrams graphically represent the steps of any algorithm (in our case of a computer program) by
means of a series of arrows that depict the sequence of the different steps of the algorithm. They are quite
useful as they take advantage of the human ability to understand symbols, easing the comprehension of the
algorithm and the detection of flaws.

Universidad Carlos III de Madrid 3


Programming 2021-2022 Exercises Week 2

4.1 Start and end of the program


Every flow diagram must have an initial (start) and an end (terminator) point, and the end must be
achievable from the start. The initial and end points are represented using the following symbols (two
different possibilities):

4.2 Definition of variables


A program transforms data according to certain instructions in order to solve a problem. In high level
languages those data are stored into memory using variables1. A variable, as shown by its name, can change
its value as a result of program execution. To assign a value to a variable (thus storing that value into
memory) we will use the ←2 symbol, which will assign the value on the right to the variable on the left.

Example: assigning a value to a variable and increasing it by one

Variable ← 1
Variable ← Variable+1

In many languages, as in Java, a variable has always an explicit fixed type, which determines which kind of
data it can store (integers, real numbers, text, etc.). However, in other languages, such as Python, the type of
variables is determined by their content. This is usually not represented in flow diagrams.

4.3 Instructions
Generally speaking, a program takes some input data, in the form of variables, and transforms them to get
output data. A good analogy is that of a calculator machine that transforms numbers using arithmetic
operators to obtain a result. In a program, data are not only numbers and the “operators” can be more
complex than simple arithmetic operations, but the process is similar. These data transformations are called
instructions, and are represented in flow diagrams as follows:

Variable assignations as the ones shown before are no more than instructions, so they should be placed
inside a rectangle, as the following example shows:

1
A variable can be seen as the name we use to refer to a specific memory cell. That way we do not need to remember
the address of the cell where a given datum is stored, just the name we gave to it.
2
In many programming languages the “=” symbol is used instead of “←” to assign a value to a variable. This symbol
should not be confused with the mathematical equality; its meaning in programming is “assign the value on the right to
the variable on the left”.

Universidad Carlos III de Madrid 4


Programming 2021-2022 Exercises Week 2

Example: flow diagram to assign a value to a variable and to increase it by one3

4.4 Input and output


In many cases the data needed by a program is entered by a human user, which very often also receives the
results of the program execution. The program communicates with the user by means of input/output
instructions.
An output operation represents some information (data) that the program shows to the user, usually by the
screen. In flow diagrams they are represented by:

When learning a new programming language it is quite usual to start with a very simple program, known as
“Hello World”, which just shows that message on the screen4. A flow diagram to represent such a simple
program could be:
Example: Flow diagram of the “Hello World” program

It is important to notice that the text string is surrounded by double quotes, which means that this string is
printed literally. If it were a variable, double quotes would not be used and the content of the variable would
be printed on the screen (see the customized greeting example below).

3
Notice that when we have more than one instruction executing in sequence, we can group them together in the same
box or use a box for each of them.
4
Programmers usually speak about “printing on the screen” to refer to “showing on the screen”

Universidad Carlos III de Madrid 5


Programming 2021-2022 Exercises Week 2

An input operation allows the user to introduce (using any input device as a keyboard, mouse, etc.)
information that will be used in the program. It is represented as follows:

In the following example the user is asked about his/her name and a personalized greeting message is
printed on the screen.
Example: Customized greeting

Again, notice the difference between printing a literal string (always placed between quotes) and a variable
like name (no quotes). Also the “+” symbol is used to represent string concatenation. The result shown on
the screen will be something as follows:
What’s your name?
Pepe (this is written by the user using the keyboard)
Hello Pepe!

4.5 Control flow control statements: conditional


So far, all the flow diagrams shown are sequential; the flow of the program goes directly from the start to
the end without any branch or loop. But in many cases it is needed to execute some instructions if a
condition is met and some others if it is not met. In such cases, the flow of the program is no more
sequential and we speak about a conditional control flow statement, which are represented as follows:

For example, if we failed to pass a subject in January we have a second chance in July, but if we passed we
would not need the second chance. A way to represent this using a flow diagram is the following:

Universidad Carlos III de Madrid 6


Programming 2021-2022 Exercises Week 2

Example: flow diagram for a university subject

In the former example there is a branch if the condition is met and another one if it is not met. But in some
cases we only want to branch if the condition is met, following the normal flow if not. For example you can
ask money to your parents to buy a soda. If they give you the money you will buy it, if they do not you will do
nothing. The flow diagram to represent it is:

Example: Money for soda

In the former examples we have used relational operators to compare the content of a variable with given
values in order to make the decision. In the first example the >= operator (also written ≥) is used to compare
the mark variable with a literal value. In the second example, two variables, money and price, are

Universidad Carlos III de Madrid 7


Programming 2021-2022 Exercises Week 2

compared using also the same operator. The following table shows the equality, relational and conditional
operators that are commonly used in comparisons. We use the Python symbols for them:

Operator (Python style) Meaning Operator (Python style) Meaning


< less than <= less than or equal to

greater greater than or equal


> >=
than to

== equal to != not equal to

not not and and

or or

4.6 Control flow statements: loops


Quite often some instructions have to be repeated several times before the flow of the program continues,
thus creating loops in the program. An example is replacing a lamp bulb; it needs to be twisted until it lights,
as shown by the following flow diagram:

Example: lighting a bulb

In the former example, what would be the result if the bulb was defective? We will keep twisting it forever,
with no way to stop doing it. This is known as an infinite loop and it is a quite common error in programs.
The way to solve it is to add a second control flow statement to assess whether we should stop twisting the
bulb.

Universidad Carlos III de Madrid 8


Programming 2021-2022 Exercises Week 2

Example: lighting a bulb with no infinite loop

Generally speaking there are three different kinds of loops: while, do … while and for. The while loop
checks a condition, performs a series of instructions if the condition holds and checks again the condition. If
it still holds it begins again. If the condition is not met at the beginning, the instructions inside the loop are
ignored. In the following example the algorithm to eat from a dish is shown:

Example: while loop (eating)

An alternative to the former algorithm is to start eating, assuming that there will be some food on the dish,
and then checking if the dish is empty. This is represented by a do … while loop. The difference with the
while loop is that in this case the instructions inside the loop will be executed at least once.

Universidad Carlos III de Madrid 9


Programming 2021-2022 Exercises Week 2

Example: do … while loop (eating)

In case we knew exactly how many food spoons a dish contained we would use the third type of loop, as we
would know how many times the loop has to be repeated. This is the way a for loop works. In the two
former types of loops, there is no way to a priori determine how many times the loop is going to be
repeated; whether the loop is going to be repeated or not depends on a condition that we need to check
after every repetition. But with the for loop we know how many times we want to repeat, so we will use
what we call a counter variable that will be increased inside the loop to account for the number of
repetitions already performed. It is important to notice that this counter is also a variable and needs to be
initialized before the loop starts.

Example: for loop (eating)

Universidad Carlos III de Madrid 10


Programming 2021-2022 Exercises Week 2

5 Final example: factorial


As a final example, the following algorithm shows the procedure to calculate the factorial of a number that is
read from the keyboard:

6 Exercises
Exercise 1. Create the flow diagram to describe the way to cook a potato omelet as it was defined in the
Exercise 3 of the former practical exercise.
Exercise 2. Create the flow diagram of an algorithm that given any two numbers A and B, calculates if A is
divisible by B, using just additions, conditionals (comparisons) and loops (as in the past week exercise).
Exercise 3. Create a flow diagram of a program allowing writing the first 100 even numbers.
Exercise 4. Create a flow diagram of a program to add the N first odd numbers.
Exercise 5. Create a flow diagram of a program asking the user to enter the coordinates of a point in a
plane, i.e. two integer values, x and y, not equal to zero (if any of them is 0 it will ask again). The program
must print the quadrant where this point lies (1st quadrant if x>0 and y>0, 2nd if x<0 and y>0, etc.)
Exercise 6. Create a flow diagram of a program that calculates and shows the final salary of a worker
depending on the base salary and seniority according to the following rules:
a. If the base salary is less than 1000 and the worker has been working for at least 10 years, the
salary will be increased by a 20%.

Universidad Carlos III de Madrid 11


Programming 2021-2022 Exercises Week 2

b. If the base salary is less than 1000 and she has been working for less than 10 years, the salary
will be increased by a 5%.
c. If the base salary is bigger or equal than 1000 just show it as final salary.
Exercise 7. Given two positive integer numbers N and D, it is said that D is a divider of N if the remainder of
dividing N by D is 0. A number N is said to be a perfect number if the result of adding all its dividers is N. For
example 28 is a perfect number, because its dividers are 1, 2, 4, 7 and 14, and the result of adding them is
28. Create the flow diagram of a program that reads a number by keyboard and calculates if it is perfect or
not.
Exercise 8. A year is leap-year if it is a multiple of 4, except if it is a multiple of 100. In this last case it will be
leap-year only if it is also a multiple of 400. For example the year 1900 was not a leap-year, but the year
2000 was. Create the flow diagram of a program that reads a number by keyboard and calculates if it is a
leap year.
Exercise 9. A bus line charges a minimum of 20 euros each one-way ticket per person. If the trip has more
than 200 kilometers, the ticket has an extra-charge of 3 cents per kilometer. However, a trip of 400
kilometers or more has 15% discount. Also, for groups of 3 or more people, the ticket has a 10% discount.
Regarding all the previous considerations please create the equivalent flow diagram.

7 Delivery rules
The solutions to the previous exercises must be uploaded to Aula Global before 20th September 2021,
8:00 am. Each group of students is requested to upload a zip file containing a PDF file for each exercise (9
PDF files compressed in one zip folder). The name of the zip file must be “w2-name-initials.zip” (for example
for Lucía Pérez Gómez the file will be named w2-lpg.zip).

Note: it is recommended to use www.draw.io to create the diagrams, but any tool can be used to create
them.

Universidad Carlos III de Madrid 12

You might also like