/* Speed Of Sound In Gases - When sound travels through a gas, its speed
depends primarily on the density of the medium. The less dense the
medium, the faster the speed will be. The following table shows the
approximate speed of sound at 0° centigrade, measured in m/sec, when
traveling through carbon dioxide, air, helium, and hydrogen.
Medium Speed (mp/s)
Carbon Dioxide 258.0
Air 331.5
Helium 972.0
Hydrogen 1,270.0
This program displays a menu allowing the user to select one of these
four gases. After a selection has been made, the user should enter the
number of seconds it took for the sound to travel in this medium from
its source to the location at which it was detected. The program then
reports how far away (in meters) the source of the sound was from the
detection location.
Input validation: The user can only select among the available menu
choices. Times less than 0 seconds or more than 30 seconds are not
allowed. */
#include "Utility.h"
int main()
{
/* Constants for speed */
const double CARBON_DIOXIDE_MPS = 258.0,
AIR_MPS = 331.0,
HELIUM_MPS = 972.0,
HYDROGEN_MPS = 1270.0;
/* Constants for menu choices */
const int CARBON_DIOXIDE = 1,
AIR = 2,
HELIUM = 3,
HYDROGEN = 4,
QUIT = 5;
/* Hold speed in seconds, distance of the source, and distancec
of the destination */
double speedSeconds = 0,
distance = 0;
/* Hold menu choice */
int choice;
/* Displaying the menu */
cout << "\t\tSpeed Of Sound In Gases\n\n"
<< "Selecet a medium from the menu through which the speed of "
<< "sound should travel.\n"
<< "Then enter a speed in seconds. I will then tell you how far the "
<< "source of the\n"
<< "sound was apart from the point it was detected.\n\n"
<< "1. CARBON DYOXIDE\n"
<< "2. AIR\n"
<< "3. HELIUM\n"
<< "4. HYDROGEN\n"
<< "5. QUIT\n";
cin >> choice;
/* Format the output */
cout << fixed << showpoint << setprecision(2);
switch (choice)
{
case 1:
/* Display the choice the user has made and ask for speed in
seconds */
cout << "\t\t1. CARBON DIOXIDE\n\n";
cout << "Enter speed in seconds (1 through 30): ";
cin >> speedSeconds;
/* Calculations */
distance = (CARBON_DIOXIDE_MPS * speedSeconds);
/* Validate input and display result */
if (speedSeconds < 1 || speedSeconds > 30)
{
cout << "You entered an invalid amount of time!\n"
<< "Only 1 through 30 seconds are allowed.\n";
}
else
{
cout << "\nThe distance from the source of sound and the"
<< "location of\n"
<< "detection in a time of " << speedSeconds
<< " sec. is: " << distance << "m.\n";
}
break;
case 2:
cout << "\t\t2. AIR\n\n"
<< "Enter speed in seconds (1 through 30): ";
cin >> speedSeconds;
distance = (AIR_MPS * speedSeconds);
if (speedSeconds < 1 || speedSeconds > 30)
{
cout << "You entered an invalid amount of time!\n"
<< "Only 1 through 30 seconds are allowed.\n";
}
else
{
cout << "\nThe distance from the source of sound and the"
<< "location of\n"
<< "detection in a time of " << speedSeconds
<< " sec. is: " << distance << "m.\n";
}
break;
case 3:
cout << "\t\t3. HELIUM\n\n"
<< "Enter speed in seconds (1 through 30): ";
cin >> speedSeconds;
distance = (HELIUM_MPS * speedSeconds);
if (speedSeconds < 1 || speedSeconds > 30)
{
cout << "You entered an invalid amount of time!\n"
<< "Only 1 through 30 seconds are allowed.\n";
}
else
{
cout << "\nThe distance from the source of sound and the"
<< "location of\n"
<< "detection in a time of " << speedSeconds
<< " sec. is: " << distance << "m.\n";
}
break;
case 4:
cout << "\t\t4. HYDROGEN\n\n"
<< "Enter the speed in seconds (1 through 30): ";
cin >> speedSeconds;
distance = (HYDROGEN_MPS * speedSeconds);
if (speedSeconds < 1 || speedSeconds > 30)
{
cout << "You entered an invalid amount of time!\n"
<< "Only 1 through 30 seconds are allowed.\n";
}
else
{
cout << "\nThe distance from the source of sound and the"
<< "location of\n"
<< "detection in a time of " << speedSeconds
<< " sec. is: " << distance << "m.\n";
}
break;
case 5:
cout << "Bye, bye!\n";
break;
default:
cout << "Invalid choice! Valid choices are 1 through 4, or 5 "
<< "to quit.\n";
break;
}
pauseSystem();
return 0;
}
Thursday, November 24, 2016
Programming Challenge 4.21 - Speed Of Sound In Gases
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment