/* Speed Of Sound - This program displays a menu, allowing the user to
select air, water or steel. After the user has made a selection, he
or she should be asked to enter the distance a sound wave will travel
in the selected medium. The program will then display the amount of
time it will take. The answer is rounded to four decimal places.
Medium Speed
Air 1,100 feet per second
Water 4,900 feet per second
Steel 16,400 feet per second
Input Validation: The user can only select menu items 1 through 4.
Distances of 0 or less are not accepted. */
#include "Utility.h"
int main()
{
/* Constants for menu */
const int TOC_Air = 1,
TOC_Water = 2,
TOC_Steel = 3;
/* Constants for feet per second */
const double AIR_FPS = 1100.0,
WATER_FPS = 4900.0,
STEEL_FPS = 16400.0;
/* Hold menu choice */
int choice = 0;
/* Hold input for, distance in feet, medium, amount of time */
double distance = 0,
amountOfTime = 0;
/* Display the menu */
cout << "\t\tSpeed Of Sound - Please select Air, Water or Steel as "
<< "medium.\n\n"
<< "1. Air\n"
<< "2. Water\n"
<< "3. Steel\n"
<< "4. Quit\n";
cin >> choice;
/* Format the output and display the result */
cout << fixed << showpoint << setprecision(4);
/* Choices */
switch (choice)
{
case 1:
/* Display the user's choice and Ask the user to enter distance */
cout << "\t\tAir\n\n"
<< "Enter a distance in feet the sound waves travel through "
<< "this medium: ";
cin >> distance;
/* Calculation */
amountOfTime = (distance / AIR_FPS);
/* Input validation */
if (distance <= 0)
{
cout << "You entered an invalid distance. Only distances above 0\n"
<< "are allowed!.\n";
}
else
{
/* Display the result */
cout << "It takes sound waves " << amountOfTime << " ft/sec "
<< "to travel a distance of " << setprecision(2) << distance
<< " ft. through air.\n";
}
break;
case 2:
cout << "\t\tWater\n\n"
<< "Enter a distance in feet the sound waves travel through "
<< "this medium: ";
cin >> distance;
amountOfTime = (distance / WATER_FPS);
if (distance <= 0)
{
cout << "You entered an invalid distance. Only distances above 0\n"
<< "are allowed!.\n";
}
else
{
cout << "It takes sound waves " << amountOfTime << " ft/sec "
<< "to travel a distance of " << setprecision(2) << distance
<< " ft. through water.\n";
}
break;
case 3:
cout << "\t\tSteel\n\n"
<< "Enter a distance in feet the sound waves travel through "
<< "this medium: ";
cin >> distance;
amountOfTime = (distance / STEEL_FPS);
if (distance <= 0)
{
cout << "You entered an invalid distance. Only distances above 0\n"
<< "are allowed!.\n";
}
else
{
cout << "It takes sound waves " << amountOfTime << " ft/sec "
<< "to travel a distance of " << setprecision(2) << distance
<< " ft. through steel.\n";
}
break;
case 4:
cout << "\t\tQuit\n\n"
<< "Exiting program ...\n";
break;
default:
cout << "You entered an invalid menu-item. Please choose 1 through 4.\n";
}
pauseSystem();
return 0;
}
Thursday, November 24, 2016
Programming Challenge 4.20 - Speed Of Sound
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment