You have N equal little cubes. You can stack several cubes on top of each other to build a tower. You can also place several towers next to each other to build a ladder. The heights of the towers within a ladder must be strictly increasing. Using 6 cubes you can build a ladder of three towers with heights 1 2 and 3. Overall, there are four ways to build a ladder out of 6 cubes:
6 (a single tower by itself is a ladder)
1 5
2 4
1 2 3
Input 6
Output 4
How to write code so that the variable k was inside the function ?
#include <iostream>
int k = 0;
void NumberOfLadders(int n, int firtsTowerHeight)
{
++k;
for (int i = n + 1; i < (firtsTowerHeight + 1) / 2; ++i)
{
NumberOfLadders(i, firtsTowerHeight - i);
}
}
int main()
{
int n;
std::cin >> n;
NumberOfLadders(0, n);
std::cout << k << "\n";
}
If I were to put "k" anywhere I would put "k"in main and pass it to the function by reference. void NumberOfLadders(int n, int firtsTowerHeight, int& k). But I do not see any use for "k" other than a counter right now.
Your bigger problem is that void NumberOfLadders(int n, int firtsTowerHeight) appears to be a recursive function with no way out, so it will be an endless loop until the stack runs out of memory.