Thread: ternary operator(?), vectors, and recursive function

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    104

    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);
    }

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    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.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    104
    Thanks, grib, but what about

    2. In the doit function which member of the p vector is called
    with "/100,p)" ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help designing a recursive function.
    By broli86 in forum C Programming
    Replies: 3
    Last Post: 07-24-2008, 12:45 PM
  2. recursive function
    By technosavvy in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 05:42 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 02:53 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM