# ternary operator(?), vectors, and recursive function

• 02-02-2003
kes103
ternary operator(?), vectors, and recursive function
I came across this little code which uses the ternary operator, vectors and a recursive function. I think I can see how the ternary operator ("?:") is used, but I dont understand several things:
1. What controls when the doit function is exited?
2. In the doit function which member of the p vector is called
with "/100,p)" ?
Can anyone help explain how this program is working?
Code:

```#include <string> #include <vector> #include <iostream> using namespace std; class Multiplier {         public: //     // a>0: this is amount spent on goods     // a<0: this the amount spent on services     int doit(int a, vector <int> &p){        //doit is recursive function ?         return             a>0?  a + doit(a*p[0]/100,p) + doit(-a*p[1]/100,p):             a<0? -a + doit(-a*p[2]/100,p) + doit(a*p[3]/100,p):0;         }         int spending(int amount, vector <int> percent){             return doit(amount, percent)-amount;         }     }; //     int main(void){         int amount;         vector <int> percent; /* //0) 23 {20,50,10,40} // //         amount=23;         percent.push_back(20);         percent.push_back(50);         percent.push_back(10);         percent.push_back(40); /* // 1) 100000 {35,60,70,25} */ //         amount=100000;         percent.push_back(35);         percent.push_back(60);         percent.push_back(70);         percent.push_back(25); /* // 2) 5 {19,19,19,19} // //         amount=5;         percent.push_back(19);         percent.push_back(19);         percent.push_back(19);         percent.push_back(19); */ //         Multiplier p;         cout<<"Total Add. Expenditure Gen.="<< p.spending(amount,percent);         cin >> "";    //pause console         percent.~vector<int>();        //Vector Destructor-frees memory         return (0); }```
• 02-02-2003
grib
It returns when a==0, this will never happen if one of the first four elements is greater than 100.

It explicitly calls the destructor, this is a big no-no (unless you know exactly what you are doing and even then its mostly a desperate hack) The destructor will be called when percent goes out of scope.

As to why it is doing this, or the correctness of the answer I cannot even begin to fathom. It looks like a quick one-off or homework for a buisness class to me.
• 02-02-2003
kes103