Two cars A and B leave an intersection at the same time. Car A travels west at an average speed of x miles per hour and car B travels south at an average speed of y miles per hour. Write a program that prompts the user to enter: The average speed of both the cars The elapsed time (in hours and minutes, separated by a space) Ex: For two hours and 30 minutes, 2 30 would be entered

Respuesta :

Answer:

Here is the C++ program:

#include <iostream>  // to use input output functions

#include <cmath>  // to use math functions like sqrt()

#include <iomanip>  //to use setprecision method

using namespace std;   //to access objects like cin cout

int main ()  {  //start of main function

  double speedA;  //double type variable to store average speed of car A

  double speedB;  //double type variable to store average speed of car B

  int hour;  //int type variable to hold hour part of elapsed time

  int minutes;  //int type variable to hold minutes part of elapsed time

  double shortDistance;  // double type variable to store the result of shortest distance between car A and B

  double distanceA;  //stores the distance of carA

  double distanceB;  //stores the distance of carB

  double mins,hours;   //used to convert the elapsed time

cout << "Enter average speed of car A: " << endl;  //prompt user to enter the average speed of car A

cin >> speedA;   //reads the input value of average speed of car A from user

cout << "Enter average speed of car B: " << endl ;  //prompt user to enter the average speed of car B

cin >> speedB;   //reads the input value of average speed of car A from user

cout << "Enter elapsed time (in hours and minutes, separated by a space): " << endl;  //prompts user to enter elapsed time

cin>> hour >> minutes;    //reads elapsed time in hours and minutes

  mins = hour * 60;  //computes the minutes using value of hour

  hours = (minutes+mins)/60;     //computes hours using minutes and mins

distanceA = speedA * (hours);  // computes distance of car A

distanceB = speedB * (hours);   //computes distance of car B

   shortDistance =sqrt((distanceA * distanceA) + (distanceB * distanceB));   //computes shortest distance using formula √[(distanceA)² + (distanceB)²)]

cout << "The (shortest) distance between the cars is: "<<fixed<<setprecision(2)<<shortDistance;

//display the resultant value of shortDistance up to 2 decimal places

Explanation:

I will explain the program with an examples:

Let us suppose that the average speeds of cars are:

speedA = 70

speedB = 55

Elapsed time in hours and minutes:

hour = 2

minutes = 30

After taking these input values the program control moves to the statement:

mins = hour * 60;  

This becomes

mins = 2 * 60

mins = 120

Next

hours = (minutes+mins)/60;

hours = (30 + 120) / 60

         = 150/60

hours = 2.5

Now the next two statements compute distance of the cars:

distanceA = speedA * (hours);  

this becomes

distanceA = 70 * (2.5)

distanceA = 175

distanceB = speedB * (hours);

distanceB = 55 * (2.5)

distanceB = 137.5

Next the shortest distance between car A and car B is computed:

shortDistance = sqrt((distanceA * distanceA) + (distanceB * distanceB));

shortDistance = sqrt((175 * 175) + (137.5 * 137.5))

                        = sqrt(30625 + 18906.25)

                        = sqrt(49531.25)

                        =  222.556173

shortDistance =  222.56

 

Hence the output is:

The (shortest) distance between the cars is: 222.56        

Ver imagen mahamnasir