Presentation on theme: "Advanced Programming 15 Feb 2003. The “OI” Programming Process Reading the problem statement Thinking Coding + Compiling Testing + Debugging Finalizing."— Presentation transcript:
Advanced Programming 15 Feb 2003
The “OI” Programming Process Reading the problem statement Thinking Coding + Compiling Testing + Debugging Finalizing the program
Reading… Read the problem statements carefully Note the constraints Meaningful Range of variables Execution time limit Do not “guess” the meaning of the problem statements.
Thinking What is the ‘type’ of the problem? DP? Graph? Maths? Others? Have you solved a similar problem before? How do you solve the problem without computer? Can the problem be further divided into some easier sub-problems?
Thinking (cont.) Drawing diagrams may be useful (especially for DP and Graph problems) Is the problem too simple? If yes, look for tricks ;-) Is the algorithm correct? Any special cases? Have you coded it before? What were the difficulties you faced?
Thinking (cont.) Is the algorithm feasible? How to code it in PASCAL? Is it easy to code? What is the approximate execution time? How to store the data? Integer? LongInt? String? Array of char? Array? Linked-List? Are there enough memory to store the data?
Thinking (cont.) Are there any (simple) ways to improve Program execution time? Ease of coding?
Coding Pre-processing (e.g. find primes) Input Process Part 1, 2, …, N Output Save and compile your code frequently
Coding (cont.) Copy-and-paste Make sure the “source” is correct Update all variables Double checking required
Testing When to test your program? After completing the whole program? After writing each statement? After coding each part? After making changes to a part?
Testing (cont.) Print all important variables to screen Print messages Use debugger, inspect program behavior by running step-by-step
Testing (cont.) Sample Output “A problem has Sample Output for two reasons: 1. To make you understand what the correct output format is 2. To make you believe that your incorrect solution has solved the problem correctly ”
Testing (cont.) Hand-made Input Boundary Case “Large” Input Execution Time Overflow
Debugging Some short-cut keys F4 – Goto Cursor F7 – Trace into F8 – Step Over Ctrl-F7 – Add Watch Ctrl-F2 – Program Reset Ctrl-F8 – Add BreakPoint Alt-F5 – Screen Output
Finalizing Check I/O filename Check Output Format Trailing spaces? Correct source/executable name? Is the executable updated?
Demostration HKOI2003 Junior Q3 – Goldbach’s Conjecture Read the problem statements, and think about the algorithms now ;-)
Reading… Given an integer N, find two primes p 1,p 2 such that p 1 +p 2 =N p 1 ≤ p 2 Minimize (p 2 -p 1 ) Minimum N = 4 Maximum N = 10,000,000 (wow!) Can N be odd?
Thinking… Find two primes… if M mod p i = 0, where p i is a prime, then M is not prime. So….the first step is to find some small prime numbers (up to ) Then, write a function that uses these prime numbers to check for larger primes Use a for-loop to find two prime numbers such that their sum is N Feasible? Easy to write?