Like writing a pseudocode?

Asked

Viewed 4,035 times

10

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

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

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

In this code above, I don’t understand what this is var lost along the way, and it caused me some confusion.

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

3 answers

12


As far as I know, there’s no rule for writing pseudocode, so there’s no way correct to write it, it should only be written in a simple way, so that anyone with minimal knowledge in programming can understand it.

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

In the Wikipedia we have the following definition:

Pseudocode is a generic way of writing an algorithm, using a simple language (native to those who write it, in order to be understood by anyone) no need to know the syntax of any programming language.

Regarding the excerpt of code written in English, the var identifies the section for declaring the 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.

10

Pseudo : "Prefix used in word formation to express falsehood or similarity".

A pseudocode mimics 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...".

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

In the example you put forward 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.

8

Let’s get some definitions before we actually answer.

According to Wikipedia:

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

An example used in educational institutions 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 this case is, as if it were the English language Portugol that closely resemble a pseudocode.

Pascal:

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

Visualg:

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

The example you posted I believe is in visualg, and adopts the var, for declaring variables, because visualg needs to be interpreted in some way to be able to compile, but in the case of pseudocode, not compilable, the intention is to understand what it is trying to be transmitted, 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

Completion:
Thus, no matter what syntax you will use to write, with a pattern minino, and since it is possible for the receiver to understand what it wants to be said, it is valid.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.