CPE 101

Winter 2008

Program 2

 

Due Date

 

11:59pm Friday, February 1.

 

Errata:

  1. None so far

     

 

Objectives

 

  • To be able to develop functions to an explicit specification
  • To be able to develop functions to a general specification
  • To be able to develop tests for functions to focus their development and increase the chances that they are correct
  • To be able to use the same functions in different ways to solve different problems
  • To appreciate the benefits of good problem decomposition and the code reuse

 


Required File Header

 

All students are required to have the following header comment at the top of their source file.  Note that the stuff to the right of the colon in bold is information for an imaginary example student.  (Your header comment is not expected to have bold text.)


/* Project
2

 *
 * Name: Jane Codewell
 * Instructor: Ha
ungs
 * Section: 19
 *
 */


Additional Resources

 

driver.c

 

Problem Description

 

You will be developing a program that calculates the mass of various 3D-shapes, both solid and hollow.  Specifically the program will support a solid 3D rectilinear (box), a solid sphere, a box with a spherical hollow void, and a sphere with a box-shaped hollow void.  Here is a visual example of a box with a spherical void:

 

 

And a sphere with a rectilinear void:

 

 

 

Your task is to develop functions which will complete a program which will prompt the user for input to define a box, a sphere, and the density of the material these shapes are made of.  The program will then calculate and report the volume and mass of each solid shape.  It will also determine and report if the specified sphere could be fully contained by the box and, if so, report the mass of the hollow box.Next it will determine if the box could be fully contained by the sphere and, if so, report the mass of the hollow sphere.  The required outputs are shown in sample runs provided below.

 

A driver is a separate file containing code which will run and test other functions.  You are provided with the driver file which can be found here.  During development, you may use the driver, change the driver, or write your own driver.  However, the file that you submit and the functions that you write must work correctly with the unmodified driver that you are provided with.

 

You are required to implement the program as a collection of several functions that provide solutions to different parts of the problem.  The required functions are fully specified and must be implemented exactly as specified.  DO NOT change the code in driver.c to accommodate your functions.

 

Sample Runs

 

Note 1: This is a sample run using the driver file provided - many other results are possible with many different inputs - be sure to read the specifications carefully for details.

 

Note 2: All user input is bold in the sample runs for clarity only - your program is not expected to behave this way.

 

 

Sample

 

vogon 12:01pm ~> a.out

Enter the dimensions in inches of a rectilinear box...

   Length: 2.3

   Width : 3.4

   Height: 5.6

 

Enter the dimensions in inches of a sphere...

   Radius: 1.123456

 

Enter the material density in pounds per cubic inch...

   Density: 5.5

 

Volume of solid shapes...

   Box   : 43.792000 cubic inches

   Sphere: 5.939595 cubic inches

 

Mass of solid shapes...

   Box   : 240.856000 pounds

   Sphere: 32.667771 pounds

 

Mass of hollow shapes...

   Box with spherical-void: 208.188229 pounds

   Sphere with box-void   : Box is not fully contained by the sphere

vogon 12:01pm ~>

 

Suggestions

 

  1. Read the program specifications fully before beginning to write code.  Ask your instructor for any necessary clarifications as early as possible.
  2. Develop and test the functions one at a time.  To do this, you can create your own driver file, or temporarily alter the driver file that is provided to you.  However, your functions must work correctly with the original driver file provided to you.

 

 

Specification

 

1.      Your program must compile without error or warning using the gcc compiler and the -Wall -ansi -pedantic -lm compiler flags.  Note that you will be compiling and linking two separate source files: the file containing the functions that you write, and the file containing the driver that we supply.  

 

2. JGrasp is surprisingly difficult to set up to compile and link multiple files.  Instead of compiling and linking with JGrasp, compile and link from the command line: gcc -ansi -Wall -pedantic massCalculator.c driver.c.

 

3.      All prompts are contained in the driver file.

 

4.      Your program outputs must match the specified outputs exactly (indentation, spacing, spelling, capitalization, et cetera).

 

5.      You may assume all dimensional and density inputs will be greater than zero.

 

6.      All numerical inputs must support any real number as supported by the C double type for the gcc compiler on vogon.

 

7.      All internal calculations must maintains as much precision as is supported by the C double type in the gcc compiler on vogon.

 

8.      All numerical outputs should be done using the %f flag - no special formatting required.

 

9.      Your program will be tested with input you have not seen and must work correctly for any valid box or sphere within the limits of the C double type in the gcc compiler on vogon.

 

10.      Implement your solution in a file called massCalculator.c.

 

11.  Define a constant in your source file, as shown below, after any #include preprocessor commands and before any functions for PI and use it in any and all calculations that require PI:

 

#define PI 3.14159265

 

12.  Use the following formula to calculate the volume of a sphere:

 

4PIr3

  3

 

13.  Develop a function called volumeOfBox that has a return type of double and three double parameters representing the length, width, and height of a 3D rectilinear box.The function should return the volume of the specified box.

 

14.  Develop a function called volumeOfSphere - that has a return type of double, and one double parameter representing the radius of the sphere.

 

15.  Develop a function called boxContainsSphere with a return type of int and four parameters of type double representing the length, width, and height of a 3D rectilinear box and the radius of a sphere.  The function should return true (non-zero) if the box is large enough to completely contain the sphere, otherwise false (zero).  You must develop the algorithm for this function yourself - this is a good problem to work out on paper before beginning to write your solution in C.

 

16.  Develop a function called sphereContainsBox - with a return type of int and four parameters of type double representing the length, width, and height of a 3D rectilinear box and the radius of a sphere.  The function should return true (non-zero) if the sphere is large enough to completely contain the box, otherwise false (zero).  You must develop the algorithm for this function yourself - this is a good problem to work out on paper before beginning to write your solution in C.

 

17.  Develop a function called getDouble - with a return type of double and no parameters.

 

18. Develop a function called printVolSolids - with no return type, and 2 parameters of type double representing the volume of the box and the volume of the sphere.  This function will print the volumes of the solid sphere and box shapes.

 

19. Develop a function called printMassSolids - with no return type, and 2 parameters of type double representing the mass of the box and the mass of the sphere.  This function will print the masses of the solid sphere and box shapes.

 

20. Develop a function called printMassVoids with no return type, and 2 parameters of type double representing the masses of the box with sphere void and the sphere with box void.  If the sphere will not fit in the box print "Sphere is not fully contained by the box" instead of the mass.  If the box will not fit in the sphere, print "Box is not fully contained by the sphere" instead of the mass.

 

 

Testing

You are responsible for creating your own test cases.  Note that there are 3 distinct possibilities - the box fits in the sphere, the sphere fits in the box, and neither fits inside the other.  In Program 1, you were provided with both input and output files.  To test Program 2, we recommend that you create your own input files. You can then use these input files and the instructor executable to create the correct or expected output files.   Then, use the same input files with your executable to create your output files.  Your output files should exactly match the expected output files.<-->

 

Handing in Your Source Electronically...

 

  1. If necessary, move the massCalculator.c to your vogon account using your favorite FTP client program.

 

  1. Log on to vogon.  Log on directly if you are in the lab and open up a terminal window.  Log on using your favorite shell program, if not using a lab computer.

 

  1. Change directory (cd-command) to the directory containing the file(s) to hand in.

 

  1. Be sure to compile and test your code on vogon using the required compiler flags one last time just before turning the files in.

 

  1. Use the following handin command being sure to replace xx with your section number.

 

handin mhaungs Program2-xx massCalculator.c