Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The Speed of Sound - string comparison methods 4 points • The Speed of sound in

ID: 3757285 • Letter: T

Question

The Speed of Sound - string comparison methods 4 points

• The Speed of sound in

• Medium Speed

• Air 1100/sec

• Water 4900/sec

• Steel 16,400/sec

• Input -Write a program that asks user to enter the medium “air, water or Steel” (which you store in string variable) and the distance that sound wave will travel in the medium.

• First validate if the input is correct. Otherwise a message “Please enter the correct medium should be displayed Calculations Time = Distance/speed •

Output – The amount of time it takes to travel in the medium Enter one of the following: air, water, or steel: steel Enter the distance the sound wave will travel: 2300 It will take 0.1402439024390244 seconds.

Explanation / Answer

I have soved this question using cpp if u expecting in another language please do let me know.


Save the below code as demo.cpp or any another name with .cpp extension


#include <iostream>

#include <string>

using namespace std;

int main(int argc, char const *argv[]) {

    string medium;

    double distance;

    double time_taken;

    cout << "The amount of time it takes to travel in the medium " << endl;

    // using while validating and calculating time

    while (1) {

        cout << "Enter one of the following: air, water, or steel: ";

        cin >> medium;

        cout << " Enter the distance the sound wave will travel: ";

        cin >> distance;

        // comparing the medium

        if (medium == "air") {

            time_taken = distance / 1100;

            break;

        } else if (medium == "water") {

            time_taken = distance / 4900;

            break;

        } else if (medium == "steel") {

            time_taken = distance / 16400;

            break;

        } else {

            // if incorrect medium then looping again

            cout << "Please enter the correct medium " << endl;

        }

    }

    cout << "It will take " << time_taken << endl;

    return 0;

}//end of code


//OUT

$./a.out
The amount of time it takes to travel in the medium
Enter one of the following: air, water, or steel: wind

Enter the distance the sound wave will travel: 2344
Please enter the correct medium

Enter one of the following: air, water, or steel: steel

Enter the distance the sound wave will travel: 2300
It will take 0.140244

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote