When sound travels through a gas, its speed depends primarily on the density of
ID: 3621435 • 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. Write a program that 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 should then report how far away (in meters) the source of the sound was from the detection location. Convert your distance in meters to miles using 1 Mile = 1609.344 Meters. If the sound source is less than one mile away, print "very close", between one and five miles away, print "moderately close", and greater than five miles away, print "far Away." Your screen output should match the test case below.
Medium....................................... Speed (Meters per Second)
Carbon Dioxide 258.0
Air 331.5
Helium 972.0
Hydrogen 1270.0
1. Use a do-while loop in your main program to control your menu processing.
2. Use a switch statement for the menu. Options 1, 2, 3, and 4 should be calls to functions to handle the processing of each gas. You may define other functions if you choose.
3. The distance should be expressed with one decimal place.
4. Screen output should match exactly for the test case.
5. If any input item is incorrect, the program should print an error message and ask for an acceptable value until the user supplies it. Your program should check that the option choice is in the valid range.
Check that the number of seconds used is not a negative value or greater than 30 seconds.
6. Use only statements covered in Chapters 1 - 6.
This part bellow is the way the program should look when is finished.
( Is not a part of the question but the finish program)
1. Distance in carbon dioxide
2. Distance in air
3. Distance in helium
4. Distance in hydrogen
5. Quit
Enter your choice (1-5): 2
Enter the number of seconds elapsed: 2.5
The distance travelled in air is 828.8 meters or 0.5 miles.
The sound source is very close.
1. Distance in carbon dioxide
2. Distance in air
3. Distance in helium
4. Distance in hydrogen
5. Quit
Enter your choice (1-5): 5
Programmer: insert your name here
Explanation / Answer
The mathematical calculation involved in this question is pretty obvious : distance = speed * time taken. Now, as the speed changes depending on the medium, we have a choice of medium. We here use a decision making structure called switch-case, implying that depending on the value given we switch to the corresponding case. so, switch(1) would give u dist. in CO2 , switch(2) in air and so forth. Th code in C++ has been given : #include #include void main() { float distance=0.0; float time=0.0; float speed = 0.0; int choice=0; int cont=0; do { coutchoice; switch(choice) { case 1: speed = 258.0; do { couttime; }while(time30); distance = speed*time; coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.