//Jeremy Huang CS1A Chapter 4, P. 224, #19
//
/**************************************************************
*
* COMPUTE DISTANCE TRAVELLED IN MEDIUM
* ____________________________________________________________
* This program takes input on which medium to calculate. It then
* uses the speed of sound within that medium to calculate the
* distance travelled based on the user input.
* ____________________________________________________________
* INPUT
* choice : choice from 1-4 to choose medium
* time : the amount of time sound travels in medium
*
* OUTPUT
* distance : amount of meters sound travelled through the medium
*
**************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
const int CARBON_DIOXIDE_CHOICE = 1;
const int AIR_CHOICE = 2;
const int HELIUM_CHOICE = 3;
const int HYDROGEN_CHOICE = 4;
const float SPEED_IN_CO2 = 258.0;
const float SPEED_IN_AIR = 331.5;
const float SPEED_IN_HELIUM = 972.0;
const float SPEED_IN_HYDROGEN = 1270.0;
int choice; //INPUT - choice from 1-4 to choose medium
float time; //INPUT - the amount of time sound travels in medium
float distance; //OUTPUT - amount of meters sound travelled through the medium
//User Input
cout << "Select a gas from the menu:"<<endl<<endl
<< "1. Carbon Dioxide"<<endl
<< "2. Air"<<endl
<< "3. Helium"<<endl
<< "4. Hydrogen"<<endl<<endl
<< "Enter your choice: ";
cin >> choice;
cout << fixed << setprecision(1);
//Output Result
switch (choice) {
case CARBON_DIOXIDE_CHOICE:
cout << "Enter the number of seconds (0-30): ";
cin >> time;
if (time >= 0 && time <= 30) {
distance = SPEED_IN_CO2 * time;
cout << "The sound traveled " << distance << " meters." << endl;
} else {
cout << "Invalid input. Time must be between 0 and 30 seconds." << endl;
}
break;
case AIR_CHOICE:
cout << "Enter the number of seconds (0-30): ";
cin >> time;
if (time >= 0 && time <= 30) {
distance = SPEED_IN_AIR * time;
cout << "The sound traveled " << distance << " meters." << endl;
} else {
cout << "Invalid input. Time must be between 0 and 30 seconds." << endl;
}
break;
case HELIUM_CHOICE:
cout << "Enter the number of seconds (0-30): ";
cin >> time;
if (time >= 0 && time <= 30) {
distance = SPEED_IN_HELIUM * time;
cout << "The sound traveled " << distance << " meters." << endl;
} else {
cout << "Invalid input. Time must be between 0 and 30 seconds." << endl;
}
break;
case HYDROGEN_CHOICE:
cout << "Enter the number of seconds (0-30): ";
cin >> time;
if (time >= 0 && time <= 30) {
distance = SPEED_IN_HYDROGEN * time;
cout << "The sound traveled " << distance << " meters." << endl;
} else {
cout << "Invalid input. Time must be between 0 and 30 seconds." << endl;
}
break;
default:
cout << "Invalid choice. Please run the program again and select 1, 2, 3, or 4." << endl;
break;
}
return 0;
}