Transcript Document

HKOI 2004 Training Team
Data Processing 2
(Intermediate Level)
Overview




Function and Procedure
Simple sorting and searching algorithms
Mapping
Some more annoying DaP questions
Why use procedures and
functions?

To separate important and repetitive
jobs as modules.




Make a program more readable and easier
to maintain.
Important for complicated DaP problems.
So that you won’t feel dizzy!
What a shame that CE doesn’t really
include functions!

Maybe most candidates can’t understand?
Procedure (Pascal)
program P1;
var i,j,k : integer;
procedure ABC( a : integer; var b : integer );
begin
a := 2*a;
{ r/w local variable }
k := k-1;
{ r/w global variable }
b := 2*b;
{ r/w variable parameter }
end;
begin
i := 1; j := 2; k := 3;
ABC( i, j );
ABC( k, k );
{ What is the output?
end.
What is k? }
Procedure (Pascal)

Be smart in using variable parameters!



Do NOT work by returning a value to the
variable used in the caller.
Work by sharing the same memory location.
More than 1 names can be assigned to a
single memory location.
Function (Pascal)
program P2;
var i,j,k : integer;
function DEF( a : integer; var b : integer ) : integer;
begin
a := 2*a; k := k-1; b := 2*b; { same as procedure }
DEF := b*b;
{ return something }
end;
begin
i :=
i :=
k :=
j :=
end.
1; j
DEF(
DEF(
DEF(
:=
i,
k,
j,
2; k := 3;
j );
k );
{ What is k? }
j ) + DEF( j, j ); { What is j? }
{ Bad style! }
Function (Pascal)

A function is a procedure that returns
something.



Only simple types can be returned.
Use pointer or variable parameter to
“return” record, array, etc.
Only one “assignment” for return
can/must be done in each call.

Though more than one assignment can
appear in the code. E.g. if-then-else.
Function (C)
#include <stdio.h>
int i,j,k;
void ABC( int a, int *b ) {
a = 2*i; k = k-1; *b = 2**b;
}
// “procedure”
// pointers again ...
// may use return
int DEF( int a, int *b ) {
a = 2*i; k = k-1; *b = 2**b;
return *b**b;
}
// returns something
int main() {
i = 1; j = 2; k = 3;
ABC( i, &j ); ABC( k, &k );
// give address of var
i = 1; j = 2; k = 3;
i = DEF( i, &j );
k = DEF( k, &k );
j = DEF( j, &j ) + DEF( j, &j ); // Bad style again!
return 0;
}
Function (C)

All procedures are functions in C.


Use void as type.
Function returns a value.


May return wherever you like! Convenient!
Void functions can return without value.


Just call “return;”. Nothing special.
Can’t pass large structure either.


But you may use pointers! Common in C.
Also simulate “variable parameters”.
Function (C++)
#include <cstdio>
int i,j,k;
void ABC( int a, int &b ) {
a = 2*i; k = k-1; b = 2*b;
}
// call-by-reference!
// may you may still
// use pointer
int DEF( int a, int &b ) {
a = 2*i; k = k-1; b = 2*b;
return b*b;
}
// returns something
int main() {
i = 1; j = 2;
ABC( i, j );
i = 1; j = 2;
i = DEF( i, j
k = DEF( k, k
j = DEF( j, j
return 0;
}
k = 3;
ABC( k, k );
k = 3;
);
);
) + DEF( j, j ); // I really hate this!
Function (C++)


Same as C, but with additional features.
Call-by-reference



Same as “variable parameters”. Share
same memory address.
Yet C-style pointers may still be used.
Beware that some large structures are callby-reference by default.


array, vector, ...
string is call-by-value by default. So very slow.
May use call-by-reference to speed up!
Better Style to Write Functions

Avoid VP/CbR.


Avoid using VP/CbR in functions.


Even worse style, yet common in C libraries.
Avoid changing global variables


They make tracing a program more difficult.
In other words, avoid “side effect”.
Anyway you have to know the actual
flow of your program.
Mapping

Data that are well presented to human
may not be good for a computer.


Data must be stored in memory that
each memory cell has an address


String, for example.
Array index can be treated as an address.
We pre-process those data and give
each of them an index (or some other
indications), so that the required job
can be done easier.
Mapping

Mapping is related to many data
structures and algorithms.





Sorting, searching, queue, pointer, ...
Yet important to teach you now!
Mapping can be used to save memory
and execution time as well.
We will use the easiest approach to
learn mapping!
Let’s see 1012 as an example!
Mapping (Step 1)


Certainly, you read in something.
But then, you have to give an index to
each school.

We map an integer to another integer.




You may map a string to an integer though.
var school : array[1..10] of integer;
int school[10];
Similar mapping is done to all students.


var student : array[1..1000] of integer;
int student[1000];
Mapping (Step 1)

Here we use a data structure called
queue.

First in, first out.


For each new school/student,



Although you won’t remove something in 1012.
Add it into the back of the queue/array.
The index of that cell is to represent that
school/student.
That index is used in the following data
manipulation process.
Mapping (Step 2)

Memorize all students’ choices.






var choices : array[1..1000,1..10] of integer;
int choices[1000][10];
Data stored maybe the original school code
or the mapped school index, but index is
preferred for efficiency.
And you need to memorize the students’
scores as well. I bet you know how to do it.
Here, you need to find out the index.
You need to search for the index!
Mapping (Step 2)

Sequential search is the simplest
algorithm.
5


8
7
4
6
2
1
3
Yet sequential search is very slow.
If the list is sorted, you can do a binary
search which is MUCH MUCH faster.


But I am not going to teach you here.
More will be taught in the Sorting and
Searching training session.
Mapping (Step 3)

You need to sort the students according
to their scores before doing the school
places allocation.


And you need to sort school codes sooner
or later. It is actually better to do it before
reading the students’ choices and scores.
Here it comes the sorting part!
Mapping (Step 3)

I think selection
sort is the most 5 8 7 4 6 2 1 3
straight forward 1 8 7 4 6 2 5 3
algorithm.


1
Yet very slow as
1
well.
1
More will be
taught in the
1
Sorting and
1
Searching
training session. 1
2
2
2
2
2
2
7
3
3
3
3
3
4
4
4
4
4
4
6
6
6
5
5
5
8
8
8
8
6
6
5
5
5
6
8
7
3
7
7
7
7
8
Mapping (Step 4)

Beware of changing the indices of
school/students in sorting!


Sometimes it is ok, sometimes it is not.
We may build another group of indices,
and map them to those school/student
indices!




Called secondary indices.
Work like pointers.
Useful for sorting and searching.
Do you feel dizzy? @_@
Mapping (Step 4)
1
1
6
1
1387
23
...
45.8
2
2
2
0026
46
...
84.25
3
3
3
0503
46
...
60
4
4
9
4
3146
87
...
35.4
5
5
7
5
7162
87
...
42.87
6
6
1
6
0005
23
...
74.2
7
10
7
7
1300
46
...
73.92
8
8
8
3140
46
...
25.9
9
9
4
9
1005
23
...
70
10
10
5
10
2418
87
...
48
sorted_
students


students
choices
scores
You sort one array instead of three!
One more level of indirect access needed.
Mapping

And then you can do the actual school
place allocation finally.
Exercises (AM and PM)






1012 – Allocating School Places
1004 – Decryption
1007 – Packet Re-assembly
20344 – Roman Digititis
1013 – Internet Usage Bills
10001 – Data, Data, Everywhere