Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Class Exercise in C Programming. Consider the following function headers, with p

ID: 3682116 • Letter: C

Question

Class Exercise in C Programming.

Consider the following function headers, with pre and post-conditions:

// Pre-condition: The value of the parameter passed in must be

//                             positive and in some type of units.

// Post-condition: The function will return the volume of the

//                              sphere with the given radius in units3.

double Sphere_Volume(double radius);

// Pre-condition: The value of both parameters must be

//                             positive. The unit used for volume must

//                             be the same used in the density.

// Post-condition: The function will return the mass of the

//                              object in the units used in the density.

double Mass(double den, double volume);

Use these to write an program to compute the mass of a snowman. Read in from the user the three radii (in centimeters) of the three “snowballs” used to create the snowman. You will also have to use the following constant, which is in gm/cm3.

#define SNOW_DENSITY 0.1

#include <stdio.h>

#define      SNOW_DENSITY     0.1

// This program computes the mass of a snowman, based on

// information about the snowman’s dimensions.

int main() {

     double r1, r2, r3, total_volume;

          

      // Read in size of each snowball.

     printf("Enter the size of each snowball in succession. ");

     scanf("%lf%lf%lf", &r1, &r2, &r3);

     // Compute volume of snowman

     total_volume = Sphere_Volume(r1) + Sphere_Volume(r2) +

                                  Sphere_Volume(r3);

  

     // Print out mass of snowman.

     printf(“Snowman mass = ”);

     printf("%lf ", Mass(SNOW_DENSITY, total_volume));

}

Explanation / Answer

snowman.c

#include <stdio.h>
#include <stdlib.h>

#define PI 3.14159
#define SNOW_DENSITY 0.1

double Sphere_Volume( double radius);
double Mass (double den, double volume);

int main() {
    double r1, r2, r3;
    double total_volume;
    double total_mass;


    printf("Enter the size of each snowball in succession. ");
    scanf("%lf%lf%lf", &r1, &r2, &3);

    total_volume= Sphere_Volume(r1)+ Sphere_Volume(r2) + Sphere_Volume(r3);
    total_mass= Mass(SNOW_DENSITY, total_volume);

    printf("Snowman mass = %lf. ", total_mass);

    system("PAUSE");
    return 0;
}


double Sphere_Volume( double radius){
       return 4*PI/3*radius*radius*radius;
}

double Mass (double den, double volume){
       return SNOW_DENSITY*volume;
}

Output :

Enter the size of each snowball in succession.

1 2 3

Snowman mass = 15.079632