Algorithms Pseudocode Examples
Algorithms Pseudocode Examples
I. Identify what is the input data, what are the output results that our algorithm needs to
provide:
What data needs the How to compute these What do we want the
user to input into inputs in order to get algorithm to
computer? the expected output? compute/to print
given the input?
What data types are What are the logical
these? and structured steps, What data types are
to take, intermediary these results?
Integers/Real results to compute for
Numbers/Characters? Integers/Real
obtaining the result?
Numbers/Characters?
ALGORITHM
II. Find some particular examples of solving the problem, given some input data:
Ex. Sum of first n natural numbers:
Input: n= 5;
Solution: Sum = 1 + 2 + 3 + 4 + 5 = 15
Input: n = 7
Solution: Sum = 1+2+3+4+5+6+7 = 28
III. Identify the variation of data in the found examples given the input all the way through
output. What are the intermediary values and results that leads to final result?
Input: n = 5; Sum = 1 + 2 + 3 + 4 + 5 = 15
Input: n=7; Sum = 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28
The result is sum of exactly n (input) numbers;
Intermediary values: 1,2,3,4,5 / 1,2,3,4,5,5,7
Intermediary results: 1+2=3-> 3+3 = 6 -> 6 + 4 = 10 -> 10 + 5 = 15 – final result
1+2=3-> 3+3 = 6 -> 6 + 4 = 10 -> 10 + 5 = 15 -> 15 + 6 = 21 -> 21 + 7 = 28 - final result
By identifying correctly, the intermediary values and results we can deduct What are the
variables required by our algorithm?
In the above example we would need a variable that will store the intermediary values and a
variable that would store the intermediary results/final results.
We can see that at some point when a certain condition is met, the intermediary result
becomes the final result. In our example this condition is met when we summed the first n
(input) numbers.
IV. Identify the rule of data variation found in above examples. Try to be as general as you can.
This can be a repetitive step/calculation or a repetitive logical check.
S = s + i, with i taking values from 1 to n and i = i + 1
V. Identify the limits of data variation, the initial values of variables and intermediary results,
the exception cases.
Initial values: s = 0; i = 1 – it assures a clean start for results computation
Limits: i varies from 1 to n – it provides the condition of repetitive structures, it answers an
important question: When the algorithm should stop? When the algorithm found the final
result?
Exceptional cases: if n = 0 then s = 0 – particular cases in which we cannot apply the rule we
found at previous step.
Program FibonacciFn;
READ n;
x1 = 0;
x2 = 1;
i = 2;
WHILE i <= n DO
xi = x1 + x2;
x1 = x2;
x2 = xi;
i = i + 1;
ENDWHILE
IF n == 0 THEN
PRINT “F(0) = 0 ”;
ELSE IF n == 1 THEN
PRINT “F(1) = 1”;
ELSE
PRINT “F(n)=” + xi; //print last calculated xi
ENDIF
END.
Generarea sirului Fibonacci
Program GenerateFibonacci;
READ n;
x1 = 0;
x2 = 1;
IF n == 0 THEN
PRINT x1;
ELSE IF n > 0 THEN
PRINT x1 x2;
ENDIF
i = 2;
WHILE i <= n DO
xi = x1 + x2;
PRINT xi; //print xi as it is calculated
x1 = x2;
x2 = xi;
i = i + 1;
ENDWHILE
END.
Program ArrayMax;
READ n; //n = nr of elements in array
i = 0;
V = INT[n]; // empty int array of n elements
WHILE i < n DO
READ V[i];
i = i + 1;
ENDWHILE
Max = V[0];
i = 1;
WHILE i < n DO
IF V[i] > Max THEN
Max = V[i];
ENDIF
i = i + 1;
ENDWHILE
PRINT “Maximum = ” + Max;
END.
Program ArrayAddEnd;
READ n; //n = nr of elements in array
i = 0;
V = INT[]; // empty int array
WHILE i < n DO
READ V[i];
i = i + 1;
ENDWHILE
READ x; //the new element to add
n = n + 1; // increase the number of elements in array with 1
V[n-1] = x; //last element will have value of x
i = 0;
WHILE i < n DO
PRINT V[i];
i = i + 1;
ENDWHILE
END.
Program ArrayAddToK;
READ n; //n = nr of elements in array
i = 0;
V = INT[n]; // empty int array of n elements
WHILE i < n DO
READ V[i];
i = i + 1;
ENDWHILE
READ x; //the new element to add
READ k; //the postion where to add
IF k < 0 OR k > n-1 THEN
PRINT “Out of the array”;
ELSE
BEGIN
m = n + 1; //new size of the new array
V1 = INT[m] //new empty array of m elements
i = 0;
WHILE i < m DO
IF i < k THEN
V1[i] = V[i]; //copy all elements from old array before k
ENDIF
IF i == k THEN
V1[k] = x; //put x on position k
ENDIF
IF i > k THEN
V1[i] = V[i-1]; //copy the elements from initial array
ENDIF
i = i + 1;
ENDWHILE
i = 0;
WHILE i < m DO //print new array V1 with x on position k
PRINT V1[i];
i = i + 1;
ENDWHILE
ENDIF
END.
Program ArrayReplaceK;
READ n; //n = nr of elements in array
i = 0;
V = INT[]; // empty int array
WHILE i < n DO
READ V[i];
i = i + 1;
ENDWHILE
READ x; //the new element to add
READ k;
IF k < 0 OR k > n-1 THEN
PRINT “Out of the array”;
ELSE
BEGIN
V[k] = x; // replace V[k]
i = 0;
WHILE i < n DO
PRINT V[i];
i = i + 1;
ENDWHILE
ENDIF
END.
Program StringContainsChar;
READ S; //read the string
READ c; //read char to search
i = 0;
found = FALSE; //suppose that we don’t find it
WHILE i < LEN(S) DO //LEN(S) returns the string length in number of sharacters
IF S[i] == c THEN
found = TRUE; //if element is found in string make found = TRUE
ENDIF
i = i + 1;
ENDWHILE
IF found == true THEN
PRINT “Character is found in input string”;
ELSE
PRINT “Character is not found in input string”;
ENDIF
END.