350 likes | 367 Views
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 :
E N D
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: 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).
Record/Structure Specification: Input: P:TPoint, TPoint=Record(x,y:Real) Output: QP:Integer Precondition: – Post cond.: P.x0 andP.y0 QP=1 and P.x<0 andP.y0 QP =2 and P.x<0 andP.y<0 QP =3 and P.x0 andP.y<0 QP =4 Type definition C++ typedefinition: structTPont{doublex,y;} type identifierfield typefield identifier(s)
Record/Structure Specification: Input: P:TPoint, TPoint=Record(x,y:Real) Output: QP:Integer Precondition: – Post cond.: P.x0 andP.y0 QP =1 and P.x<0 andP.y0 QP =2 and P.x<0 andP.y<0 QP =3 and P.x0 andP.y<0 QP =4 C++ typedeclaration: TPontP; type identifierdata identifier data_identifier.field_identifier
Record/Structure Y N Y N data_identifier.field_identifier Algorithm: Y N
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;}
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) =
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
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) =
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:TPointTPointInteger Dir(p,q):=sgn(p.y*q.x–q.y*p.x) Algorithm:
Functions(direction) Formal parameters Definition of functionfor determining the rotation direction.
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:
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:
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:
Functions C++ notes – summary: • Formalscalarparameter: • inputnospecial sign • output&sign as a prefix • Actualscalarparameter: if the corresponding formal parameter is • inputeitherconstantoridentifier • outputonlyidentifier
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)
Functions C++ notes – summary: • Formalarrayparameter: • inputconst prefix • outputnospecial mark • Actualarrayparameter: if the corresponding formal parameter is • inputeitherconstantoridentifer • outputonlyidentifier
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)
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
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
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 (BC) turns related to segments (AB)?
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…
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.
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,):=…
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
Functions(intersects?) Task (example): Let's decide if segment (A,B) intersects with segment (C,D)! Possible cases:
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) )
Functions(intersects?) Algorithm:
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(ABCA) point D is always on the left hand side or always on the right hand side.
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:
Functions Call structure of functions defined in the previous examples: