al_eng_TTY_15ab_slaidi_alg_c_

advertisement
Creating and implementing algorithms
Vladimir Viies, Lembit Jürimägi, Margit Aarna
viis@ati.ttu.ee
Tallinn 2015
Knowledge and skills
logical thinking
sub-tasks
algorithm
writing the code
POINTS
practice
homework
tests
bonus
ALGORITHM CREATING 1
What is an algorithm?
Why use it?
How to use it?
WHAT IS AN ALGORITHM WHEN
SOLVING TASKS WITH COMPUTERS?
Instructions for solving the task.
but not just any instruction,
instructions with certain properties.
Examples:
1. Task: Find the largest number from {N} numbers
Instructions that aren’t an algorithm: Compare the numbers and choose the
largest number
Instructions that can be an algorithm :
1. Read the numbers (remember, write down etc.) – enter numbers.
2. Take the first number and mark it.
3. Compare marked number with next number(s) until a larger number
is found or it has been compared with every number.
4. If during the comparison a larger number is found then mark it and
repeat step 3.
Examples
2. Task: win 1 million in a lottery game
Instructions that aren’t an algorithm: buy a ticket until you win.
Instructions that can be an algorithm: there is none.
3. Task : exchange 100 euro bank note into smaller bank notes
Instructions that can be an algorithm: We have to specify more
details. We need to explain, what “into smaller” means,
which banknotes and how many we can use. If the task is
vague then we need to put restrictions into place ourselves
and consider these restrictions when creating an algorithm.
ALGORITHM CREATING 2
THE ALGORITHM PROPERTIES
• Must specify inputs and outputs which
are interrelated
• It should provide a solution in finite time
• Must be unequivocal
......
ALGORITHM CREATING 3
ALGORITHM VISUALISATION
Algorithm graphical
presentation tools 1
(flowchart)
Algorithm graphical presentation tools 1
There are two types of algorithms editors:
I permit only drawing the algorithm ( eg. MS
Excel, UML activity…);
II additionally generate basic program code in the
selected language (eg. SFC, JSP….).
SFC elements of the schemes(1)
Algorithm with such a layout
always has one start and one end
All activities will take place in
sequence!
Each kind of activity has its own
notation.
This diagram shows the I / O,
conditional and operation
activities.
Task: find the total count of even
and odd numbers in a row of
numbers.
.
Algorithm (for writing program
code):
1. Insert data
2. Assign initial values to the counters
3. Let’s start comparing the numbers
in a loop and update the counters
4. Print out the results
Algorithm realization, the language choice
Mis on algoritm?
// Author:
viies // Course:
iag0581
void main ()
{
Editor SFC
generated
immediately the
code in C
language based
on the
description of
the algorithm
mitu arvu sisestan;
loen mitu arvu - N;
paaris ja paaritu=0;
for (i = 1; i <= N; i = i + 1)
{
loen arvu a(i);
if (kas a(i) on paarisarv(jagub 2-ga))
{
paaris=paaris+1;
}
else
paaritu=paaritu+1;
}
väljasta paaris, paaritu;
}
SFC runs!
ALGORITHM CREATING 4
Algorithm graphical presentation tools 2
(Jackson editor)
Jackson algorithm Elements Editor
ACTIVITIES
SELECTION (branched)
O in right corner
REPEAT (iteration)* in right corner
An algorithm moves from top down and from left to
right
Structural algorithm
The algorithm can be created as a complete solution,
or consisting of sub-tasks. In the latter case, the task
needs to be divided.
Each task can be divided into at least three parts:
DATA
DATA
RESULTS
INPUT
PROCESSING
OUTPUT
Structural approach is well suited to Jackson editor
ALGORITHM CREATING 5
Algorithm graphical presentation tools 4
(Using the UML activity diagram)
Algorithm using the UML (ArgoUML) :
Task: find the sum of
positive numbers .
ALGORITHM CREATING 6
Finding extremes, and "bubblesort" method
Extremes and sorting
One frequent task when processing data is sorting and
finding the maximum and minimum values. Such
tasks always include two activities:
Comparing the
two values
“Exchanging places” of
two values
The first includes one activity, but the second three activities.
A simple sorting algorithm, can also be used
successfully to find extremes


< MAXIMUM
Analogously can be found
MINIMUM
ALGORITHM CREATING 7
Shell sort algorithm
Shell sort algorithm (1)
(has less exchanges, presumes that data is partially sorted)



Maatrix1( a’la sheet)
A11 A12 A13 A14
A21 A22 A23 A24
t 33 A34
A31 A32 A
A41 A42 A43 A44.
{AIJ }-every member has two index, the
first for the row, second for the column.
Through the indexes will know the
location of each element in the matrix.
Matrix2( a’la table where numbered cells)
A11 A12 A13 A14
A21 A22 A23 A24
A31 A32 A33 A34
A41 A42 A43 A44.
{AIJ }-every element has two indices,
which are determined its location.
Indexes can become the available
values of 1 or 0 (depending on the
language in which they are used), the
columns / rows to the number 0 or less
of one.
The matrix elements of the distinction between, certain
generalizations on the basis of the most common programming
tasks in the logic of usable algorithms for learning
Output a matrix row based on maximum
element
Output matrix row, on the
basis of max element
Matrix input A(i,j)
i,j=1..N
Max A(i,J)
finding,
max(i)
Row separation
on the base of
max(i)
We separate the
task into sub-tasks
and solve them
later!
Kokkuvõtteks:
1.Algoritmi
võimaldab
What iskoostamine
an algorithm?
eraldada ülesande sisulise lahendamise
programmi
koodi
Why use
it? kirjutamisest ja seega
muudab lahenduse sõltumatuks keelest.
How to use it?
2.Võimaldab eritasanditel rühmatööd.
3.Muudab lahenduse jälgitavust, lihtsustab
algoritmi kontrollimist ja tõstab töökindlust.
In conclusion:
Kokkuvõtteks:
1. Algorithm creating enables separating the
1.Algoritmi
koostamine
võimaldab
contents
from
the program
code, thereby
eraldada
lahendamise
making
theülesande
solutionsisulise
independent
from writing
programmi
koodi kirjutamisest ja seega
and
language.
muudab lahenduse sõltumatuks keelest.
2. Allows for different levels of group work.
2.Võimaldab eritasanditel rühmatööd.
3. Changes traceability of the solution,
3.Muudabverification
lahenduse and
jälgitavust,
simplifies
increaseslihtsustab
the
algoritmi kontrollimist ja tõstab töökindlust.
reliability.
HOMEPAGE:
http://www.tud.ttu.ee/im/Vladimir.Viies/
Download