fork download
  1. // Description: Macros to calculate the perimeter (distance around)
  2. // of six two-dimensional shapes.
  3. //
  4. // Shapes covered:
  5. // a. Triangle
  6. // b. Square
  7. // c. Rectangle
  8. // d. Quadrilateral
  9. // e. Circle
  10. // f. Sector
  11. //
  12. //**************************************************************
  13.  
  14. #include <stdio.h>
  15.  
  16. // PI constant used for circle and sector calculations
  17. #define PI 3.14159265358979323846
  18.  
  19. //**************************************************************
  20. // Perimeter Macros
  21. //
  22. // a. Triangle
  23. // Perimeter = sum of all three sides
  24. // Parameters: a, b, c (lengths of the three sides)
  25. //
  26. // b. Square
  27. // Perimeter = 4 times the length of one side
  28. // Parameters: s (side length)
  29. //
  30. // c. Rectangle
  31. // Perimeter = 2 times (length + width)
  32. // Parameters: l (length), w (width)
  33. //
  34. // d. Quadrilateral
  35. // Perimeter = sum of all four sides
  36. // Parameters: a, b, c, d (lengths of the four sides)
  37. //
  38. // e. Circle (circumference)
  39. // Perimeter = 2 * PI * radius
  40. // Parameters: r (radius)
  41. //
  42. // f. Sector
  43. // Perimeter = 2 * radius + arc length
  44. // = 2r + (theta * r)
  45. // = r * (2 + theta)
  46. // Parameters: r (radius), theta (central angle in RADIANS)
  47. //
  48. //**************************************************************
  49.  
  50. #define PERIMETER_TRIANGLE(a, b, c) ((a) + (b) + (c))
  51. #define PERIMETER_SQUARE(s) (4 * (s))
  52. #define PERIMETER_RECTANGLE(l, w) (2 * ((l) + (w)))
  53. #define PERIMETER_QUADRILATERAL(a, b, c, d) ((a) + (b) + (c) + (d))
  54. #define PERIMETER_CIRCLE(r) (2 * PI * (r))
  55. #define PERIMETER_SECTOR(r, theta) ((r) * (2 + (theta)))
  56.  
  57.  
  58. //**************************************************************
  59. // main - tests each macro with sample values
  60. //**************************************************************
  61.  
  62. int main ()
  63. {
  64. // --- a. Triangle ---
  65. // sides: 3, 4, 5 => expected: 12
  66. printf("a. Triangle Perimeter : %.2f\n",
  67. (double) PERIMETER_TRIANGLE(3, 4, 5));
  68.  
  69. // --- b. Square ---
  70. // side: 5 => expected: 20
  71. printf("b. Square Perimeter : %.2f\n",
  72. (double) PERIMETER_SQUARE(5));
  73.  
  74. // --- c. Rectangle ---
  75. // length: 8, width: 3 => expected: 22
  76. printf("c. Rectangle Perimeter : %.2f\n",
  77. (double) PERIMETER_RECTANGLE(8, 3));
  78.  
  79. // --- d. Quadrilateral ---
  80. // sides: 4, 6, 5, 7 => expected: 22
  81. printf("d. Quadrilateral Perimeter : %.2f\n",
  82. (double) PERIMETER_QUADRILATERAL(4, 6, 5, 7));
  83.  
  84. // --- e. Circle ---
  85. // radius: 7 => expected: 2 * PI * 7 = 43.98
  86. printf("e. Circle Perimeter : %.2f\n",
  87. PERIMETER_CIRCLE(7.0));
  88.  
  89. // --- f. Sector ---
  90. // radius: 5, theta: PI/3 (60 degrees)
  91. // expected: 5 * (2 + PI/3) = 5 * 3.0472 = 15.24
  92. printf("f. Sector Perimeter : %.2f\n",
  93. PERIMETER_SECTOR(5.0, PI / 3));
  94.  
  95. return 0;
  96.  
  97. } // main
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
a. Triangle Perimeter       : 12.00
b. Square Perimeter         : 20.00
c. Rectangle Perimeter      : 22.00
d. Quadrilateral Perimeter  : 22.00
e. Circle Perimeter         : 43.98
f. Sector Perimeter         : 15.24