Density Example using Subprograms
/* Program header for density of a cube goes here */
#include <stdio.h>
/* function prototypes - void functions correspond to the concept of
procedures */
void get_int_low(int *, int); /* void get_int_low(int *val, int low); */
int calc_volume_cube(int); /* int calc_volumne_cube(int side); */
float calc_density(int, int); /* int calc_density(int vol, int mass); */
int main(void)
{
int side, mass, volume;
float density;
printf("\n\n\nEnter the length of a side in centimeters ");
get_int_low(&side, 1);
printf("\nEnter the mass of the cube in grams ");
get_int_low(&mass, 1);
volume = calc_volume_cube(side);
density = calc_density(volume, mass);
printf("\nFor a cube with side = %d and mass = %d, the density is %f\n",
side, mass, density);
printf("\n\n\n");
return (0);
}
/** function header goes here **/
void get_int_low(int *val, int low)
{
scanf("%d", val);
while (*val < low)
{
printf("\nError in input, value must be >= %d - try again. ", low);
scanf("%d", val);
}
/** haven't done this loop yet **
do
{
scanf("%d", val);
if (*val < low)
printf("\nError in input, value must be >= %d - try again. ", low);
}
while (*val < low);
********************/
}
/** function header goes here **/
int calc_volume_cube(int side)
{
return(side * side * side);
}
/** function header goes here **/
float calc_density(int vol, int mass)
{
return ((float)mass/(float)vol);
}