190 likes | 299 Views
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
E N D
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.) • Avoid long and complex expressions x:=(2*(y2-y1)*(x1*x1+y1*y1-x3*x3-y3*y3)-2*(x1*x1+y1*y1-x2*x2-y2*y2)*(y3-y1)) /(4*(x2-x1)*(y3-y1)-4*(y2-y1)*(x3-x1)); y:=((x1*x1+y1*y1-x2*x2-y2*y2)*2*(x3-x1)-2*(x2-x1)*(x1*x1+y1*y1-x3*x3-y3*y3)) /(4*(x2-x1)*(y3-y1)-4*(y2-y1)*(x3-x1)); r:=sqrt((x-x1)*(x-x1)+(y-y1)*(y-y1)); a:=2*(x2-x1); b:=2*(y2-y1); c:=x1*x1+y1*y1-x2*x2-y2*y2; d:=2*(x3-x1); e:=2*(y3-y1); f:=x1*x1+y1*y1-x3*x3-y3*y3; x:=(b*f-c*e)/(a*e-b*d); y:=(c*d-a*f)/(a*e-b*d); r:=sqrt((x-x1)*(x-x1)+(y-y1)*(y-y1));
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: • To make you understand what the correct output format is • 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 p1,p2 such that p1+p2=N • p1 ≤ p2 • Minimize (p2-p1) • Minimum N = 4 • Maximum N = 10,000,000 (wow!) • Can N be odd?
Thinking… • Find two primes… • if M mod pi = 0, where pi 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?