• C++ Programming for Financial Engineering
    Highly recommended by thousands of MFE students. Covers essential C++ topics with applications to financial engineering. Learn more Join!
    Python for Finance with Intro to Data Science
    Gain practical understanding of Python to read, understand, and write professional Python code for your first day on the job. Learn more Join!
    An Intuition-Based Options Primer for FE
    Ideal for entry level positions interviews and graduate studies, specializing in options trading arbitrage and options valuation models. Learn more Join!

C++ Possible combinations help

Joined
4/18/12
Messages
67
Points
118
Hey guys. I am doing a project in school. I have three variables: C1, C2, C3 which add to 48. I need to find all the possible combinations that fit that equality and run them all through a function. Is there any easy way to do this? If not does anybody have an idea about how the loop would look, to generate these combinations? Thanks! APalley Daniel Duffy
 
assuming all are >= 0; (edit: and they are integers)

By brut force u need two loops:
k=1;
for i=0 to 48{
for j= i to 48{
a1[k]=i;
a2[k]=j-i;
a3[k]=48-j;
k++
}
}
 
Hey guys. I am doing a project in school. I have three variables: C1, C2, C3 which add to 48. I need to find all the possible combinations that fit that equality and run them all through a function. Is there any easy way to do this? If not does anybody have an idea about how the loop would look, to generate these combinations? Thanks! APalley Daniel Duffy
Can you explain a bit more, using mathematical and even an example (4 instead of 48) to tell us what you have in mind?
thanks
D
 
Can you explain a bit more, using mathematical and even an example (4 instead of 48) to tell us what you have in mind?
thanks
D
So basically we have a queueing problem with 3 queues. We want to see what combination of the number of servers will yield the lowest waiting time among jobs. So we have C1 , C2 , and C3. Those represent the number of servers for each queue. They most total 48. So we want to generate the possible combinations of the # of servers, then run each through an objective function, and take the lowest yield. So we need all combinations of C1, C2, C3 such that C1+C2+C3 = 48.
 
Is it a maths issue (alone) and/or write a system with producers and consumers of data?
 
Thinking of the top of my head

A+B +C = 48

Make an outer loop i (i = 0; i <= 48; i++) for A; then there will be an inner loop where B and C fight it out for 48 - i (for each i). These loops will get smaller... I suppose it's a matter of writing a loop and see what happens.

Is the output a set of tuples<A,B,C> ; is [0,0,48] == [48,0,0] = [0,48,0] ?

It feels as if the algo could be done as a recursive function?(?)
 
Personally I would go for a recursive algo only if it improves the run time.

But in this case there will be O(n^2) tuples and as a result one can not improve the run time to anything < O(n^2) (which is the run time order of the BF approach).
 
C++:
// Jamie.cpp
//
// DJD
//
#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_io.hpp>
#include <string>
#include <list>
#include <iostream>
 
int main()
{
               
    typedef boost::tuple<int, int, int>  Tuple;
    std::list<Tuple> result;
 
    int N = 4;
    int a, b, c;
    Tuple t;
 
    for (std::size_t i=0; i <= N; i++)
    {
        a = i;
        for (std::size_t j=0; j < N-i+1; j++)
        {
            b = j;
            c = N - i - j;
 
            // Update current tuple and add to list
            t.get<0>() = a;
            t.get<1>() = b;
            t.get<2>() = c;
            result.push_back(t);
        }
    }
 
    std::cout << "Size of set, type ANY key to continue: " << result.size() << std::endl;
    int kk; std::cin >> kk;
 
    std::list<Tuple>::const_iterator it;
    for (it = result.begin(); it != result.end(); ++it)
    {
        std::cout << (*it) << std::endl;
    }
 
    return 0;
}
Jamie,
H :) ere is a solution in C++ and Boost (not needed if you have C++ 11).
 
C++:
// JamieII.cpp
//
// C++ 11 solution variant
//
// DJD
//
#include <tuple>
#include <string>
#include <list>
#include <iostream>
 
int main()
{
                 
    typedef std::tuple<int, int, int>  Tuple;
    std::list<Tuple> result;
 
    int N = 5;
    int a, b, c;
    Tuple t;
 
    for (std::size_t i=0; i <= N; i++)
    {
        a = i;
        for (std::size_t j=0; j < N-i+1; j++)
        {
            b = j;
            c = N - i - j;
 
            // Update current tuple and add to list
            std::get<0>(t) = a;
            std::get<1>(t) = b;
            std::get<2>(t) = c;
            result.push_back(t);
        }
    }
 
    std::cout << "Size of set, type ANY key to continue: " << result.size() << std::endl;
    int kk; std::cin >> kk;
 
      for (auto it = result.begin(); it != result.end(); ++it)
    {
        // STL has no tuple cout :-)
        std::cout << "[" << std::get<0>(*it) << "," << std::get<1>(*it) << "," << std::get<2>(*it) << "]\n";
    }
 
    return 0;
}
Version 2 C++ 11
 
C++:
// JamieII.cpp
//
// C++ 11 solution variant
//
// DJD
//
#include <tuple>
#include <string>
#include <list>
#include <iostream>
 
int main()
{
               
    typedef std::tuple<int, int, int>  Tuple;
    std::list<Tuple> result;
 
    int N = 5;
    int a, b, c;
    Tuple t;
 
    for (std::size_t i=0; i <= N; i++)
    {
        a = i;
        for (std::size_t j=0; j < N-i+1; j++)
        {
            b = j;
            c = N - i - j;
 
            // Update current tuple and add to list
            std::get<0>(t) = a;
            std::get<1>(t) = b;
            std::get<2>(t) = c;
            result.push_back(t);
        }
    }
 
    std::cout << "Size of set, type ANY key to continue: " << result.size() << std::endl;
    int kk; std::cin >> kk;
 
      for (auto it = result.begin(); it != result.end(); ++it)
    {
        // STL has no tuple cout :-)
        std::cout << "[" << std::get<0>(*it) << "," << std::get<1>(*it) << "," << std::get<2>(*it) << "]\n";
    }
 
    return 0;
}
Version 2 C++ 11
Wow.... Thank you so much. I will write some code tonight and I will let you know it goes!
 
Back
Top