Algo Pekan 02
Algo Pekan 02
INFORMATICS ENGINEERING
SEKOLAH TINGGI TEKNOLOGI BANDUNG
2019
PSEUDO-CODE
Pseudo-code is an informal way to EXPRESS THE DESIGN OF A
COMPUTER PROGRAM OR AN ALGORITHM. The aim is TO GET THE
IDEA QUICKLY and also easy to READ WITHOUT DETAILS.
There are several ways of writing pseudo-code; there are no strict
rules. But to reduce ambiguity between what you are required to do
and what you express let’s base the pseudo code on the few defined
conventions and carry out the exercises.
EXAMPLE : SORT
SORT
Taking the sorting example; let’s sort an array using the BUBBLE SORT
technique.
BUBBLE SORT : Repeatedly steps through the list to be sorted,
comparing each pair of adjacent items and swapping them if they are in
the wrong order
void ArraySort(int This[], CMPFUN fun_ptr, uint32 ub)
{ /* bubble sort */
What’s easier to understand, the
uint32 indx; uint32 indx2;
implementation in C or pseudo-code?
int temp; int temp2; int flipped;
if (ub <= 1)
return;
Set n to number of records to be sorted
indx = 1;
Do { flipped = 0; repeat
for (indx2 = ub - 1; indx2 >= indx; --indx2) flag = false;
{ temp = This[indx2]; for counter = 1 to n-1 do
temp2 = This[indx2 - 1]; if key[counter] > key[counter+1] then
if ((*fun_ptr)(temp2, temp) > 0)
swap the records;
{ This[indx2 - 1] = temp;
set flag = true;
This[indx2] = temp2;
flipped = 1;
end if
} end do
} n = n-1;
} while ((++indx < ub) && flipped); until flag = false or n=1
}
repeat
set a flag to False
for each pair of keys The main part is that it is
important to PROVIDE
if the keys are in the wrong order then EASY TO READ BUT
swap the keys PRECISE instructions; this
set the flag to True will KEEP THE DESIGN
SIMPLE AND
end if UNAMBIGUOUS.
next pair
until flag is not set.
Taking a practical example the following instructions:
(a) Take a left, then take a right, go down the stairs, on your right enter
the kitchen, pick a cup and pour some hot water and add some hot
chocolate….
OR
(b) Please make me a hot chocolate.
The above line of instruction depends on the reader, some prefer to (a)
if not experienced while others prefer (b) because it nails it to the
point. It is pretty concise too
EXAMPLE : FIBONACCI
int main( )
{ DECLARE AND INITIALISE ALL THE VARIABLES
int n, k, f1, f2, f;
if ( n < 2 ) return n; Declare an integer variable called n // n is the
else { numberlimit
f1 = f2 = 1; Declare an integer variable sum // f is the sum
for(k=2;k<n;k++) Declare an integer variable f1 // f1 is temporary
{ storage
f = f1 + f2; Declare an integer variable f2 // f2 is temporary
storage
f2 = f1; set loopcounter to 2 // assigning the variables
f1 = f; declared above to values
} set sum to 0
return f; set f1 and f2 to 1
} set n to 50
More simple sollution
Instead of an equal sign an arrow repeat n times
sign ← can also be used sum = f1 + f2
f2 = f1
f1 = sum
print sum
end loop
1. Declare an integer variable called n If all the above sections
2. Declare an integer variable sum
of pseudo code are put
3. Declare an integer variable f1
4. Declare an integer variable f2 together we will get
5. set sum to 0 something looking like
6. set f1 and f2 to 1 the example below :
7. set n to 50
8. repeat n times
a. sum = f1 + f2
b. f2 = f1
c. f1 = sum
d. print sum
9. end loop
QUIZ 2
Write the pseudocode
THANKS