/* program to compute bearings cost, multiple times redone with functions October 10, 1997 CS1711A Class*/ #include #include const double HI_CARBON_PRICE = 1.35; // program constants... const double LO_CARBON_PRICE = 1.00; // could be global void get_input(double& radius, int& number, char& carbon); // prompted input for call-by-ref paramters void output(int number_bearings, double radius, char carbon); double tot_volume(int number_bearings, double radius); // computes volume of number_bearings spherical bearings of radius radius // using formula for sphere volume : 4/3*pi*radius-cubed int main() { int number_bearings; double radius, total_volume, volume_of_one; char carbon,continu; do { // while continu = Y get_input(radius,number_bearings,carbon); output(number_bearings,radius, carbon); cout << "Input 'Y' to continue, 'N' otherwise"; cin >> continu; } while (continu=='Y'); return 0; } void get_input(double& radius, int& number, char& carbon) {cout << "Input a decimal number for the radius\n"; cin >> radius; cout << "How many bearings?\n"; cin >> number; cout << "Input carbon: high (h) or low (l)\n"; cin >> carbon; } void output(int number_bearings, double radius, char carbon) { if (carbon=='h'|| carbon=='l') /*valid inputs*/ { cout << "The total volume required for " << number_bearings; cout << " bearings of radius " << radius << " is "; cout << tot_volume(number_bearings,radius); // format for 2 decimal output cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); {if(carbon=='h') cout << endl << "Total cost is $ " << tot_volume(number_bearings,radius)*HI_CARBON_PRICE; else cout << endl << "Total cost is $ " << tot_volume(number_bearings,radius)*LO_CARBON_PRICE;}} else /*traps bad input*/ {cout << endl << "Invalid input " << carbon; cout << endl << "Input value must be 'h' or 'l'";} cout << endl; } double tot_volume(int number_bearings, double radius) { const double PI=3.14159; double volume_of_one, total_volume; volume_of_one = 4.0/3.0*PI*pow(radius,3.0); total_volume = number_bearings* volume_of_one; return (total_volume); }