0% found this document useful (0 votes)
7 views11 pages

Best Practices for Algorithm Development

The document outlines common programming mistakes and best practices, emphasizing the importance of maintaining variable integrity during loops, proper input handling, and the distinction between functions and procedures. It also provides standard algorithms for mathematical operations, array manipulations, and guidelines for using loops and parameterized actions effectively. Key points include avoiding syntax errors, ensuring correct data types, and maintaining clarity in algorithm design.

Uploaded by

thesickslicer
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views11 pages

Best Practices for Algorithm Development

The document outlines common programming mistakes and best practices, emphasizing the importance of maintaining variable integrity during loops, proper input handling, and the distinction between functions and procedures. It also provides standard algorithms for mathematical operations, array manipulations, and guidelines for using loops and parameterized actions effectively. Key points include avoiding syntax errors, ensuring correct data types, and maintaining clarity in algorithm design.

Uploaded by

thesickslicer
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

NOTES (Common mistakes)

1- never assign and change the values of N or i in a loop !!


since they're needed for itertions.
instead, use intermidiate variables if you need to use their values.

2- never forget to read the size N on two conditions :


Until (N>0 AND N<= [declared size] )

3- never forget to read the array elements

4- never consider a matrix square if not told so !!

5- Always check the algorithm progress (déroulement)


with 1st, last and a random elements to avoid serious errors.

6- always recheck the declared types and array sizes.

7- Don't change the names of already given-name variables

8- if we have to verify if an array has undergone changes we don't have to


save an org array and compare ❌
we declare a flag, and if we change on the array we change the flag’s
value, and that's just what we'll compare at the end ✅
9- Don't confuse a Function & a Procedure!!
always choose a Function for a single output result, whatever type it
is, even boolean !!
In calling, a function must be assigned to a variable of the same type in
the main algorithm. whereas, calling a Procedure is already assigning
values to Global Variables.

\\ beware syntax mistakes !! \\


SOME STANDARD ALGORITHMS

1- product calculator (a*b)


P <- 0 ;
for i <- 1 to b
P <- P + a;
endfor;

2- power calculator (a^n)


P <-1;
for i <- 1 to n
P <- P*a;
endfor;

3- integer division (q = a/b , r : remainder)


// condition b <> 0
q <- 0; r <- a;
while (r <> 0) do
r <- r - b;
q <- q +1;
end while;

4- determining all divisors of n


// without considering 1 and n itself
// don't forget all the negative equivalents
for i <- 2 to n div 2 do
if (i mod n = 0) then
i is divisor;
end if;
end for;

5- determining if A is prime
bool <- true;
for i <- 2 to A div 2 do
if (A mod i = 0) then
bool <- false
end if;
end for;
6- extracting digits off a decimal number A
while (A <> 0)
digit <- A mod 10;
A <- A div 10;
end while;

7- inversing a decimal number A


inv <- 0;
while (A <> 0) do
inv <- inv*10 + A mod 10;
A <- A div 10;
end while;

8- if we want to display 123456


Going like :
6
56
456
3456
23456
123456

We notice unlike inversing, the old digit is keeping it's position


instead of augmenting by *10
Thus, the new Digit is the one getting augmented in position :
num <- num + n*10;
Tho In inversing it would be :
Inv <- inv*10 + num;
NOTES

1- String declaration : Type = String[length]


max size = 255
// strings are treated exactly like Arrays
exepct we can choose to display it as an entire element

2- Never take the input for granted!!


• always put in consideration when the input value is 0 or negative
and how it affects our actions.
• Depending on input, either make cases or force it to be one
specific case.
• And always consider the interval you're working in

3- in comparaison, always give the Min, Max the first value in the
variables series / array and then compare with the remaining
elements

4- when using else if, always consider what the else case will be.

5- if a condition is made of multiples conditions, we combine all


conditions using OR and AND logical operators, instead of treating
each case separately unnecessarily.
Ex :
Write : If ( A AND B OR C)
Instead of :
if (A) then
If (B) then
If (C) then
6- mostly
a Sum S is always initiated to O
A Product P is always initiated to 1
{In particular cases we might need to initiate the
first element to avoid mathematical errors}

7- if we are given CASE choices


we first calculate everything regardless of choices,
in the end we display based on choice Cases

8- always keep an original (org) variable to save the value of a variable


that'll be used but needed in comparaison later!!

~~~~~~~~~~~~~~~~~~
increasing = ascending order (smaller to larger)
decreasing = descending order (larger to smaller)
~~~~~~~~~~~~~~~~~~

pow(a,n) : a^n
/ : real division
div : integer division
abs(n) : absotute value
sqrt(n) : square root
LOOPS

N : number of iterations
N = (valfin - valint) / n + 1 // n : steps
if N <= 0 : 0 iterations

1- if we are given an interval (n to m), consider the larger and smaller number
before setting the loop counter.

2- whatever mathematical Sum Expression we are given,


it's always going to be a For loop from the first element to n
(first element is often 1)

But consider the function condition :

example :

In devison, divisor must never be 0


division of two integers output = real
For ln function : input x < 0 (N*) | output : real
For sin & cos functions : input real | output [-1;1]
Square (X²) : input real | output x ≥ 0 (N)

4- choosing only odd nums


for i <-1 to n step 2 do
choosing only even nums
for i <-0 to n step 2 do

5- choosing between For , while & repeat loops :


• if we only have N known iterations : For loop
• if we have a logical condition :
1- if we want to excute the algo at least once : Repeat loop
2- if No : while loop
// don't forget incrementation i <- i +1
6- simplify the mathematical operation :
if the number is being powered :
it's always p <- p*x;
if the sign changes :
it's always sign <- -sign;

7- when asked to make a calculation until a certain value is reached :


we use repeat loop with a condition :
Until (<next iteration output value doesn't exceed the limit>)
Ex :
Repeat
x=x*2
Until (x*2 >= C)
//

8- a sequence Un is simply a For loop


ARRAYS

// no need for ''write' instructions for reading Size


and elements of arrays anymore (time waste)
// but always display at the end

1- to calcul the greatest difference in an array find the max and min and
substract max-min

2- reversing elements of an array


X <- N+1;
for i <- 1 to N div 2
Tmp <- T[i] ;
T[i] <- T[M-i] ;
T[M-i] <- Tmp ;
endfor;

~~~~~~~~~~~~~~~~~~
Dot product = sum of all elements
just Product / sum or substraction = extract new array
~~~~~~~~~~~~~~~~~~

3- when extracting a new array :


we initialiate k <- 0;
we assign K <- k +1 if our extracting condition is true.
k is both a counter at first and the size of the extracted Array at the end.

4- when deleting values from an array :


• we decrement N (N <- N -1) with each true condition (deleted value)
• And since it took the place of the deleted value, we recheck the new assigned
variable value :
we either :
Using for loop : decrement i (i <- i - 1) with each iteration
Or
using while loop : only increment i (i <- i + 1) if the codition is false (didn't delete)
// so it automatically always rechecks
5- when extracting an array from another array :
1- Vector :
• default : we declare with the vector's size.
• if the extracted array is a merging of Vectors , we add the sizes
2- matrix :
• default : the size of N*M (all cells)
(Might be less depending on the extracting conditions)
• extracting elements off a Square matrix diagnol : Size of N
• extracting elements above/below the diagnol : N*M / 2

6- when displaying a matrix


use the comment // return to the line as a " \n"

7- if we don't have the size of the array we extract it : length (T)

8- if we are filling an array A with repeated values in array B,


we must be warry of filling the same value again, thus we need to verify if the
array A already has that value, if not we fill it.
We verify using a while/repeat loop
Parameterized Actions

1- if the output is one, always choose a Function


// rather return a boolean variable instead of
displaying message using a Procedure

2- if our algorithm need for example 4 inputs


and we were forced to write a 2 input PA, we can call the same PA twice
and combine it in a way to get the desired result

3- better way to fill a matrix :


For i 1 to N do
For j 1 to N do
If i = j Then // fill the diagnol
End If;
If i < j Then // fill above the diagnol
End If ;
If i > j Then // fill below the diagnol
End If ;
EndFor ;
EndFor ;

4- Better way to transpose a Matrix :


For i 1 to N do
For j 1 to N do
B[j,i] A[i,j] ;
EndFor ;
EndFor ;

5- it’s better to use a Procedure for arrays,


in case we are asked to give their sizes as outputs as well.
also, we can’t always assign a function to an array
6- a Function can actually display a message (mostly error messages)
if it couldn't return the desired value.
ex :
if (condition) then
Sum <- Sum + n;
else
write ('error');
endif;

nothing will be assigned to the main algorithm Variable,


instead, an error message will be displayed.

7- A PA can call another PA

8- in an exam exo, we don't rewrite the PA twice.


if we are asked to use it in the main algo, In the first question we write :
Function * <name function>
<Actions>
end; *
Then in Declaring in the main Algo we write : Function *...*
To each PA it's symbole : ‘*’ or ‘+’ or ‘-’ etc...

10- a defined type can be used everywhere in our PA or main algorithm

11- we can either use the <Function name> withing the function bloc of
actions directly and return nothing before end;
or
use an intermediate variable X then return X; before end;

12- better declare the defined types twice, when using PAs
and again in the main Algorithm

13-better not Display the resuts in the procedure itself

You might also like