ANSWERS TO EXERCISES ON STRUCTURES


 1.
        A. Everything. Need braces, not parens; semicolons, not comma.
        B. Comma is wrong (need semicolons).
        C. Need semicolon after y.
        D. Nothing wrong. This one is OK.
        E. Need semicolon at end.
                 

 2. 
        A. Semicolon missing after "y".
        B. "struct" missing; some type must follow "typedef".
        C. Useless because there is no name for the typedef.
        D. Nothing wrong.  This one is OK.


 3.  They all do the same thing.


 4.       void POINTshow(Point p) {
             printf("[%f %f]\n", p.x, p.y);
          }

 5.       double POINTdist(Point p, Point q) {
             double dx = p.x - q.x;
             double dy = p.y - q.y;
             return sqrt(dx*dx + dy*dy);
          }


 6.       #define EPSILON 0.000001

          int POINTeq(Point p, Point q) {
             if (POINTdist(p, q) < EPSILON)
                return 1;
             else
                return 0;
          }

   Since a logical expression evaluates to 1 (true) or 0 (false), the
   function can be written more compactly:

          int POINTeq(Point p, Point q) {
             return POINTdist(p, q) < EPSILON;
          }

 7.      typedef struct {
            Point lowerleft;         /* lower right endpoint of rectangle */
            Point upperright;        /* upper right endpoint of rectangle */
         } Rect;

 8.      double RECTarea(Rect r) {
            double width  = r.upperright.x - r.lowerleft.x;
            double height = r.upperright.y - r.lowerleft.y;
            return width * height;
         }

         Observe how to access a data field of a struct that itself is the
         data field of another struct.


 9.      int POINTinRect(Point p, Rect r) {
            return (p.x <= upperright.x) && (p.x >= lowerleft.x) &&
                   (p.y <= upperright.y) && (p.y >= lowerleft.y);
         }

10.      int RECTinRect(Rect r1, Rect r2) {
            return POINTinRect(r1.upperleft, r2) && POINTinRect(r1.lowerright,r2);
         }


11.      #include <stdio.h>
         #include <math.h>
         #define N 10

         typedef struct {
            double x;
            double y;
         } Point;

         POINTdist()...
         POINTshow()...

         int main(void) {
            int i;
            Point points[N];          /* store list of points in array  */
            Point bestp1, bestp2;     /* store furthest pair of points  */
            double bestdist = 0.0;    /* and their distance             */
            double x, y, dist;
             
            for (i = 0; i < N; i++) {
               scanf("%lf %lf", &x, &y);
               a[i].x = x; a[i].y = y;
            }
            
            for (i = 0; i < N; i++)
               for (j = i + 1; j < N; j++) {
                  dist = POINTdist(points[i], points[j]);
                  if (dist > bestdist) {
                     bestdist = dist;
                     bestp1 = a[i]; bestp2 = a[j];
                  }
               }

            printf("furthest distance = %f\n", bestdist);
            POINTshow(bestp1);
            POINTshow(bestp2);
            return 0;
         }