5. Assume you are located at location: pair (0.0, 0.0). Write a function that ta
ID: 3904963 • Letter: 5
Question
5. Assume you are located at location: pair (0.0, 0.0). Write a function that takes an arbitrary fleet of vehicles and returns the dosest and farthest vehicles in the fleet as a pair. You will need to modify the Vehide dass so that it can return the current location. Also, you should calculate the distance using the formula: d - vx2 y2). This function should have the following signature: pair 2/3 CloseAndFar(const vector& fleet). Hint: You should be able to use the following code in main and get the correct results // move vehicles for (auto vehicle :fleet) vehicle->SendToLocation(paircdouble, double>( 1.e, 10) (next(fleet.begin),3)))->sendToLocation(paircdouble, double>(e.s,.3)) Cprev(fleet.end), 2)))->SendToLocation(pair(5.5, 3.3)) // print status for (auto vehicle: fleet) vehicle->Status) /I find and print closest and farthest auto minmax-CloseAndFar(fleet); cout Status) I1 should be the 2nd to last vehicleExplanation / Answer
pair<shared_ptr<Vehicle>,shared_ptr<Vehicle>> CloseAndFar(const vector<Vehicle>> &fleet){
pair<shared_ptr<Vehicle>, shared_ptr<Vehicle>> d;
double min = 1000;
int minindex =-1;
int maxindex = -1;
double max = 0;
for (int i = 0; i<fleet.size(); i++){
pair<double,double> m = fleet.get(i).getLocation();
double s = sqrt(m.first*m.first + m.second*m.second)
if (s > max){
max = s;
maxindex = i;
}
if (s < min){
min = s;
minindex = i;
}
}
if (minindex != -1 && maxindex != -1){
d.first = fleet[maxindex];
d.second = fleet[minindex];
}
else {
d.first = NULL;
d.second = NULL;
}
return d;
}
Vehicle class should have a method:
Vechicle::Status(){
cout << "Location: (" << location.first << "," << location.second << " )" << endl;
}
Vechicle::SendToLocation(pair<double,double> b){ // location is a memberin Vehicle class pair<double,double> locvation
location.first = b.first;
location.second = b.second;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.