In order to get more familiar with C++, I have started working on some algebraic problems. Now, I have created an algorithm which generate a combination of numbers, with some constraints, based on an input number:
void abc(const int n) {
std::vector<int> aOut, bOut, cOut; // creating vectors to store values (dynamic int arrays)
for (int a = 9; a <= n - 2; a++) {
for (int b = a + 1; b <= n - 1; b++) {
for (int c = b + 1; c <= n; c++) {
aOut.push_back(a);
bOut.push_back(b);
cOut.push_back(c);
// std::cout << "a = " << a << " b = " << b << " c = " << c << std::endl;
}
}
}
Now, I need to continue working with these vectors, so I need to return them somehow. I have tried to create an int array of the size: int ABC[N][3]
, where const int N = cOut.size();
. This does not work, as N is not accepted as a constant.
I also tried making a counter in the loop where I generate the vectors, which I then transferred to a string which I then transferred to a constant integer - this did not work either.
I tried making a pointer to a constant int, and using it to change the constant in the loop, which did not work either.
I even found a way to calculate the size based on my loops:
const int n = 20;
const int n1 = n - 10; // Manipulating input
const int N = n1*(n1 + 1)*(n1 + 2) / 6; // Size of vectors
And then passing the value to the function as:
void abc(const int n, const int N) { // same code as before }
But nothing works. I am honestly out of ideas (and losing my head). I have browsed trough the forum and google, but without luck. If someone could point me in the right direction, I would be forever grateful.
Of cause the solution includes changing the void
to a function that returns the parameters. I added the void as I would like to check if the values were printed correctly.