When Sound travels through a gas, its speed depends primarily on the density of
ID: 3630726 • Letter: W
Question
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 degrees centigrade, measured in meters per second, when traveling through carbon dioxide, air, helium, and hydrogen.
Medium Speed(Meters per Second)
Carbon Dioxide 258.0
Air 331.5
Helium 972.0
Hydrogen 1,270.0
Write a program that displays a menu allowing 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 should then report how far away (in meters) the source of the sound was from the detection location.
Input Validation: Check that the user has selected one of the available menu choices. Do not accept times less than 0 seconds or more than 30 seconds.
Explanation / Answer
please rate - thanks
#include <iostream>
using namespace std;
int main()
{
double speed;
int error,sec,choice;
do
{
cout<<"Choose gas would you like to use ";
cout<<"1 CarbonDioxide ";
cout<<"2 Air ";
cout<<"3 Helium ";
cout<<"4 Hydrogen ";
cin>>choice;
error=0;
switch(choice)
{case 1: speed=258;
break;
case 2: speed=331.5;
break;
case 3: speed=972;
break;
case 4: speed=1270;
break;
default: cout<<"Illegal input: Try Again ";
error=1;
}
}while(error==1);
cout<<"how many seconds did the sound travel? ";
cin>>sec;
while(sec<0||sec>30)
{cout<<"must me between 0 and 30 ";
cout<<"how many seconds did the sound travel? ";
cin>>sec;
}
cout<<"The source of sound was "<<speed*sec<<" meters away ";
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.