Thread: Recursive function and apvector

  1. #1
    Niranjan
    Guest

    Unhappy Recursive function and apvector

    I have the task of creating a program that lets the user input numbers into an apvector then let them enter a goal(final number), and have a recursive function tell whether or not the numbers in the apvector can add up to be exactly the goal number. Im not sure where to start but this is what i have done so far.

    Code:
    bool IsPossible(const apvector <int> &A, int goal)
    {
      
    // I think i need code to add the elements of an apvector but 
    //I dont know how to do something like that
    
    if( *****  == goal)  
      return true;
    else 
    
         
    
    }

  2. #2
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186
    try this example for adding elements of apvector:

    Code:
    bool IsPossible(const apvector <int> &A, int goal)
    {
      
        A.resize(20);  // resize comes with apvector, the 20 can replaced by how large you want the array to be
        
        cout << "Enter number 1: ";
        cin >> A[0];   
        cout << endl;
    
        cout << "Enter number 2: ";
        cin >> A[1];
        cout << endl;
    
        // To add elements of an array;
        A[0] + A[1] = A[2];
        
        // You can also do something like this
        int Num;
        A[0] + A[1] = Num;
    
    /*
        if( *****  == goal)  
        return true;
        else 
    */
         
    
    }
    the best things in life are simple.

  3. #3
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    You can't do this..

    A[0] + A[1] = A[2];
    A[0] + A[1] = Num;

    You cannot assign to a temporary variable. The result of A[0] + A[1] is a tempoarry. Perhaps you wanted the reverse?
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Learning Merge Sort Technique
    By JoshR in forum C++ Programming
    Replies: 3
    Last Post: 04-12-2005, 04:48 PM
  2. apvector...Arrays
    By JoshR in forum C++ Programming
    Replies: 2
    Last Post: 03-25-2005, 11:07 PM