fork download
  1. //Natalie Zarate CIS 5 Chapter 4, P. 222, #18
  2.  
  3. /*******************************************************************************
  4.  *
  5.  * COMPUTE SPEED OF SOUND
  6.  * _____________________________________________________________________________
  7.  * This program computes the time it will take for sound to through steel, air
  8.  * in ft/sec with the distance traveled and the medium (air, water or steel)
  9.  * specified by the user.
  10.  *
  11.  * Computation will be based on the following:
  12.  * - The speed of sound through air is 1,100 ft/sec
  13.  * - The speed of sound through water is 4,900 ft/sec
  14.  * - The speed of sound through steel is 16,400 ft/sec
  15.  * - Distance must be > 0
  16.  * _____________________________________________________________________________
  17.  * INPUT
  18.  * mediumchoice : Choice of medium
  19.  * steelSpeed : Speed that sound travels through steel
  20.  * airSpeed : Speed that sound travels through air
  21.  * waterSpeed : Speed that sound travels through water
  22.  * distance : Distance the sound will travel
  23.  *
  24.  * OUTPUT
  25.  * timeTravel : Time it took for sound to travel
  26.  *
  27.  ******************************************************************************/
  28. #include <iostream>
  29. #include <iomanip>
  30. using namespace std;
  31.  
  32. int main()
  33. {
  34. /*********************************************************************
  35. * CONSTANTS
  36. * -------------------------------------------------------------------
  37.   * steelSpeed : Speed that sound travels through steel
  38.   * airSpeed : Speed that sound travels through air
  39.   * waterSpeed : Speed that sound travels through water
  40.   ********************************************************************/
  41.  
  42. const float steelSpeed = 16400;
  43. const float airSpeed = 1100;
  44. const float waterSpeed = 4900;
  45.  
  46. char mediumchoice; // INPUT - Choice of medium
  47. float distance; // INPUT - Distance sound will travel
  48. float timeTravel; // Output - Time it takes for sound to travel
  49.  
  50. // Promt user for medium selection
  51. cout << "What is sound travelling through?" << endl;
  52. cout << "Please select from the following:" << endl;
  53. cout << "A - Air" << endl << "B - Water" << endl << "C - Steel" << endl;
  54. cin >> mediumchoice;
  55.  
  56. // Validate input and prompt for distance
  57. switch (mediumchoice)
  58. {
  59. case 'A': cout << "How far will the sound travel in feet?" << endl;
  60. cin >> distance;
  61. break;
  62. case 'B': cout << "How far will the sound travel in feet?" << endl;
  63. cin >> distance;
  64. break;
  65. case 'C': cout << "How far will the sound travel in feet?" << endl;
  66. cin >> distance;
  67. break;
  68. default : cout << "Enter A, B, or C:" << endl;
  69. cin >> mediumchoice;
  70. }
  71.  
  72. // Validate Distance
  73. if (distance <= 0)
  74. cout << "Distance muts be greater than 0 feet!" << endl;
  75. // Compute time traveled
  76. switch (mediumchoice)
  77. {
  78. case 'A': timeTravel = distance / airSpeed;
  79. break;
  80. case 'B': timeTravel = distance / waterSpeed;
  81. break;
  82. case 'C': timeTravel = distance / steelSpeed;
  83. break;
  84. }
  85. // Display compuation
  86. cout << "It will take " << setprecision(4) << timeTravel;
  87. cout << " seconds for sound to travel " << distance << " feet in ";
  88. switch (mediumchoice)
  89. {
  90. case 'A': cout << "air.";
  91. break;
  92. case 'B': cout << "water.";
  93. break;
  94. case 'C':cout << "steel";
  95. break;
  96. }
  97. return 0;
  98. }
Success #stdin #stdout 0.01s 5284KB
stdin
B
300
stdout
What is sound travelling through?
Please select from the following:
A - Air
B - Water
C - Steel
How far will the sound travel in feet?
It will take 0.06122 seconds for sound to travel 300 feet in water.