Thread: Combination of point coordinates (recursion help !)

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jul 2019
    Posts
    13

    Combination of point coordinates (recursion help !)

    Hello
    I could not understand below recursion solution for a combination of upper limit and lower limit point coordinates. How does it progress after print first (1 2 3 4) ? How does ff call stacks work?

    Code:
    #include <iostream>
    #include <iomanip>
    #include <vector>
    using namespace std;
    
    // pre: a.size() == b.size()
    void ff(const vector<int>& a, const vector<int>& b, vector<int>& c) {
        if (c.size() == a.size()) {
            for (int x: c) cout << setw(4) << x << ' ';
            cout << '\n';
        }
        else {
            c.push_back(a[c.size()]);  ff(a, b, c);  c.pop_back();
            c.push_back(b[c.size()]);  ff(a, b, c);  c.pop_back();
        }
    }
    
    void f(const vector<int>& a, const vector<int>& b) {
        vector<int> c;
        ff(a, b, c);
    }
    
    int main() {
        vector<int> a{1, 2, 3, 4}, b{10, 20, 30, 40};
        f(a, b);
    Last edited by scorp08; 12-26-2020 at 09:15 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-27-2011, 04:34 AM
  2. Need help with floating point values
    By 69gto96z in forum C++ Programming
    Replies: 6
    Last Post: 07-18-2008, 07:01 AM
  3. values into array based on coordinates
    By BabyWasabi in forum C++ Programming
    Replies: 1
    Last Post: 12-13-2006, 07:48 PM
  4. floating point values
    By jmarsh56 in forum C++ Programming
    Replies: 5
    Last Post: 12-28-2005, 02:28 PM
  5. floating point values
    By jmarsh56 in forum C++ Programming
    Replies: 1
    Last Post: 12-14-2005, 10:30 AM

Tags for this Thread