In: Computer Science
Your team was asked to program a self-driving car that reaches its destination with minimum travel time.
Write an algorithm for this car to choose from two possible road trips. You will calculate the travel time of each trip based on the car current speed and the distance to the target destination. Assume that both distances and car speed are given.
Here is the solution if you have any doubt then please write in the comment section.
Please give feedback.
If there will be same speed for both routes then we are not required to calculate time because the route which have less distance will be chosen automatically.
Algorithm:
I am also writing the program for better understanding.
Program in C++
I have written comments for better understanding.
#include <iostream>
using namespace std;
int main()
{
//Declare 4 double variables firstDistance,secondDistance,firstSpeed,secondSpeed
double firstDistance,secondDistance,firstSpeed,secondSpeed;
//Input the 4 numbers, firstDistance,secondDistance,firstSpeed,secondSpeed respectively
cin>>firstDistance>>secondDistance>>firstSpeed>>secondSpeed;
//Declare 2 double variables firstTime,secondTime
double firstTime,secondTime;
//Divide firstDistance by firstSpeed and store in firstTime
firstTime=firstDistance/firstSpeed;
//Divide secondDistance by secondSpeed and store in secondTime
secondTime=secondDistance/secondSpeed;
//If firstTime is less than secondTime then
if(firstTime<secondTime)
{
cout<<"Time Taken by first route is:"<<firstTime<<" So Choose Route first";
}
//otherwise
else
{
cout<<"Time Taken by second route is:"<<secondTime<<" So Choose Route second";
}
return 0;
}
Output:
If you have any type of doubts then please write in the comment section, I will feel happy to help you.
Please give feedback.
Thank You!