recursive

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <iomanip>
#include <algorithm> // for min, max
using namespace std;

void show_1_to_n_loop(int n) { // looping PROVIDED
  for (int i=1; i<=n; ++i) cout<<i<<" ";
}

void show_1_to_n_recurse(int n) { // recursive
  // Constraints: No loops allowed; no static local or global variables.
  
}

void show_n_to_1_loop(int n) { // looping, PROVIDED
  for (int i=n; i>=1; --i) cout<<i<<" ";
}

void show_n_to_1_recurse(int n) { // recursive
  // Constraints: No loops allowed; no static local or global variables.

}
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/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <iomanip>
#include <algorithm> // 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?
1
2
3
4
5
6
7
8
9
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<<endl;
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?
1
2
3
4
5
6
7
8
9
10
void show_1_to_n_recurse(int n) { // recursive
    if (n==0)
    {
        cout <<" ";
    }
    else{
        cout <<n<<" ";
        show_1_to_n_recurse(n-1);
    }
}
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <iomanip>
#include <algorithm> // 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"<<n<<iorr<<") "<<s<<setw(max_len+5)<<left<<msg;
}

//======================

void show_1_to_n_loop(int n) { // 1 to n, looping
  for (int i=1; i<=n; ++i) cout<<i<<" ";
}

// Constraints: No loops allowed; no static local or global variables.
void show_1_to_n_recurse(int n) { // 1 to n, recursive
    if (n<=0) // won't recurse any further once n gets to 0, no negative numbers
    {
        cout <<"";
    }
    else{
        show_1_to_n_recurse(n-1);
        cout <<n<<" ";
    }
}

void show_n_to_1_loop(int n) { // looping
    for (int i=n; i>=1; --i) cout<<i<<" ";
}

// Constraints: No loops allowed; no static local or global variables.
void show_n_to_1_recurse(int n) { // recursive
    if (n>=0)
    {
        cout <<"";
    }
    else{
        show_1_to_n_recurse(n+1);
        cout <<n<<" ";
    }
}


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<<endl;
  
  show_test(2, "show_n_to_1", looping);    show_n_to_1_loop(11);
  show_test(2, "show_n_to_1", looping);    show_n_to_1_loop(-5);    // handle unexpected values
  show_test(2, "show_n_to_1", recursive);  show_n_to_1_recurse(11);
  show_test(2, "show_n_to_1", recursive);  show_n_to_1_recurse(-5); // avoid runaway recursion
  cout<<endl;


  return 0;
}


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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <iomanip>
#include <algorithm> // 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<<i<<" ";
}

// Constraints: No loops allowed; no static local or global variables.
void show_1_to_n_recurse(int n) { // 1 to n, recursive
    if (n<=0) // won't recurse any further once n gets to 0, no negative numbers
    {
        cout <<"";
    }
    else{
        show_1_to_n_recurse(n-1);
        cout <<n<<" ";
    }
}

// ===========n TO 1==================
void show_n_to_1_loop(int n) { // looping, PROVIDED, DO NOT CHANGE
    for (int i=n; i>=1; --i) cout<<i<<" ";
}

// Constraints: No loops allowed; no static local or global variables.
void show_n_to_1_recurse(int n) { // recursive
    if (n>=0)
    {
        cout <<"";
    }
    else{
        cout <<n<<" ";
        show_n_to_1_recurse(n+1);
    }
}
    

// ===========MESSAGE LAYOUT==================
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"<<n<<iorr<<") "<<s<<setw(max_len+5)<<left<<msg;
}


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<<endl;
  
  show_test(2, "show_n_to_1", looping);    show_n_to_1_loop(11);
  show_test(2, "show_n_to_1", looping);    show_n_to_1_loop(-5);    // handle unexpected values
  show_test(2, "show_n_to_1", recursive);  show_n_to_1_recurse(11);
  show_test(2, "show_n_to_1", recursive);  show_n_to_1_recurse(-5); // avoid runaway recursion
  cout<<endl;

  cout<<"\n...end\n";
    
  return 0;
} // end of main 
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

1
2
3
4
5
6
7
8
9
10
11
void show_n_to_1_recurse(int n) { // recursive
    if (n>=0)
    {
        cout <<"";
    }
    else{
  //      cout <<n<<" ";
        show_n_to_1_recurse(n-1);
        cout <<n<<" ";
    }
}
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:
1
2
3
4
5
6
7
8
9
10
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>

// 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 a<b print all numbers from a+1 to b (ascending)
    //            else /*if a>b */ print all numbers from a-1 to b (descending)
    else show_a_to_b( a + ( a<b ? 1 : -1 ), b ) ;
}

void show_1_to_n( int n ) { show_a_to_b( 1, n ) ; }

void show_n_to_1( int n ) { show_a_to_b( n, 1 ) ; }

int main() {

    for( int n : { -15, -9, -5, -1, 0, 1, 11, 15 } ) {

        std::cout << "1 to " << n << ": " ;
        show_1_to_n(n) ;
        std::cout << n << " to 1: " ;
        show_n_to_1(n) ;
    }
}

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