Trace an algorithm
- a01337106
- 9 feb 2015
- 1 Min. de lectura
There are a lot of types of algorithms as we saw on the last post. But, how is an algorithm in computer sciences? There are different languages in which an algorithm can be written.
Let's se an example for pseudocode:
1. int a=0;
2. loop I from 0 to 5
3. a=a+2;
4. end loop
The return of this algorithm will be a=12.
The explanation:
In the first line an integer a is declared with a value of cero.
Then it enters in a loop named I that repeats 6 times, from 0 to 5.
The integer a will act as an acomulator inside the loop I
Step by step the algortihm will be set as this:
First loop:
I=0
a=2
Second:
I=1
a=4
Third:
I=2
a=6
Fourth:
I=3
a=8
Fifth:
I=4
a=10
Sixth:
I=5
a=12
As we can see the loop increases by one each time and the acomulator a increases by 2 as estipulated inside the loop.
Comentarios