Solutions TOPIC L: Complete Exercises.

Contents :

   .

Exercises :


D1

Exercise

S1

Exercise
 



S2

Exercise
 

S3

MODULE S3;
<* NOOPTIMIZE + *> <* WOFF + *>
    FROM IO IMPORT RdChar, WrChar, WrStr, RdStr, WrLn, RdLn, RdCard, WrCard, RdInt, WrInt,RdReal, WrReal, WrFixReal, RdBool, WrBool, RdKey, KeyPressed;
    FROM RealMath IMPORT sqrt, exp, sin, cos, tan, arcsin, arccos, arctan, power, round;
    FROM Lib IMPORT Delay; (* Wait n milliseconds *)
    FROM Windows IMPORT VK_ESCAPE;
    FROM Wimdows IMPORT SetFont, Text, GetKeyPressed, GetTextSize;
    FROM Wimdows IMPORT Window, KillWindow, Line, Ellipse, Disc, Circle, Rectangle, _clrBLACK, _clrBLUE, _clrGREEN, _clrCYAN, _clrRED, _clrMAGENTA, _clrLIGHTRED;
         (* zie C:/Bin/Xds/Def/Ts/Graph.def  *)
   (* A. Constants *)
    CONST SCREEN_SIZE = 600;
          NBR = 10;
          SIZE = 4;
          EXITMESSAGE = "Press 'ESC' To Finish Program";

   (* B. Types *)
  TYPE PointRc = RECORD
                           x, y: REAL;
      (*4*)
      (*6*)
                       END;

   (* C. Procedures *)
  PROCEDURE Square(x0,y0,size,colour:CARDINAL);
  BEGIN
    Rectangle (x0 - size/2,y0 - size/2,x0 + size/2,y0 + size/2, colour,TRUE);
  END Square;

  PROCEDURE Asterisk(x0,y0,size,colour:CARDINAL);
    VAR a:CARDINAL;
  BEGIN
    a:= VAL(CARDINAL,((VAL(REAL,size)/(2.0))*0.7071) ); (* cos(45)*)

    Line(x0-size/2, y0, x0+size/2,y0, colour);
    Line(x0, y0+size/2, x0, y0-size/2, colour);
    Line(x0-a, y0+a, x0+a, y0-a, colour);
    Line(x0+a, y0+a, x0-a, y0-a, colour);
  END Asterisk;

  PROCEDURE CalculateDistance(p1, p2: PointRc): REAL;
  BEGIN
    RETURN sqrt((p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y));
  END CalculateDistance;

  PROCEDURE AverageDistance(p: PointRc; points: ARRAY OF PointRc): REAL;
    VAR sum : REAL;
      i: CARDINAL;
  BEGIN
    sum := 0.0;
    FOR i := 0 TO HIGH(points) DO
      sum := sum + CalculateDistance(p, points[i]);
    END;
    RETURN sum / VAL(REAL, HIGH(points) + 1);
  END AverageDistance;

  (* 2 make procedure PlotArray *)
    (*6 change procedure PlotArray*)

  (* 3 procedure to calculate centre of gravity *)

   (* D. Variables*)
     VAR k, w, h :CARDINAL;
       (*1 define arr*)
       (*3 define cg *)
BEGIN
    WrLn;
  Window("Topic L Exercise S3",SCREEN_SIZE,SCREEN_SIZE);
  (* fill array *)
    arr[1].x := 7.3;   arr[1].y := 8.4E-1;
    arr[2].x := 14.2;  arr[2].y := 7.34;
    arr[3].x := 1.4;   arr[3].y := 6.23;
    arr[4].x := 1.0E1; arr[4].y := 1.0E1;
    arr[5].x := 2.5;   arr[5].y := 4.5;
    arr[6].x := 5.2;   arr[6].y := 10.4;
    arr[7].x := 3.4;   arr[7].y := 5.432;
    arr[8].x := 14.6;  arr[8].y := 9.8;
    arr[9].x := 12.2;  arr[9].y := 15.8;
    arr[10].x := 4.5;  arr[10].y := 4.5;

  (* plot array *)
    (*2 call plot procedure for arr *)

  (* calculate centre of gravity *)
     (*3 call procedure *)

  (* plot centre of gravity *)
     (*3 draw asterisk on cg *)

  (* distance to cg for every point*)
     (*4 use the given CalculateDistance procedure *)

  (*5 *)

  (*6 call plot array again*)



  (* tell user to press escape to finish the program... *)
  SetFont(1, _clrLIGHTRED);
  GetTextSize(EXITMESSAGE, 1, w, h);
  Text(EXITMESSAGE, SCREEN_SIZE/2 - w/2, SCREEN_SIZE - 10 - h);
  (* ...and program will wait here until user presses escape *)
  REPEAT
    Delay(25);
  UNTIL (GetKeyPressed(k) AND (k = VK_ESCAPE));
  (* finally close the window *)
  KillWindow();

END S3.

Exercise

Solution

  1. cg.x = 6.8,  cg.y = 7.4
  2. distance to cg: arr[1].distance = ,  arr[2].distance = ,  arr[3].distance = , ...
  3. average distance to cg = 6.44
  4. Points further away from cg than average:...

X1

Exercise

H1

Exercise

H2

Exercise

T1

Exercise