Pseudo code for writing algorithms .

Pseudo code for writing algorithms . Using a pseudo code instead of a completely formal language (such as Fortran, Basic, Pascal, C, Matlab, etc.) has several advantages: on the one hand, a pseudo code is not subject to such strict syntax rules and there is no than to sacrifice the reader’s preference for one language or another. On the other hand, the pseudo code to be used is close to most of the computer languages ​​in use, so it will not be difficult for the interested reader to translate the algorithms into the language of their choice.

Summary

[ hide ]

  • 1 The assignment operator
  • 2 The alternative structure
  • 3 Repetitive structures
  • 4 Example 1
    • 1 Solution:
  • 5 Example 2
    • 1 Solution:
  • 6 Sources

The assignment operator

The sign: = will be used to indicate that the expression that appears on the right must be assigned to the variable that appears on the left. For example, Area  : = 45 means that the variable called Area must be assigned the value 45; n  : = n – 1, indicates that the variable named n must be assigned the value that the expression n – 1 takes. Notice that this symbol indicates an order, not an equality relation.

The alternative structure

It is used to indicate that, if one condition is met, one sequence of actions is executed and if not, another one is executed. Its general structure is as follows:

Sometimes sequence 2 of actions is not needed and in that case the word else is omitted.

Repetitive structures

They serve to repeatedly execute a sequence of actions. The sequence is repeated until a certain condition is satisfied. Three types of repetitive structures will be used: do – While , repeat – until and for.

The do – while structure has the form:

The sequence of actions runs over and over as long as the condition remains true. As soon as the condition is no longer met, the execution of the algorithm passes to the instruction that follows the word end. Unlike the do – while structure. In which the veracity of the condition is analyzed as a prerequisite for entering the cycle, the repeat – until structure allows you to directly enter the corresponding sequence of actions and, after executing these actions, the validity of a condition is analyzed for allow or not repeat the sequence of actions.

The repeat – until structure has the form:

As noted, the word end is not required here to indicate the end of the sequence of actions. since the word until performs this function. After executing the actions of the sequence, it is analyzed if the condition is true and, if it is, the statement that follows the word until is executed. In other words, the sequence of actions runs over and over, until the condition is met.

The repetitive structure for , allows repeating a sequence of actions a number of times that is previously fixed, under the control of a variable that takes consecutive integer values ​​from an initial number to a final number, both prefixed.

The for structure has the form:

The three repetitive structures are not really essential. The same algorithm, in general, can be expressed using one or the other. The objective of using the three forms is to seek clarity and brevity in each case.

Below are examples of algorithms written with the pseudo code adopted.

 

Example 1

Write a pseudo code to get the real roots of a quadratic equation: ax 2 + bx + c = 0 ( a ≠ 0) using the general formula.

Solution:

As is known, if the discriminant D = b 2 – 4 ac is negative, the roots are imaginary. Otherwise the roots are real and are obtained as:

The calculation algorithm will be as follows:

Example 2

Perform in pseudo code an algorithm that allows obtaining the elements of an arithmetic progression of increment d > 0 and initial value a, that are less than or equal to a certain known value

Solution:

The elements of the progression will be obtained using the formula: x k ; = x k -1 ; for k = 2,3, 4 …. with x 1 = a The calculation algorithm can be expressed like this:

 

Leave a Comment

Scroll to Top