fork download
  1. //Jeremy Huang CS1A Chapter 4, P. 224, #19
  2. //
  3. /**************************************************************
  4.  *
  5.  * COMPUTE DISTANCE TRAVELLED IN MEDIUM
  6.  * ____________________________________________________________
  7.  * This program takes input on which medium to calculate. It then
  8.  * uses the speed of sound within that medium to calculate the
  9.  * distance travelled based on the user input.
  10.  * ____________________________________________________________
  11.  * INPUT
  12.  * choice : choice from 1-4 to choose medium
  13.  * time : the amount of time sound travels in medium
  14.  *
  15.  * OUTPUT
  16.  * distance : amount of meters sound travelled through the medium
  17.  *
  18.  **************************************************************/
  19. #include <iostream>
  20. #include <iomanip>
  21. using namespace std;
  22.  
  23. int main() {
  24. const int CARBON_DIOXIDE_CHOICE = 1;
  25. const int AIR_CHOICE = 2;
  26. const int HELIUM_CHOICE = 3;
  27. const int HYDROGEN_CHOICE = 4;
  28.  
  29. const float SPEED_IN_CO2 = 258.0;
  30. const float SPEED_IN_AIR = 331.5;
  31. const float SPEED_IN_HELIUM = 972.0;
  32. const float SPEED_IN_HYDROGEN = 1270.0;
  33.  
  34. int choice; //INPUT - choice from 1-4 to choose medium
  35. float time; //INPUT - the amount of time sound travels in medium
  36. float distance; //OUTPUT - amount of meters sound travelled through the medium
  37.  
  38. //User Input
  39. cout << "Select a gas from the menu:"<<endl<<endl
  40. << "1. Carbon Dioxide"<<endl
  41. << "2. Air"<<endl
  42. << "3. Helium"<<endl
  43. << "4. Hydrogen"<<endl<<endl
  44. << "Enter your choice: ";
  45. cin >> choice;
  46. cout << fixed << setprecision(1);
  47.  
  48. //Output Result
  49. switch (choice) {
  50. case CARBON_DIOXIDE_CHOICE:
  51. cout << "Enter the number of seconds (0-30): ";
  52. cin >> time;
  53.  
  54. if (time >= 0 && time <= 30) {
  55. distance = SPEED_IN_CO2 * time;
  56. cout << "The sound traveled " << distance << " meters." << endl;
  57. } else {
  58. cout << "Invalid input. Time must be between 0 and 30 seconds." << endl;
  59. }
  60. break;
  61.  
  62. case AIR_CHOICE:
  63. cout << "Enter the number of seconds (0-30): ";
  64. cin >> time;
  65.  
  66. if (time >= 0 && time <= 30) {
  67. distance = SPEED_IN_AIR * time;
  68. cout << "The sound traveled " << distance << " meters." << endl;
  69. } else {
  70. cout << "Invalid input. Time must be between 0 and 30 seconds." << endl;
  71. }
  72. break;
  73.  
  74. case HELIUM_CHOICE:
  75. cout << "Enter the number of seconds (0-30): ";
  76. cin >> time;
  77.  
  78. if (time >= 0 && time <= 30) {
  79. distance = SPEED_IN_HELIUM * time;
  80. cout << "The sound traveled " << distance << " meters." << endl;
  81. } else {
  82. cout << "Invalid input. Time must be between 0 and 30 seconds." << endl;
  83. }
  84. break;
  85.  
  86. case HYDROGEN_CHOICE:
  87. cout << "Enter the number of seconds (0-30): ";
  88. cin >> time;
  89.  
  90. if (time >= 0 && time <= 30) {
  91. distance = SPEED_IN_HYDROGEN * time;
  92. cout << "The sound traveled " << distance << " meters." << endl;
  93. } else {
  94. cout << "Invalid input. Time must be between 0 and 30 seconds." << endl;
  95. }
  96. break;
  97.  
  98. default:
  99. cout << "Invalid choice. Please run the program again and select 1, 2, 3, or 4." << endl;
  100. break;
  101. }
  102. return 0;
  103. }
Success #stdin #stdout 0s 5320KB
stdin
1
20
stdout
Select a gas from the menu:

1. Carbon Dioxide
2. Air
3. Helium
4. Hydrogen

Enter your choice: Enter the number of seconds (0-30): The sound traveled 5160.0 meters.