1 / 24

HKOI 2004 Training Team

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.

rich
Télécharger la présentation

HKOI 2004 Training Team

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. HKOI 2004 Training Team Data Processing 2 (Intermediate Level)

  2. Overview • Function and Procedure • Simple sorting and searching algorithms • Mapping • Some more annoying DaP questions

  3. 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?

  4. 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? What is k? } end.

  5. 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.

  6. 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 := 1; j := 2; k := 3; i := DEF( i, j ); k := DEF( k, k );{ What is k? } j := DEF( j, j ) + DEF( j, j );{ What is j? } end. { Bad style! }

  7. 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.

  8. Function (C) #include <stdio.h> int i,j,k; void ABC( int a, int *b ) { // “procedure” a = 2*i; k = k-1; *b = 2**b; // 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; }

  9. 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”.

  10. Function (C++) #include <cstdio> int i,j,k; void ABC( int a, int &b ) { // call-by-reference! a = 2*i; k = k-1; b = 2*b; // 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; k = 3; ABC( i, j ); ABC( k, k ); i = 1; j = 2; k = 3; i = DEF( i, j ); k = DEF( k, k ); j = DEF( j, j ) + DEF( j, j ); // I really hate this! return 0; }

  11. 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 call-by-reference by default. • array, vector, ... • string is call-by-value by default. So very slow. May use call-by-reference to speed up!

  12. Better Style to Write Functions • Avoid VP/CbR. • They make tracing a program more difficult. • Avoid using VP/CbR in functions. • Even worse style, yet common in C libraries. • Avoid changing global variables • In other words, avoid “side effect”. • Anyway you have to know the actual flow of your program.

  13. Mapping • Data that are well presented to human may not be good for a computer. • String, for example. • Data must be stored in memory that each memory cell has an address • 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.

  14. 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!

  15. 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];

  16. Mapping (Step 1) • Here we use a data structure called queue. • First in, first out. • Although you won’t remove something in 1012. • For each new school/student, • 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.

  17. 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!

  18. Mapping (Step 2) • Sequential search is the simplest algorithm. • 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.

  19. 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!

  20. Mapping (Step 3) • I think selection sort is the most straight forward algorithm. • Yet very slow as well. • More will be taught in the Sorting and Searching training session.

  21. 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? @_@

  22. Mapping (Step 4) • You sort one array instead of three! • One more level of indirect access needed.

  23. Mapping • And then you can do the actual school place allocation finally.

  24. 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

More Related