I wrote this recursive function to print a number sequence from x to n and n to x back.
Here is one of the ideas I had to improve it, but it didn't work out (use another ternary operator at the end of the first one):
return x < n ? printNumberSequence(x + 1, n) : n = 0 x > n && x != n ? printNumberSequence(x - 1, n) : n;
However, I do have a working solution below:
#include <iostream>
using namespace std;
int printNumberSequence(int x, int n)
{ //counts from x until n
cout << x << endl;
x < n ? printNumberSequence(x + 1, n) : n = 0 ;
return x > n && x != n ? printNumberSequence(x - 1, n) : n;
}
int main()
{
return printNumberSequence(0, 5);
}
I want to know if this way to solve the problem is good and how could I improve that code.
n-x
, but fail when that becomes significantly larger (actual values will depend on your compiler and OS). \$\endgroup\$