Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Lecture#01:
Introduction, Algorithmic Structures
Lecture Contents
History
Definition
Algorithm Examples
Algorithm & Programming
Algorithms, Data Structures, Implementation Issues
Random Access Machine for Modeling Algorithms
Algorithmic Structures
Indentation,
Loop Structures & End of Loops
Comments
Arithmetic Expressions & Assignments
Swapping,
GOTO & Return Statement,
Statement End,
No Global Variables,
Array Length,
Parameters Passing
Example:
Proc calculateFactor(a, b, c as integers)
factor (-b+sqrt(power(b, 2) – 4*a*c))/(2*a)
//In above line, we calculate one factor of quadratic equations whose
//coefficients as provided as parameters
Return factor
End findSum
Algorithmic Structures
GOTO Label
GOTO statement with label defines transfer of control while execution
Algorithmic Structures … Procedure Boundaries
Procedure Start and End Proc isPrime(value)
factor as integer
A procedure starts with the keyword proc
followed by procedure name If value = 2 then
return true
End Procedure-Name indicates the end of
procedure body end if
factor 2
Return statement is used to return control and
while factor <= value / 2
result to caller statement
do
if value mod factor = 0 then
return false
end if
factor factor + 1
end while
return true
End isPrime
Algorithmic Structures … Statement End
End of line is statement terminator
No semi-colon (like C++/Java) is used at end of statements
Example:
Proc findSum(a, b, c as integers)
Sum a + b + c
Return Sum
End findSum
Algorithmic Structures … No Global Variables
All variables used in algorithms are local to procedures
If global variables are used (due to any reason), they will be explicitly
mentioned
Algorithmic Structures … Array Elements
Array Elements
Array elements are accessed same as C / C ++ syntax e.g. values[5]
Array index starts with 1 instead of 0
Example:
Proc printAll(A as array)
for x = 1 to |A| do
print A[x]
End-for
End findSum
Algorithmic Structures … Parameter Passing
All parameters to a function are passed by value i.e. any change in
parameter values does not affect actual variables
Algorithmic Structures … Structure Size
If S is a structure (an array or a collection), |S| is the size of structure
Algorithmic Functions … Ceil, Floor
Example:
x 5.3
Ceil will return 6
Floor will return 5
Algorithmic Functions … Power
Expression Simplification
Xa.Xb Xa+b
Xa/Xb Xa-b
(Xa)b Xab
Xa+Xa 2Xa
2a.2a 22a
Algorithmic Functions … Logarithmic Expressions
Expression Simplification
X = log2 y 2x=y
Logab logcb / logca
log ab log a + log b
log a/b log a – log b
(log a)b b log a