1 / 35

Programming Fundamentals 6 th lecture Szabolcs Papp

Programming Fundamentals 6 th lecture Szabolcs Papp. Record/structure – data abstraction Compound types – outlook Functions – algorithmic abstraction. Content. Record / Structure. Task (example) : Let's determine which quarter-plain contains a given point P! Towards the solution :

ttrail
Download Presentation

Programming Fundamentals 6 th lecture Szabolcs Papp

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. Programming Fundamentals6th lectureSzabolcs Papp

  2. Record/structure – data abstraction Compound types –outlook Functions – algorithmic abstraction Content

  3. Record/Structure Task (example): Let's determine which quarter-plain contains a given point P! Towards the solution: Points on plane are defined by their x and y coordinates. To store them, we need a new, compound data type:P:Record(x,y:Real) Similarly to arrays, records are compound types. In arrays the elements have index, in records the elements have names: (P.x, P.y).

  4. Record/Structure Specification: Input: P:TPoint, TPoint=Record(x,y:Real) Output: QP:Integer Precondition: – Post cond.: P.x0 andP.y0  QP=1 and P.x<0 andP.y0  QP =2 and P.x<0 andP.y<0  QP =3 and P.x0 andP.y<0  QP =4 Type definition C++ typedefinition: structTPont{doublex,y;} type identifierfield typefield identifier(s)

  5. Record/Structure Specification: Input: P:TPoint, TPoint=Record(x,y:Real) Output: QP:Integer Precondition: – Post cond.: P.x0 andP.y0  QP =1 and P.x<0 andP.y0  QP =2 and P.x<0 andP.y<0  QP =3 and P.x0 andP.y<0  QP =4 C++ typedeclaration: TPontP; type identifierdata identifier data_identifier.field_identifier

  6. Record/Structure Y N Y N data_identifier.field_identifier Algorithm: Y N

  7. Record/Structure Y N I N data_identifier.field_identifier data_identifier.field_identifier Algorithm: Y N C++ reference: if (P.x>=0){if (P.y>=0) QP=1;elseQP=4;}else{if(P.y>=0) QP=2;elseQP=3;}

  8. Compound data types

  9. Functions(direction) Task (example): Let's determine if –viewing from the Origin– point Q can be seen to the left or to the right relative to point P which is located in the first quarter-plane! Dir(P,Q) =

  10. Functions(direction) Interpretation: A point's "direction" can be defined by its azimuthal angle (the angle between its vector and axis x). <  tan()<tan() tan()=P.y/P.x

  11. Functions(direction) <  tan()<tan()  P.y/P.x<Q.y/Q.x  P.y*Q.x<Q.y*P.x  P.y*Q.x–Q.y*P.x<0 Predicate: Dir(P,Q)=sgn(P.y*Q.x–Q.y*P.x)(and that is true in all quarter-planes!). We can check that it's true: sgn(P.y*Q.x–Q.y*P.x) =

  12. Functions(direction) Actual parameters Formal parameters Actual parameters Specification: Input: P,Q:TPoint,TPoint=Record(x,y:Real) Output: Ir:Real Precondition: – Post condition: Direction=Dir(P,Q) Definition: Dir:TPointTPointInteger Dir(p,q):=sgn(p.y*q.x–q.y*p.x) Algorithm:

  13. Functions(direction) Formal parameters Definition of functionfor determining the rotation direction.

  14. Functions(direction) intdirection=Dir(P,Q); intDir(TPoint p, TPoint q){intD,S;//helper variables S=p.y*q.x-q.y*p.x;//type conversion if (S<0) D=-1;elseif (S==0) D=0;elseif (S>0) D=1;returnD;} C++ coding:

  15. Functions(direction) TPointPointInput(string _pointName) { TPointpoint;//helper point point.x=InputCoord("Please define "+_pointName+ " x-coordinate!"); point.y=InputCoord("Please define "+_pointName+ " y-coordinate!"); returnpoint; } Output parameter Input parameter voidPointIn(string_pointName, TPoint&_point) { _point.x=InputCoord("Please define "+_pointName+ " x-coordinate!"); _point.y=InputCoord("Please define "+_pointName+ " y-coordinate!"); return; } Note:We can have function without return value, this is called "prodecure". Typical usage for procedure is data input:

  16. Functions funcTypefuncId(parType formParId,…); or voidfuncId(parType formParId,…); If there are no formal parameters, parenthesis is still needed! funcTypefuncId(parType formParId,…) //The head line is without semicolon! { … //function body return functionValue; } in case of void function we have: return; C++ notes – summary: Function head – definition: Function definition:

  17. Functions C++ notes – summary: • Formalscalarparameter: • inputnospecial sign • output&sign as a prefix • Actualscalarparameter: if the corresponding formal parameter is • inputeitherconstantoridentifier • outputonlyidentifier

  18. Functions C++ notes – summary: Scalarparameterization: By value− a local identifier is created from the formal parameter, and the value of the actual parameter is copied into it. Thus changing the parameter within the function body won't effect the actual parameter. E.g.: By reference− the memory reference of the actual parameter is copied onto the formal parameter, so the same memory slot will be accessible through the formal parameter, too. Input parameters intmax(int x, int y) Input parameters. voidmax(int x, int y, int &max_xy) By reference (In/Output)

  19. Functions C++ notes – summary: • Formalarrayparameter: • inputconst prefix • outputnospecial mark • Actualarrayparameter: if the corresponding formal parameter is • inputeitherconstantoridentifer • outputonlyidentifier

  20. More details regarding C++ code C++ notes – summary: Arrayparameter: Principle: arrays are always passed by reference as a parameter! Input −example for head: Input/Output −example for head: Input parameters. voidout_int_array(const int x[], int n, int maxN) or voidout_int_array(const int x[maxN], int n, int maxN) In/Output paramaters voidin_int_array(constint x[], int&n, int maxN) or voidin_int_array(constint x[maxN],int&n, int maxN)

  21. More details regarding C++ code C++ notes – summary: Main structure of a simple program1 …function head definitions… intmain()//the main program { //input: …declaration of input data… //output: …declaration of output data… //top level of activities in this program: input_function(input_data); main_logic_function(input_data, output_data); output_function(output_data); return 0; } …function-definitions… Program parameters based on the input and output of the specification Actualparameters Main logic based on the post condition of the specification

  22. More details regarding C++ code C++ notes – summary: Main structure of a simple program 2 //input: …declaration of input data… //output: …declaration of output data… … function head definitions … intmain()//the main program { // top level of activities in this program: input_function(); main_logic_function(); output_function(); return 0; } …function definitions… Program parameters based on the input and output of the specification GLOBAL data of program 2020.01.04. 22/51

  23. Functions(turn) Idea for solution: Let's shift s andt so that point A gets into the Origin! With this transformation we got the same task as task "direction" was: Turn(A,B,C)=Dir(B–A,C–A) Task (example): How much does segment t (BC) turns related to segments (AB)?

  24. Functions(turn) Specification: Input: A,B,C:TPoint, TPoint=… Output: TurnDegr:Integer Precondition: – Post condition: TurnDegr=Turn(A,B,C) Definition: Turn(a,b,c):=…–1, +1 or 0…

  25. Functions(turn) intTurn(TPoint a, TPoint b, TPoint c){TPoint p,q; p.x=b.x – a.x; p.y=b.y – a.y; q.x=c.x – a.x; q.y=c.y – a.y; returnDir(p,q);} In the solution we call function Dir.

  26. Function(contains) Task (example): Let's decide if point C is contained by segment (A,B)! Specification: Input: A,B,C:TPoint Output: Contains:Logikai Precondition: – Post condition: Contains=Cont(A,B,C) Definition: Cont(a,b,c,):=…

  27. Functions(contains?) Definition: Cont(a,b,c):= Turn(a,b,c)=0 and Between(a.x,c.x,b.x) and Between(a.y,c.y,b.y) So we need to define an additional function that decides if the second parameter is between the two other parameters! Between(r,s,t):= r ≤ s ≤ t or t ≤ s ≤ r

  28. Functions(contains?)

  29. Functions(intersects?) Task (example): Let's decide if segment (A,B) intersects with segment (C,D)! Possible cases:

  30. Function(intersects?) Specification: Input: A,B,C,D:TPoint Output: Intersects:Boolean Precondition: – Post condition: Intersects= ( Turn(A,B,C)*Turn(A,B,D)<0 andTurn(C,D,A)*Turn(C,D,B)<0 orCont(A,B,C) orCont(A,B,D) orCont(C,D,A) orCont(C,D,B) )

  31. Functions(intersects?) Algorithm:

  32. Functions(within triangle?) Task (example): Let's decide if point D is contained by triangle (A,B,C)! Idea for solution: Point D is contained if moving around the triangle(ABCA) point D is always on the left hand side or always on the right hand side.

  33. Function(within triangle?) Specification: Input: A,B,C,D:TPoint Output: InTriangle:Boolean Precondition: – Post cond.: InTriangle=(Turn(A,B,D)==Turn(B,C,D)=Turn(C,A,D)) Algorithm:

  34. Functions Call structure of functions defined in the previous examples:

  35. Programming FundamentalsEnd of 6th lecture

More Related