Write four functions to compute the following properties of a sphere, given a di
ID: 3591003 • Letter: W
Question
Write four functions to compute the following properties of a sphere, given a diameter, d, which is greater than or equal to 0.0: (a) Radius r = d/2 (b) Surface area = 4 × × r 2 (c) Circumference = d (d) Volume = 4 3 × r 3
Make sure all output numbers align at their (implied) decimal point. Check to be sure your numbers are not less than zero, if so print an error message with the number that is incorrect but continue anyway. Use 3.14 for PI. Must also use FUNCTIONS. Each function must have a prototype and must have a return statement with 1 variable on it, NOT a calculation. NO use of global variables.
This is what i have so far, but this code just loops at the first printf even when the input is greater than 0. Also this is the first program with prototypes I do, so I don't yet understand how they are supposed to be used. I started with the radius function first, but the rest are also there.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.14
#define SENTINEL 0
double radiusF(double diameter);
//double surfaceAreaF(double )
int main(void)
{
double diameter = 0;
double radius = 0;
double surfaceArea = 0;
double circumference = 0;
double volume = 0;
while(1)
{
printf("Diameter of sphere to calculate properties for?: ");
scanf("%i", &diameter);
if(diameter == SENTINEL) break;
if(diameter < 0)
{
printf("invalid entry ");
printf("please try again ");
continue;
}
}
return 0;
}
//----------------------------------------------------------------------------
double radiusF(double diameter)
{
double radius = 0;
radius = (diameter/2);
return radius;
}
------------------------------------------------------------------------------------
/*double surfaceAreaF (double raduis)
{
double radius = 0;
}
surfaceArea = (4.0*PI*(pow(radius,2.0)));
circumference = (PI*diameter);
volume = (((4.0*PI)/3.0)*(pow(radius,3.0)));
printf("Input: Radius: Surface Area: Circumference: Volume: ");
printf("%10i %10i %10i %10i %10i ", diameter, radius, surfaceArea, circumference, volume);
}
return 0;
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.14
#define SENTINEL 0
//function prototypes
double radiusF(double diameter);
double surfaceAreaF(double diameter);
double circumferenceF(double diameter);
double volumeF(double diameter);
int main(void)
{
double diameter = 0;
while(1)
{
printf("Diameter of sphere to calculate properties for?: ");
scanf("%lf", &diameter);
if(diameter == SENTINEL) break;
else if(diameter < 0)
{
printf("invalid entry ");
printf("please try again ");
continue;
}
else
{
printf(" Input: Radius: Surface Area: Circumference: Volume: ");
//call to functions
printf(" %.2lf %.2lf %.2lf %.2lf %.2lf ", diameter, radiusF(diameter), surfaceAreaF(diameter), circumferenceF(diameter), volumeF(diameter));
}
}
return 0;
}
//----------------------------------------------------------------------------
double radiusF(double diameter)
{
double radius = 0;
radius = (diameter/2);
return radius;
}
//------------------------------------------------------------------------------------
double surfaceAreaF (double diameter)
{
double surfaceArea,radius;
radius = radiusF(diameter);
surfaceArea = (4.0*PI*(pow(radius,2)));
return surfaceArea;
}
double circumferenceF(double diameter)
{
double circum = (PI*diameter);
return circum;
}
double volumeF(double diameter)
{
double radius = radiusF(diameter);
double volume = ((4.0*PI)/3.0)*(pow(radius,3));
return volume;
}
output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.