home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / tyc / list11_1.c < prev    next >
C/C++ Source or Header  |  1993-10-16  |  1KB  |  51 lines

  1.  /* Demonstrates structures that contain other structures. */
  2.  
  3.  /* Receives input for corner coordinates of a rectangle and
  4.     calculates the area. Assumes that the y coordinate of the
  5.     upper-left corner is greater than the y coordinate of the
  6.     lower-right corner, that the x coordinate of the lower-
  7.     right corner is greater than the x coordinate of the upper-
  8.     left corner, and that all coordinates are positive. */
  9.  
  10.  #include <stdio.h>
  11.  
  12.  int length, width;
  13.  long area;
  14.  
  15.  struct coord{
  16.      int x;
  17.      int y;
  18.  };
  19.  
  20.  struct rectangle{
  21.      struct coord topleft;
  22.      struct coord bottomrt;
  23.  } mybox;
  24.  
  25.  main()
  26.  {
  27.      /* Input the coordinates */
  28.  
  29.      printf("\nEnter the top left x coordinate: ");
  30.      scanf("%d", &mybox.topleft.x);
  31.  
  32.      printf("\nEnter the top left y coordinate: ");
  33.      scanf("%d", &mybox.topleft.y);
  34.  
  35.      printf("\nEnter the bottom right x coordinate: ");
  36.      scanf("%d", &mybox.bottomrt.x);
  37.  
  38.      printf("\nEnter the bottom right y coordinate: ");
  39.      scanf("%d", &mybox.bottomrt.y);
  40.  
  41.      /* Calculate the length and width */
  42.  
  43.      width = mybox.bottomrt.x - mybox.topleft.x;
  44.      length = mybox.bottomrt.y - mybox.topleft.y;
  45.  
  46.      /* Calculate and display the area */
  47.  
  48.      area = width * length;
  49.      printf("The area is %ld units.", area);
  50.  }
  51.