Guest User

C Program Help 2

a guest
Sep 30th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.14 KB | None | 0 0
  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <math.h>
  11. #define RADIUS (7.6)
  12. #define HEIGHT (8.2)
  13. //shape numbers
  14. #define CIRCLE (0)
  15. #define SPHERE (1)
  16. #define CYLINDER (2)
  17. #define CONE (3)
  18. #define NOSHAPES (4)
  19. #define MAXGEN (5)
  20. #define FORMAT ("%d %‐8s_%‐9s=%7.1lf\n")
  21. #define ERRMSG ("%d is not a recognized shape number\n")
  22. //prototypes
  23. int main(void);
  24. double areaCircle(double);
  25. double periCircle(double);
  26. double volSphere(double);
  27. double surfaceSphere(double);
  28. double volCylinder(double, double);
  29. double surfaceCylinder(double, double);
  30. double volCone(double, double);
  31. double surfaceCone(double, double);
  32.  
  33. // Computes area of a circle with radius r
  34. double areaCircle(double r) {
  35.     return M_PI*r*r;
  36. }//areaCircle
  37.  
  38. // Computes perimeter of a circle with radius r
  39. double periCircle(double r) {
  40.     return 2*r*M_PI;
  41. }//periCircle
  42.  
  43. // Computes volume of a sphere with radius r
  44. double volSphere(double r) {
  45.     return 4.0/3.0*M_PI*r*r*r;
  46. }//volSphere
  47.  
  48. // Computes surface area of a sphere with radius r
  49. double surfaceSphere(double r) {
  50.     return 4*M_PI*r*r;
  51. }//surfaceSphere
  52.  
  53. // Computes volume of a cylinder with base radius r and height h
  54. double volCylinder(double r, double h) {
  55.     return M_PI*r*r*h;
  56. }//volCylinder
  57.  
  58. // Computes surface area of a cylinder with base radius r and height h
  59. double surfaceCylinder(double r, double h) {
  60.     return 2*M_PI*r*h+2*M_PI*r*r;
  61. }//surfaceCylinder
  62.  
  63. // Computes volume of a cone with base radius r and height h
  64. double volCone(double r, double h) {
  65.     return M_PI*r*r*h/3.0;
  66. }//volCone
  67.  
  68. // Computes volume of a cone with base radius r and height h
  69. double surfaceCone(double r, double h) {
  70.     double step = (h*h)+(r*r);
  71.     step = sqrt(step);
  72.     step = r + step;
  73.     return M_PI*r*step;
  74. }//surfaceCone
  75.  
  76. void shapeCalculator(int shape) {
  77.     if (shape == CIRCLE) {
  78.         double aCircle = areaCircle(RADIUS);
  79.         double pCircle = periCircle(RADIUS);
  80.         printf(FORMAT, shape, "Circle", "Area", aCircle);
  81.         printf(FORMAT, shape, "Circle", "Perimeter", pCircle);
  82.     }
  83.     else if (shape == SPHERE) {
  84.         double vSphere = volSphere(RADIUS);
  85.         double sSphere = surfaceSphere(RADIUS);
  86.         printf(FORMAT, shape, "Sphere", "Volume", vSphere);
  87.         printf(FORMAT, shape, "Sphere", "Surface", sSphere);
  88.     }
  89.     else if (shape == CYLINDER) {
  90.         double vCylinder = volCylinder(RADIUS, HEIGHT);
  91.         double sCylinder = surfaceCylinder(RADIUS, HEIGHT);
  92.         printf(FORMAT, shape, "Cylinder", "Volume", vCylinder);
  93.         printf(FORMAT, shape, "Cylinder", "Surface", sCylinder);
  94.     }
  95.     else if (shape == CONE) {
  96.         double vCone = volCone(RADIUS, HEIGHT);
  97.         double sCone = surfaceCone(RADIUS, HEIGHT);
  98.         printf(FORMAT, shape, "Cylinder", "Volume", vCone);
  99.         printf(FORMAT, shape, "Cylinder", "Surface", sCone);
  100.     }
  101.     else {
  102.         printf(ERRMSG, shape);
  103.     } //cascaded if
  104. } //shapeCalculator
  105.  
  106. int main(void) {
  107.     int shape = 1;
  108.     for (int k= 0; k< MAXGEN; k++) {
  109.         shape = rand() % NOSHAPES;
  110.         shapeCalculator(shape);
  111.     }//for
  112.     return EXIT_SUCCESS;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment