0% found this document useful (0 votes)
62 views

Algorithms Pseudocode Examples

This document outlines steps for developing algorithms: 1. Define the problem by specifying inputs, outputs, and the goal 2. Provide examples of solving the problem for different inputs 3. Identify patterns in how the examples transform inputs to outputs 4. Generalize the pattern into a rule of variation 5. Specify limits on the rule and initial variable values 6. Combine the analysis into a structured algorithm The document provides several examples of applying this process to problems like summing numbers, Fibonacci sequence, and array operations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

Algorithms Pseudocode Examples

This document outlines steps for developing algorithms: 1. Define the problem by specifying inputs, outputs, and the goal 2. Provide examples of solving the problem for different inputs 3. Identify patterns in how the examples transform inputs to outputs 4. Generalize the pattern into a rule of variation 5. Specify limits on the rule and initial variable values 6. Combine the analysis into a structured algorithm The document provides several examples of applying this process to problems like summing numbers, Fibonacci sequence, and array operations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Method:

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.

VI. Put it all together in a structured algorithm:


1. GET the input data and put it into a variable: n=5
2. Treat the Exception cases – if it is the case
3. Make necessary initialization of variables which holds the intermediary values and/or
intermediary results: i=1; s = 0;
4. Apply the rule of variation repetitively while the limits/condition to stop are not met
(the challenges here are: identify the right conditions considering the identified limits –
when the final result is found and write the repetitive step that provides intermediary
results and leads to final results)
5. Provide the final results to the user

1. Compute the sum of numbers from 1 to n


PROGRAM SumN;
READ n;
IF n == 0 THEN
PRINT “Sum = 0”;
ELSE
BEGIN
i = 1;
s = 0;
WHILE i <= n DO
s = s + i;
i = i + 1;
ENDWHILE
PRINT “Sum = ” + s; // + este operatorul de concatenare pentru stringuri: “a” + “b” = “ab”
ENDIF
END.
2. Compute the sum of even numbers from 1 to n
PROGRAM SumEvenN;
READ n;
IF n == 0 THEN
PRINT “Sum = 0”;
ELSE
BEGIN
i = 1;
s = 0;
WHILE i <= n DO
IF s % 2 == 0 THEN // % - este operatorul modulo si returneaza catul impartirii intregi – 5%2 = 1
BEGIN
s=s+i;
ENDIF
i = i + 1;
ENDWHILE
PRINT “Sum = ” + s; // + este operatorul de concatenare pentru stringuri: “a” + “b” = “ab”
ENDIF
END.

3. Compute the sum of numbers from n to m


PROGRAM SumNM;
READ n;
READ m;
Max = 0;
Min = 0;
IF n > m THEN
Max = n;
Min = m;
ELSE
Max = m;
Min = n;
ENDIF
i = Min;
s = 0;
WHILE i <= Max DO
s = s + i;
i = i + 1;
ENDWHILE
PRINT “Sum = ” + s;
END.
4. Compute n!
Program FactorialN;
READ n;
IF n == 0 THEN
PRINT “0! = 1”;
ELSE
BEGIN
p = 1;
i = 1;
WHILE i <= n DO
p = p * i;
i = i + 1;
ENDWHILE
PRINT “n! = ” + p;
ENDIF
END.

5. Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 …


Compute F(n), knowing that
F(n) = F(n-1) + F(n-2)
F(0) = 0
F(1) = 1

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.

6. Print all the elements of an array


Program ArrayInputOutput;
READ n; //n = nr of elements in array
i = 0;
V = INT[n]; // empty int array of n elements
WHILE i < n DO
READ x; //or we can just READ V[i] directly
V[i] = x;
i = i + 1;
ENDWHILE
i = 0; //reinitialize index i for print array elements
WHILE i < n DO
PRINT V[i]; //print element one by one
i = i + 1;
ENDWHILE
END.

7. Find the maximum number in an array

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.

8. Add an element in an array at the 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.

9. Add an element in an array at position K.

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.

10. Replace an element in an array at position K.

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.

11. Determine if a string contains a certain character

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.

You might also like