Thread: Combination of point coordinates (recursion help !)

  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.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2019
    Posts
    13
    Quote Originally Posted by Salem View Post
    Could not get the answer from original, thats why asked here

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