How to write a pseudocode?

10

I was reading about algorithms, but all the algorithms I read, were written in some unknown language or not invented, but somehow I could understand what was written there.

I read about Portugol , a "language" written in Portuguese that was made only to be interpreted by the human being, that would be a hello world:

Algoritmo "OlaMundo"
var
inicio
Escreval("Olá, Mundo!")
Fimalgoritmo

In this code above, I do not understand what this var is missing in the middle of the path, and this caused me some confusion.

My question is: how to correctly write a pseudocode, so that all programmers can understand what I'm trying to interpret? Is there a defined syntax for this? Can I write as I want, in such a way that I can understand?

    
asked by anonymous 03.08.2017 / 07:38

3 answers

10
As far as I know, there is no rule to write a pseudocode, so there is no correct way to write it, it should only be written in a simple way, making anyone with knowledge minimum in programming can understand it.

Note that even using conventions of a normal programming language, the pseudocode is intended exclusively for human reading .

At Wikipedia we have the following definition:

  

Pseudocode is a generic way of writing an algorithm, using a simple language ( native to someone who writes it in a way that anyone can understand) know the syntax of any programming language.

With regard to the code snippet written in Portuguese, var identifies the section for declaration of variables that the program will use. In the example in question, the var could be omitted without any problem, the code compiles normally in the Visualg .

    
03.08.2017 / 14:07
9

Pseudo: "Prefix used in the formation of words to express falsehood or similarity."

A pseudocode imitates a real code, but it is not. It is a representation of an algorithm, which in turn: "... is a finite sequence of well-defined and unambiguous instructions ...".

Each one can write its own pseudo-code for its own purpose, but here we are talking about an algorithm that tries to imitate a program that will be executed by an interpreter / compiler x, which has its own syntax and semantics rule to define the actions to be taken.

In the example you put, we could translate from pseudo-code (Portugol) to the Portuguese language like this:

Algoritmo "OlaMundo"{Define o nome do algorítimo como OlaMundo}
var{palavra chave para declarar uma ou mais variáveis nesse caso está vazio é desnecessário e confunde mesmo, se o VisualG precisar disso vazio eu vejo como erro}
inicio{palavra chave para marcar o inicio do algorítimo}
Escreval("Olá, Mundo!"){Escreva na tela Olá, Mundo!}
Fimalgoritmo{palavra chave para marcar o fim do algorítimo}

The actual algorithm in PHP for example would be:

<?php
$variavel = "Olá, Mundo!";//Atribuo a variável uma string Olá, Mundo! 
echo $variavel;//echo é o comando que emite a string passada como argumento
?>

Every language has its documentation, containing its rules.

The VisualG is based on Portugol, see its documentation.

    
03.08.2017 / 14:26
7

Let's get some definitions before actually answering.

Second Wikipedia :

  

Pseudocode is a generic way of writing an algorithm, using a simple language (native to the writer, so   be understood by any person) without the need to know the   syntax of any programming language.

An example used in schools is the compiler Visualg , which facilitates the illustration of an algorithm, so that all programmers can understand them (regardless of the language they use).

In my time I learned in Pascal, which in the case is, as if it were the English-language Portuguese that resemble a pseudo-code enough.

Pascal:

program OlaMundo;
begin
 WriteLn('Olá, Mundo!');
end.

Visualg:

Algoritmo "OlaMundo"
inicio
Escreval("Olá, Mundo!")
Fimalgoritmo

The example you posted, I believe it is in visualg, and it uses var , for declaration of variables, because the visualg, somehow need to interpret to compile, but in the case of pseudocode, not compilable, the intention is to understand what this is trying to convey, another example:

INÍCIO
VARIÁVEIS
S,C,I,A,MD:Real;
S ← 0;
C ← 0;
PARA I de 1 ATÉ 10 FAÇA PASSO 1
    Escreva "Digite um número: ";
    LEIA A;
    SE A ≥ 0 ENTÃO
         S ← S + A;
         C ← C + 1;
    FIM SE;
FIM PARA;
MD ← S / C;
ESCREVER ("A média é: ", MD);
FIM

Conclusion:
So, it does not matter what syntax you're going to use to write, as long as the recipient is able to understand what you're saying.

    
03.08.2017 / 14:36