//Natalie Zarate CIS 5 Chapter 4, P. 222, #18
/*******************************************************************************
*
* COMPUTE SPEED OF SOUND
* _____________________________________________________________________________
* This program computes the time it will take for sound to through steel, air
* in ft/sec with the distance traveled and the medium (air, water or steel)
* specified by the user.
*
* Computation will be based on the following:
* - The speed of sound through air is 1,100 ft/sec
* - The speed of sound through water is 4,900 ft/sec
* - The speed of sound through steel is 16,400 ft/sec
* - Distance must be > 0
* _____________________________________________________________________________
* INPUT
* mediumchoice : Choice of medium
* steelSpeed : Speed that sound travels through steel
* airSpeed : Speed that sound travels through air
* waterSpeed : Speed that sound travels through water
* distance : Distance the sound will travel
*
* OUTPUT
* timeTravel : Time it took for sound to travel
*
******************************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
/*********************************************************************
* CONSTANTS
* -------------------------------------------------------------------
* steelSpeed : Speed that sound travels through steel
* airSpeed : Speed that sound travels through air
* waterSpeed : Speed that sound travels through water
********************************************************************/
const float steelSpeed = 16400;
const float airSpeed = 1100;
const float waterSpeed = 4900;
char mediumchoice; // INPUT - Choice of medium
float distance; // INPUT - Distance sound will travel
float timeTravel; // Output - Time it takes for sound to travel
// Promt user for medium selection
cout << "What is sound travelling through?" << endl;
cout << "Please select from the following:" << endl;
cout << "A - Air" << endl << "B - Water" << endl << "C - Steel" << endl;
cin >> mediumchoice;
// Validate input and prompt for distance
switch (mediumchoice)
{
case 'A': cout << "How far will the sound travel in feet?" << endl;
cin >> distance;
break;
case 'B': cout << "How far will the sound travel in feet?" << endl;
cin >> distance;
break;
case 'C': cout << "How far will the sound travel in feet?" << endl;
cin >> distance;
break;
default : cout << "Enter A, B, or C:" << endl;
cin >> mediumchoice;
}
// Validate Distance
if (distance <= 0)
cout << "Distance muts be greater than 0 feet!" << endl;
// Compute time traveled
switch (mediumchoice)
{
case 'A': timeTravel = distance / airSpeed;
break;
case 'B': timeTravel = distance / waterSpeed;
break;
case 'C': timeTravel = distance / steelSpeed;
break;
}
// Display compuation
cout << "It will take " << setprecision(4) << timeTravel;
cout << " seconds for sound to travel " << distance << " feet in ";
switch (mediumchoice)
{
case 'A': cout << "air.";
break;
case 'B': cout << "water.";
break;
case 'C':cout << "steel";
break;
}
return 0;
}