164k views
3 votes
IN C++

Integer vectSize is read from input, then vectSize strings are read and stored in vector airportsToPick. Complete the GenerateAllRoutes() recursive case to perform the following tasks:

Move the element at index i of remainAirports to the end of pickedAirports.

Recursively call GenerateAllRoutes().

Move the element from the end of pickedAirports back to index i of remainAirports.

Ex: If the input is 3 Dallas Miami Tampa, then the output is:

All routes:
Depart: Dallas, Arrive: Miami
Depart: Dallas, Arrive: Tampa
Depart: Miami, Arrive: Dallas
Depart: Miami, Arrive: Tampa
Depart: Tampa, Arrive: Dallas
Depart: Tampa, Arrive: Miami
Note:

Vector input always has at least 2 elements.

vector.erase(vector.begin() + i) removes the element at index i of vector.

vector.insert(vector.begin() + i, x) insert x into vector at index i.

#include
#include
using namespace std;

void GenerateAllRoutes(vector remainAirports, vector pickedAirports) {
unsigned int i;
string pick;

if (pickedAirports.size() == 2) {
cout << "Depart: " << pickedAirports.at(0) << ", Arrive: " << pickedAirports.at(1) << endl;
}
else {
for (i = 0; i < remainAirports.size(); ++i) {
pick = remainAirports.at(i);

/* Your code goes here */

}
}
}

int main() {
vector airportsToPick(0);
vector picks(0);
int vectSize;
string name;
unsigned int i;

cin >> vectSize;

for (i = 0; i < vectSize; ++i) {
cin >> name;
airportsToPick.push_back(name);
}

cout << "All routes:" << endl;

GenerateAllRoutes(airportsToPick, picks);

return 0;
}

1 Answer

5 votes

Final answer:

To complete the recursive case in the GenerateAllRoutes function, move the current airport from remainAirports to pickedAirports, call the function recursively, and then move the airport back to its original position in remainAirports.

Step-by-step explanation:

In the context of the C++ program provided, the GenerateAllRoutes recursive function generates all possible routes between airports. If there are only two airports in the pickedAirports vector, the base case will simply print out the route. However, if there are more than two airports in the remainAirports vector, the recursive case should iterate through the remainAirports, move each airport to pickedAirports, recursively call GenerateAllRoutes again, and then move the airport back to remainAirports to continue the iteration for other possibilities.

To complete the recursive case, you would do the following:

  • Move the element at index i of remainAirports to the end of pickedAirports.
  • Recursively call GenerateAllRoutes() with the updated remainAirports and pickedAirports.
  • Move the element from the end of pickedAirports back to index i of remainAirports.

The code inside the for loop should look like this:

// Move the element at index i from remainAirports to the end of pickedAirports
pickedAirports.push_back(pick);
remainAirports.erase(remainAirports.begin() + i);

// Recursively call GenerateAllRoutes with the updated vectors
GenerateAllRoutes(remainAirports, pickedAirports);

// Move the picked airport back to its original position in remainAirports
pickedAirports.pop_back();
remainAirports.insert(remainAirports.begin() + i, pick);
User Pratikad
by
8.7k points