Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++: The following table shows the approximate speed of sound in air, water, and

ID: 3827007 • Letter: C

Question

C++: The following table shows the approximate speed of sound in air, water, and steel.

____________________________________________________________

Medium                                 Speed

____________________________________________________________

Air                                         1,100 feet per second

Water                                    4,900 feet per second

Steel                                      16,400 feet per second

____________________________________________________________

Write a program that 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. (Round the answer to four decimal places.) Your program should include a loop that lets the user repeat this calculation until the user says she or he is done.

Input Validation: Check that the user has selected one of the available choices from the menu. Do not accept distances less than 0.   

Sample output: Select a medium: 1. Air 2. Water 3. Steel 4. Quit Enter your choice: 2 Enter the distance: 3000 A sound wave takes xxxxx seconds to travel 3000.0000 feet through water. Select a medium: 1. Air 2. Water 3. Steel 4. Quit Enter your choice: 4 Program ending

Explanation / Answer

#include <iostream>

using namespace std;

int main()
{
int choice ;
double seconds;
int distance;
while(true){
cout<<"Select a medium: 1. Air 2. Water 3. Steel 4. Quit Enter yor choice: ";
cin >> choice;
if(choice == 1){
cout<<"Enter the distance: ";
cin >> distance;
seconds = distance/1100.0;
cout<<"The sound wave takes "<<seconds<<" seconds to travel "<<distance<<" feet through water."<<endl;
}
else if(choice == 2){
cout<<"Enter the distance: ";
cin >> distance;
seconds = distance/4900.0;
cout<<"The sound wave takes "<<seconds<<" seconds to travel "<<distance<<" feet through water."<<endl;
}
else if(choice == 3){
cout<<"Enter the distance: ";
cin >> distance;
seconds = distance/16400.0;
cout<<"The sound wave takes "<<seconds<<" seconds to travel "<<distance<<" feet through water."<<endl;
}
else if(choice == 4){
break;
}
else{
cout<<"Program ending."<<endl;
}
}
return 0;
}

Output:

sh-4.2$ g++ -o main *.cpp                                                                                                                                                                                                                                                

sh-4.2$ main                                                                                                                                                                                                                                                             

Select a medium:                                                                                                                                                                                                                                                         

1. Air                                                                                                                                                                                                                                                                   

2. Water                                                                                                                                                                                                                                                                 

3. Steel                                                                                                                                                                                                                                                                 

4. Quit                                                                                                                                                                                                                                                                  

                                                                                                                                                                                                                                                                         

Enter yor choice: 2                                                                                                                                                                                                                                                      

Enter the distance: 3000                                                                                                                                                                                                                                                 

The sound wave takes 0.612245 seconds to travel 3000 feet through water.                                                                                                                                                                                                 

Select a medium:                                                                                                                                                                                                                                                         

1. Air                                                                                                                                                                                                                                                                   

2. Water                                                                                                                                                                                                                                                                 

3. Steel                                                                                                                                                                                                                                                                 

4. Quit                                                                                                                                                                                                                                                                  

                                                                                                                                                                                                                                                                         

Enter yor choice: 1                                                                                                                                                                                                                                                      

Enter the distance: 3000                                                                                                                                                                                                                                                 

The sound wave takes 2.72727 seconds to travel 3000 feet through water.                                                                                                                                                                                                  

Select a medium:                                                                                                                                                                                                                                                         

1. Air                                                                                                                                                                                                                                                                   

2. Water                                                                                                                                                                                                                                                                 

3. Steel                                                                                                                                                                                                                                                                 

4. Quit                                                                                                                                                                                                                                                                  

                                                                                                                                                                                                                                                                         

Enter yor choice: 4

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote