### recursive

Pages: 12
could someone explain how to do the recursive portion on this

 ``12345678910111213141516171819202122`` ``````#include #include #include // for min, max using namespace std; void show_1_to_n_loop(int n) { // looping PROVIDED for (int i=1; i<=n; ++i) cout<=1; --i) cout<
Given that they are two-line (at most) answers I think you should have a go at them first.

The majority of your posts seem to be dumping homework.
Do you understand what is a recursive function?

See https://www.learncpp.com/cpp-tutorial/recursion/

 ``123456789101112131415161718192021222324252627282930313233343536`` ``````#include #include #include // for min, max using namespace std; // Constraints: No loops allowed; no static local or global variables. // Your new code goes here; modify... static void show_1_to_n_recurse(int n, int min) { // recursive if(min>n) return; else { show_1_to_n_recurse(min+1,n); } } // Constraints: No loops allowed; no static local or global variables. // Your new code goes here; modify... static void show_n_to_1_recurse(int n) { // recursive { if(n==1) { return; } else { show_n_to_1_recurse(n-1); //Recursive call } } }``````
Last edited on
In your recurse functions, where's the cout to display the numbers?
 ``123456789`` ``````int main () { cout<<"start...\n"; show_test(1, "show_1_to_n", looping); show_1_to_n_loop(15); show_test(1, "show_1_to_n", looping); show_1_to_n_loop(-9); // handle unexpected values show_test(1, "show_1_to_n", recursive); show_1_to_n_recurse(15); show_test(1, "show_1_to_n", recursive); show_1_to_n_recurse(-9); // avoid runaway recursion cout<
IN YOUR RECURSIVE FUNCTIONS - NOT IN int main() - where's the cout to display the numbers?
sorry I get what they're saying now. I think I have it wrong but I am reading more about recursive functions right now I will post an updated code soon

if I can't return a value how do I stop it from infinitely running?
 ``12345678910`` ``````void show_1_to_n_recurse(int n) { // recursive if (n==0) { cout <<" "; } else{ cout <
Last edited on
You stop it from running infinitely by not calling the function recursively.

But you need to recurse up to 1 before you start printing, so you need to switch lines 7 and 8.
 if I can't return a value how do I stop it from infinitely running?
`if (n<=0)`
It won't recurse any further once n gets to 0 (or below if it was originally called with a negative number). I presume that is what is intended, and that you aren't expected to count through negative numbers also?
what do I do to show the 1 to -9?

still working on the show_n_to_1 (L41-L55) but I am having an issue where it's only printing the first number

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576`` ``````#include #include #include // for min, max using namespace std; enum control_flow_t {functional, looping, recursive}; void show_test(int n, string s, control_flow_t control_flow) { // utility function to format test output // n: test number; s: "description"; control_flow: functional, looping or recursive static const string fx="functional", sl="looping", sr="recursive"; int max_len=max(fx.size(), max(sl.size(), sr.size())); string msg; switch (control_flow) { case functional: msg=fx; break; case looping: msg=sl; break; case recursive: msg=sr; break; default: msg="??"; break; } char iorr=msg[0]; msg=" ("+msg+"): "; cout<<"\n"<=1; --i) cout<=0) { cout <<""; } else{ show_1_to_n_recurse(n+1); cout <

 start... 1l) show_1_to_n (looping): 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1l) show_1_to_n (looping): 1r) show_1_to_n (recursive): 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1r) show_1_to_n (recursive): 2l) show_n_to_1 (looping): 11 10 9 8 7 6 5 4 3 2 1 2l) show_n_to_1 (looping): 2r) show_n_to_1 (recursive): 2r) show_n_to_1 (recursive): -5 Program ended with exit code: 0
Last edited on
L53 is wrong. This is not a recursive call as per L47.
ahh I see silly mistake

how do I create a loop so it prints out both? this is my current output
 start... 1l) show_1_to_n (looping): 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1l) show_1_to_n (looping): 1r) show_1_to_n (recursive): 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1r) show_1_to_n (recursive): 2l) show_n_to_1 (looping): 11 10 9 8 7 6 5 4 3 2 1 2l) show_n_to_1 (looping): 2r) show_n_to_1 (recursive): 2r) show_n_to_1 (recursive): -5 -4 -3 -2 -1
Last edited on
What's your current code for show_n_to_1_recurse()?

What's your current param to the function in L53?
this is my current code
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980`` ``````#include #include #include // for min, max using namespace std; // ===========1 TO n================== void show_1_to_n_loop(int n) { // 1 to n, looping, PROVIDED, DO NOT CHANGE for (int i=1; i<=n; ++i) cout<=1; --i) cout<=0) { cout <<""; } else{ cout <
L36 - are you sure about n + 1 for show_n_to_1() ??? Think about it...
running out of ideas on what I could changes besides show_n_to_1_recurse(n-1);

I also have a question about using 0's because I think it's what's preventing me from printing the negative numbers?
show_n_to_1_recurse(-5);, show_1_to_n_recurse(-9);

What else can I use besides 0's

 ``1234567891011`` ``````void show_n_to_1_recurse(int n) { // recursive if (n>=0) { cout <<""; } else{ // cout <
eh, show n to 1? Recursion takes a while to get a feel for. I will show you, in hopes you will study it and see what you were missing.
consider this:
 ``12345678910`` ``````show_n_to_1_recurse(int n) { if(n) { cout << n << " "; show_n_to_1_recurse(n - 1); } } ``````

that works for positive numbers. Now how would you change it to handle both + and -?
adding a negative number is subtraction and all that math stuff.
so, instead of n-1, it should be n- something : either 1 or -1.
boolean expressions give 1 and 0 for true/false.
anything to the zeroth power is 1.
so..
show_n_to_1_recurse(n - pow(-1, (n<0))); perhaps? Its horribly inefficient, but for now, let that slide.
Last edited on
> using 0's because I think it's what's preventing me from printing the negative numbers?
> What else can I use besides 0's

Often, addressing the general case is simpler than grappling with each special case.

 ``12345678910111213141516171819202122232425262728`` ``````#include // the general case: print all integers from a to b, inclusive void show_a_to_b( int a, int b ) { std::cout << a << ' ' ; // print a if( a == b ) std::cout << '\n' ; // if a == b, we are done // otherwise, if ab */ print all numbers from a-1 to b (descending) else show_a_to_b( a + ( a

http://coliru.stacked-crooked.com/a/17ef27899cee1fac
Pages: 12