This chapter covers the romanian variant of pseudocode. Pseudocode is a language that is used to describe the steps of a program in a human readable way without having to write it in a specific programming language.
Usually important details such as variable declaration or certain sequences are skipped. Pseudocode algorithms can contain sequences explained in "natural language" and compact mathematical formulas which do no exist in real programming languages.
Algorithm concepts:
-
Instructions
-
Variables
-
Constants
-
Operations
-
Expressions
There are many variants of pseudocode so I will only focus on the variant used at the Romanian Baccalaureate for Informatics. The language used is Romanian.
##Variables & Constants
In pseudocode variables are not declared and their numbers respect the usual rules of using letters, numbers, underlines and not starting with numbers.
Constants however, are literal constants: numerical values, characters delimited by ', character arrays delimited by ' or ". There is also the chance of mathematical constants appearing such as PI: π.
Every usual arithmetic operation is present:
- addition of 2 numbers
a+b. Depending on the context, the result is whole or real. - subtraction of 2 numbers
a-b. Depending on the context, the result is whole or real. - multiplication of 2 numbers
a*b. Depending on the context, the result is whole or real. - division of 2 numbers
a/b. The result is considered real. If only the whole part is what interests you the syntax is[a/b]. - the remainder of dividing 2 numbers
a%b. It applies only to whole numbers and the result is a whole number.
Note: Some pseudocode variants propose the operations
DIVandMODfor division and modulus. Also there is a possibility that operations of raising to a powera^nand square root might appearsqrt(a).
- = - equality
- ≠ - inequality
- < - les than
- ≤ - less or equal to
-
- greater than
- ≥ - greater or equal to
The operands are usually numbers and the result is either true or false
- NOT
- AND
- OR
These all have their usual semnifications.
Instructions are the components of the algorithm. Usually every instruction is written takes one line (or multiple in case of complex ones), but there are situations where for saving space, 2 or more simple instructions are written on the same line and are separated with ;
Any algorithm can be represented through 3 types of structures:
- liniar structures
- alternative structures
- repetitive structures
- with an unknown number of steps
- initial test
- final test
- with a known number of steps
- with an unknown number of steps
To read data we use citește <listă variabile>, where <listă variabile> is a list of variables separated by ,.
Example
citeste a , b , c
Reading data is usually followed by an explanation of the data type.
For displaying data we use scrie <listă expresii>, where <listă expresii> represents a list of expressions separated with the , character.
Example
scrie 'a + b = ' , a + b
If a = 3 and b = 4 it will display:
a + b = 7To attribute a value to a variable we use <variabilă> ← <expresie>
a ← 0
S ← a + b
i ← i + 1
In pseudocode the equivalent to if is daca and to else is altfel and has the following syntax:
┌ daca <conditie> atunci
│ <instructiuni1>
│ altfel
│ <instructiuni2>
└■or
┌ daca <conditie> atunci
│ <instructiuni1>
└■The order of execution is this:
- evaluate
<conditiile> - if it is true, execute
<instructiuni> - if it is fake, execute the
<instructiuni>fromaltfel
The equivalent to for is pentru and its syntax is:
┌ pentru <variabila> ← <expresie initiala>, <expresie finala> , <pas> executa
│ <instructiuni>
└■Expresie initiala, expresie finala and pas are expressions usually with whole numbers as a result and pas can be 1 or -1 or not appear at all, in which case it is presumed to be 1.
variabila gets values inscreasing in each step if pas = 1 or decreasing if pas = -1 starting from expresie initiala all the way to expresie finala and for each value instructiuni gets executed.
If pas = 1 and expresie initiala > expresie finala the sequence instructiuni will not get executed at all. The same happens if pas = -1 and expresie initiala < expresie finala. In the contrary case the number of steps is expresie initiala - expresie finala + 1 if pas = 1 and respectively expresie finala - expresie initiala + 1 if pas = -1.
Example Determine the sum and product of the first n natural numbers:
S ← 0; P ← 1
┌ pentru i←1,n executa
│ S ← S + i
│ P ← P * i
└■
scrie S, ' ' , PThe equivalent to while is cat timp ... executa and its syntax is:
┌ cat timp <conditie> executa
│ <instructiuni>
└■Explanation
- Evaluate the condition
- if it's true execute instructions
- if it's fake skip it
Example Determine the sum of the digits of a number
citeste n
S ← 0
┌ cat timp n ≠ 0 executa
│ S ← S + n % 10
│ n ← [n/10]
└■
scrie SThe equivalent to do ... while is executa ... cat timp
┌ executa
│ <instructiuni>
└ cat timp <conditie>Explanation
- Execute the instructions
- Evaluate the condition
- If it's true, go back to step 1
- If it's false, skip it
Example Find out the number of digits a number has
citeste n
cnt ← 0
┌ executa
│ cnt ← cnt + 1
│ n ← [n/10]
└ cat timp n ≠ 0
scrie cntThis condition is a bit strange since it is the equivalent to do ... while(!condition) and its syntax is this:
│ <instructiuni>
└ pana cand <conditie>
Explanation
- Execute the instructions
- Evaluate the condition
- If it's false, go back to step 1
- If it's true, skip it
Note: Even if the condition is initially true, the instructions were already executed once.
Example The number of digits a number has
citeste n
cnt ← 0
┌ repeta
│ cnt ← cnt + 1
│ n ← [n/10]
└ pana cand n = 0
scrie cntNumerous exercises propose an algorithm and ask to write an equivalent using another repetitive structure. This section focuses on writing equivalents to various repetitive structures.
┌ pentru i←a,b executa
│ <instructiuni>
└■i ← a
┌ cattimp i ≤ b executa
│ <instructiuni>
│ i ← i + 1
└■┌ pentru i←a,b executa
│ <instructiuni>
└■i ← a
┌ daca i ≤ b atunci
│┌ executa
││ <instructiuni>
││ i ← i + 1
│└ cattimp i ≤ b
└■┌ pentru i←a,b executa
│ <instructiuni>
└■i ← a
┌ daca i ≤ b atunci
│┌ repeta
││ <instructiuni>
││ i ← i + 1
│└ panacand i > b
└■┌ cattimp <conditie> executa
│ <instructiuni>
└■┌ daca <conditie> atunci
│┌ executa
││ <instructiuni>
│└ cattimp <conditie>
└■┌ cattimp <conditie> executa
│ <instructiuni>
└■┌ daca <conditie> atunci
│┌ repeta
││ <instructiuni>
│└ panacand NOT <conditie>
└■┌ repeta
│ <instructiuni>
└ panacand <conditie><instructiuni>
┌ cattimp NOT <conditie> executa
│ <instructiuni>
└■