fork download
  1. // Marvyn De La Torre CS1A Chapter 4, P.223, #18
  2.  
  3. /**************************************************************
  4. * Calculate Speed of Sound
  5. *
  6. * This program calculates the time required for sound to travel
  7. * a given distance through air, water, or steel.
  8. *
  9. * Input:
  10. * Medium selected = choice
  11. * Distance in feet = distance
  12. *
  13. * Output:
  14. * Speed of sound = speed
  15. * Time in seconds = time
  16. **************************************************************/
  17.  
  18. #include <iostream>
  19. #include <iomanip>
  20. using namespace std;
  21.  
  22. int main()
  23. {
  24. int choice;
  25. double distance, speed, time;
  26.  
  27. cout << "Speed of Sound" << endl;
  28. cout << "1. Air" << endl;
  29. cout << "2. Water" << endl;
  30. cout << "3. Steel" << endl;
  31. cout << "Enter your choice: ";
  32. cin >> choice;
  33.  
  34. cout << "Enter the distance in feet: ";
  35. cin >> distance;
  36.  
  37. if (distance < 0)
  38. {
  39. cout << "Distance cannot be negative." << endl;
  40. }
  41. else
  42. {
  43. switch (choice)
  44. {
  45. case 1:
  46. speed = 1100;
  47. break;
  48.  
  49. case 2:
  50. speed = 4900;
  51. break;
  52.  
  53. case 3:
  54. speed = 16400;
  55. break;
  56.  
  57. default:
  58. cout << "Invalid choice." << endl;
  59. return 0;
  60. }
  61.  
  62. time = distance / speed;
  63.  
  64. cout << fixed << showpoint << setprecision(4);
  65. cout << "Time: " << time << " seconds" << endl;
  66. }
  67.  
  68. return 0;
  69. }
Success #stdin #stdout 0.01s 5324KB
stdin
3 4 
stdout
Speed of Sound
1. Air
2. Water
3. Steel
Enter your choice: Enter the distance in feet: Time: 0.0002 seconds