In C, Create a library (i.e. a .h header and .c source file) that calculates vol
ID: 3875502 • Letter: I
Question
In C, Create a library (i.e. a .h header and .c source file) that calculates volume of three geometric shapes: the sphere, the right (untilted) cylinder, and the ellipsoid (look it up!). The user supplies the appropriate parameter such as radius for the sphere in decimal form when calling the function. Your function returns the volume. All the formulas use ; use a #define statement in the source code to replace _PI_ with 4.0*ATAN(1.0). Underscores in defined names are used to avoid unwanted substitutions; you wouldn’t want PIE to become 4.0*ATAN(1.0)E for example. Make sure you name the functions and variables to enhance readability. Add comments for clarity.
Explanation / Answer
Header file:VOLUME.H
float volumeSphere(float radius);
float volumeCylinder(float radius,float height);
float volumeEllipsoid(float a,float b, float c);
Source code: VOLUME.C
# define _PI_ 3.14159
float volumeSphere(float radius)
{
return (4 * _PI_ * radius * radius * radius)/3;
}
float volumeCylinder(float radius,float height)
{
return _PI_ * radius * radius * height;
}
float volumeEllipsoid(float a,float b, float c)
{
return (4 * _PI_ * a * b * c)/3;
}
Main code to test:
#include <stdio.h>
#include <stdlib.h>
#include "volume.h"
int main(void){
float radius,height,a,b,c;
printf(" *******Calculate Volumd of sphere******** ");
printf("Enter radius: ");
scanf("%f",&radius);
printf("Volums of Sphere is %f", volumeSphere(radius));
printf(" **************************************** ");
printf(" *******Calculate Volumd of Cyclinder******** ");
printf("Enter radius: ");
scanf("%f",&radius);
printf("Enter height: ");
scanf("%f",&height);
printf("Volums of Sphere is %f", volumeCylinder(radius,height));
printf(" **************************************** ");
printf(" *******Calculate Volumd of Ellipsoid******** ");
printf("Enter side of a: ");
scanf("%f",&a);
printf("Enter side of b: ");
scanf("%f",&b);
printf("Enter side of c: ");
scanf("%f",&c);
printf("Volums of Sphere is %f", volumeEllipsoid(a,b,c));
printf(" **************************************** ");
return 0;
}
Sample Output:
*******Calculate Volumd of sphere********
Enter radius: 7
Volums of Sphere is 1436.753784
****************************************
*******Calculate Volumd of Cyclinder********
Enter radius: 7
Enter height: 6
Volums of Sphere is 923.627441
****************************************
*******Calculate Volumd of Ellipsoid********
Enter side of a: 2
Enter side of b: 3
Enter side of c: 4
Volums of Sphere is 100.530884