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 and the elapsed time (in seconds) and
outputs the total distance each car has traveled as well as the shortest distance between the cars.
Output data MUST be rounded to 2 decimal places and have precision set so that all data is evenly lined
up. For simplicity, assume that decimal numbers will be no more than 8 digits long.
Source code
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
double x,y;
cout<<"\nEnter the average speed of car A: ";
cin>>x;
cout<<"\nEnter the average speed of car B: ";
cin>>y;
int time_elp;
cout<<"\nEnter time elapsed in seconds: ";
cin>>time_elp;
double total_distance_A=x*(time_elp/3600);
double total_distance_B=y*(time_elp/3600);
double shortest_distance=sqrt((total_distance_A*total_distance_A)+(total_distance_B*total_distance_B));
cout<<"\nTotal distance travelled by car A: "<<total_distance_A;
cout<<"\nTotal distance travelled by car B: "<<total_distance_B;
cout<<"\nShortest distance: "<<shortest_distance;
return 0;
}
Output
Comments
Leave a comment