In theprevious section, algorithms were presented in English. However, this is not the only way of specifying and documenting an algorithm. In this section, you will learn about four notations for algorithm representation, namelynatural language,flowchart, pseudocode, andprogramming language.
To make thistask easier to understand, you will specify the algorithm for calculating anarithmetic mean in all of these notations. As a reminder, the mean can be calculated using thefollowing formula:
Figure 2.2 – Formula for calculating an arithmetic mean
As youcan see, two inputs are used, namely the provided numbers (a) and the total number of elements (n). If no numbers are provided,null
is returned, indicating that no mean is available. Otherwise, you sum the numbers and divide them by the total number of elements to getthe result.
Natural language
First, let’s specify the algorithm using a natural language. It is a very easy way of providinginformation about algorithms, but it can be ambiguous and unclear. So, let’s describe our algorithm inthis way:
The algorithm reads the input, which represents the total number of elements from which an arithmetic mean will be calculated. If the entered number is equal to 0, the algorithm should return null. Otherwise, it should read the numbers in the amount equal to the expected total count. Finally, it should return the result as the sum of numbers divided bytheir count.
Quite simple and understandable, isn’t it? You can use this notation for simple algorithms, but it can be useless for complex and advanced algorithms. Of course, some descriptions in the natural language are often useful, regardless of the complexity of an algorithm. They can give you a brief understanding of what the aim of the algorithm is, how it works, and what aspects should be taken into account while you’re analyzing or implementingthe algorithm.
Flowchart
Another wayof presenting an algorithm is via aflowchart. A flowchartuses a set of graphical elements to prepare a diagram that specifies the algorithm’s operation. Some of the available symbols areas follows:
Figure 2.3 – The available symbols while designing a flowchart
The algorithm should contain theentry point and one or moreexit points. It can also contain other blocks, includingoperation,input,output, orcondition. The following blocks are connected witharrows that specify the order of execution. You can alsodrawloops.
Let’s take a look at a flowchart for calculating thearithmetic mean:
Figure 2.4 – Flowchart for calculating the arithmetic mean
The execution starts in theSTART
block. Then, we assign0
as a value of thesum
variable, which stores the sum of all the entered numbers. Next, we read a value from the input andstore it as a value of then
variable. This is the total number of elements used to calculate the arithmetic mean. Next, we checkwhethern
is equal to0
. If so, theYES
branch is chosen,null
is returned to the output, and the execution stops. Ifn
is not equal to0
, theNO
branch is chosen and we assign0
as a value of thei
variable. It stores the number of elements already read from the input. Next, we read the number from the input and save it as a value of thea
variable. The following operation block increasessum
by the value ofa
, as well as increments the valueofi
.
The next block is a conditional one that checks whetheri
is not equal ton
, which means that the required number of elements is not read from the input yet. Ifi
is equal ton
, theNO
branch is chosen and a value of theresult
variable is set to a result of a division ofsum
byn
. Then, theresult
variable is returned and the execution stops. An interesting construction is used when the conditional expression evaluates totrue
, which means that we need to read another input. Then, the loop is used and the execution comes back just before the input block for readinga
. Thus, we can execute some operations multiple times, until the conditionis met.
As you can see, a flowchart is a diagram that makes it possible to specify a way of algorithm operation in a more precise way than using natural language. It is an interesting option for simple algorithms, but it can be quite cumbersome in the case of advanced and complex ones, where it is impossible to present the whole operation within a diagram of a reasonablysmall size.
Pseudocode
The nextnotation we’ll look at ispseudocode. It allows you to specify algorithmsin another way, which is a bit similar to the code written in a programming language. Here, we use the English language to define inputs and outputs, as well as to present a set of instructions clearly and concisely, but without the syntax of anyprogramming language.
Here’s some example pseudocode for calculating thearithmetic mean:
INPUT:n – total number of elements used for mean calculation.a – the following numbers entered by a user.OUTPUT:result - arithmetic mean of the entered numbers.INSTRUCTIONS:sum <- 0read nif n = 0 then return nullendifi <- 0do read a sum <- sum + a i <- i + 1while i <> nresult <- sum / nreturn result
As youcan see, the pseudocode provides uswith a syntax that is easy to understand and follow, as well as quite close to a programming language. For this reason, it is a precise way of algorithm presentation and documentation that can be later used to transform it into a set of instructions in our chosenprogramming language.
Programming language
Now, let’s look at the last form of algorithm notation:programming language. It is very precise, can be compiled and run. Thus, we can see the result of its operation and check it using a set of test cases. Of course, wecan implement an algorithm in any programming language. However, in this book, you will see only examples in theC# language.
Let’s take a look at the implementation of the meancalculation algorithm:
double sum = 0;Console.Write("n = ");int.TryParse(Console.ReadLine(), out int n);if (n == 0) { Console.WriteLine("No result."); }int i = 0;do{ Console.Write("a = "); double.TryParse(Console.ReadLine(), out double a); sum += a; i++;}while (i != n);double result = sum / n;Console.WriteLine($"Result: {result:F2}");
The preceding code contains anif
conditional statement and ado-while
loop.
If we run the application, we need to enter the number of elements from which we would like to calculate the arithmetic mean. Then, we will be asked to enter the numbern
times. When the number of provided elements is equal to the expected value, the result is calculated and presented in the console,as follows:
n = 3a = 1a = 5a = 10Result: 5.33
That’s all! Now, you know what algorithms are, where you can find them in yourdaily life, as well as how to represent algorithmsusing natural language, flowcharts, pseudocode, and programming languages. With this knowledge, let’s proceed to learn about different types of algorithms, including recursive andheuristic algorithms.