Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #define RADIUS (7.6)
- #define HEIGHT (8.2)
- //shape numbers
- #define CIRCLE (0)
- #define SPHERE (1)
- #define CYLINDER (2)
- #define CONE (3)
- #define NOSHAPES (4)
- #define MAXGEN (5)
- #define FORMAT ("%d %‐8s_%‐9s=%7.1lf\n")
- #define ERRMSG ("%d is not a recognized shape number\n")
- //prototypes
- int main(void);
- double areaCircle(double);
- double periCircle(double);
- double volSphere(double);
- double surfaceSphere(double);
- double volCylinder(double, double);
- double surfaceCylinder(double, double);
- double volCone(double, double);
- double surfaceCone(double, double);
- // Computes area of a circle with radius r
- double areaCircle(double r) {
- return M_PI*r*r;
- }//areaCircle
- // Computes perimeter of a circle with radius r
- double periCircle(double r) {
- return 2*r*M_PI;
- }//periCircle
- // Computes volume of a sphere with radius r
- double volSphere(double r) {
- return 4.0/3.0*M_PI*r*r*r;
- }//volSphere
- // Computes surface area of a sphere with radius r
- double surfaceSphere(double r) {
- return 4*M_PI*r*r;
- }//surfaceSphere
- // Computes volume of a cylinder with base radius r and height h
- double volCylinder(double r, double h) {
- return M_PI*r*r*h;
- }//volCylinder
- // Computes surface area of a cylinder with base radius r and height h
- double surfaceCylinder(double r, double h) {
- return 2*M_PI*r*h+2*M_PI*r*r;
- }//surfaceCylinder
- // Computes volume of a cone with base radius r and height h
- double volCone(double r, double h) {
- return M_PI*r*r*h/3.0;
- }//volCone
- // Computes volume of a cone with base radius r and height h
- double surfaceCone(double r, double h) {
- double step = (h*h)+(r*r);
- step = sqrt(step);
- step = r + step;
- return M_PI*r*step;
- }//surfaceCone
- void shapeCalculator(int shape) {
- if (shape == CIRCLE) {
- double aCircle = areaCircle(RADIUS);
- double pCircle = periCircle(RADIUS);
- printf(FORMAT, shape, "Circle", "Area", aCircle);
- printf(FORMAT, shape, "Circle", "Perimeter", pCircle);
- }
- else if (shape == SPHERE) {
- double vSphere = volSphere(RADIUS);
- double sSphere = surfaceSphere(RADIUS);
- printf(FORMAT, shape, "Sphere", "Volume", vSphere);
- printf(FORMAT, shape, "Sphere", "Surface", sSphere);
- }
- else if (shape == CYLINDER) {
- double vCylinder = volCylinder(RADIUS, HEIGHT);
- double sCylinder = surfaceCylinder(RADIUS, HEIGHT);
- printf(FORMAT, shape, "Cylinder", "Volume", vCylinder);
- printf(FORMAT, shape, "Cylinder", "Surface", sCylinder);
- }
- else if (shape == CONE) {
- double vCone = volCone(RADIUS, HEIGHT);
- double sCone = surfaceCone(RADIUS, HEIGHT);
- printf(FORMAT, shape, "Cylinder", "Volume", vCone);
- printf(FORMAT, shape, "Cylinder", "Surface", sCone);
- }
- else {
- printf(ERRMSG, shape);
- } //cascaded if
- } //shapeCalculator
- int main(void) {
- int shape = 1;
- for (int k= 0; k< MAXGEN; k++) {
- shape = rand() % NOSHAPES;
- shapeCalculator(shape);
- }//for
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment