Advertisement
AbsolutelyS

Untitled

Apr 18th, 2024
669
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. #include "BuildingRec.hpp"
  2.  
  3. /**
  4.  * ! EXERCISE #7 -- IMPLEMENT A RECURSIVE SEARCH METHOD
  5.  *
  6.  * Count the buildings that are younger than the target object building.
  7.  *
  8.  * The method MUST BE RECURSIVE.
  9.  */
  10. unsigned int Building::countYoungerBuildingsHelper(vector<Building *> &buildings, unsigned int i) {
  11.     if(buildings.size() <= i)
  12.         {
  13.             return 0;
  14.         }
  15.  
  16.     if (buildings[i]->getAge() < this->getAge())
  17.         {
  18.             return 1 + countYoungerBuildingsHelper(buildings, i + 1);
  19.         }
  20.     else
  21.         {
  22.             return countYoungerBuildingsHelper(buildings, i + 1);
  23.         }
  24. }
  25.  
  26. unsigned int Building::countYoungerBuildings(vector<Building *> &buildings) {
  27.     return countYoungerBuildingsHelper(buildings, 0);
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement