Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "BuildingRec.hpp"
- /**
- * ! EXERCISE #7 -- IMPLEMENT A RECURSIVE SEARCH METHOD
- *
- * Count the buildings that are younger than the target object building.
- *
- * The method MUST BE RECURSIVE.
- */
- unsigned int Building::countYoungerBuildingsHelper(vector<Building *> &buildings, unsigned int i) {
- if(buildings.size() <= i)
- {
- return 0;
- }
- if (buildings[i]->getAge() < this->getAge())
- {
- return 1 + countYoungerBuildingsHelper(buildings, i + 1);
- }
- else
- {
- return countYoungerBuildingsHelper(buildings, i + 1);
- }
- }
- unsigned int Building::countYoungerBuildings(vector<Building *> &buildings) {
- return countYoungerBuildingsHelper(buildings, 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement