Skip to content

Latest commit

 

History

History
353 lines (259 loc) · 8.03 KB

File metadata and controls

353 lines (259 loc) · 8.03 KB

Romanian Pseudocode

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: π.

Operations

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 DIV and MOD for division and modulus. Also there is a possibility that operations of raising to a power a^n and square root might appear sqrt(a).

Relational operations

  • = - 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

Logical operations

  • NOT
  • AND
  • OR

These all have their usual semnifications.

Instructions

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

Input

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.

Output

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 = 7

Attribution

To attribute a value to a variable we use <variabilă> ← <expresie>

a ← 0
S ← a + b
i ← i + 1

Alternative structure

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> from altfel

Repetitive structures

Known number of steps

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:

S0; P1pentru i1,n executaSS + iPP * i
└■
scrie S, ' ' , P

Unknown number of steps and initial test

The 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
S0cat timp n0 executaSS + n % 10n ← [n/10]
└■
scrie S

Unknown number of steps and final test

The 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
cnt0executacntcnt + 1n ← [n/10]
└ cat timp n0
scrie cnt

Repeat ... until

This 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
cnt0repetacntcnt + 1n ← [n/10]
└ pana cand n = 0
scrie cnt

Equivalence of repetitive structures

Numerous 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 → CÂTTIMP

pentru ia,b executa<instructiuni>
└■
iacattimp ib executa<instructiuni>ii + 1
└■

PENTRU → EXECUTĂ … CÂTTIMP

pentru ia,b executa<instructiuni>
└■
iadaca ib atunci
│┌ executa
││     <instructiuni>
││     ii + 1
│└ cattimp ib
└■

PENTRU → REPETĂ … PÂNÂCÂND

pentru ia,b executa<instructiuni>
└■
iadaca ib atunci
│┌ repeta
││     <instructiuni>
││     ii + 1
│└ panacand i > b
└■

CÂTTIMP → EXECUTĂ … CÂTTIMP

cattimp <conditie> executa<instructiuni>
└■
daca <conditie> atunci
│┌ executa
││     <instructiuni>
│└ cattimp <conditie>
└■

CÂTTIMP → REPETĂ … PÂNÂCÂND

cattimp <conditie> executa<instructiuni>
└■
daca <conditie> atunci
│┌ repeta
││     <instructiuni>
│└ panacand NOT <conditie>
└■

REPETĂ … PÂNĂCÂND → CÂTTIMP

repeta<instructiuni>panacand <conditie>
<instructiuni>cattimp NOT <conditie> executa<instructiuni>
└■