Recursion problem

Hiya. So I'm supposed to make one of those star diamonds like this:
1
2
3
4
5
6
7
8
9
*
**
***
****
*****
****
***
**
*

but a little different using recursive functions.

Here's the code so far:
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
#include <cstdlib>
#include <iostream>

using namespace std;

void starPut(int, int);

int main(int argc, char *argv[])
{
    starPut(4, 7);
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

void starPut(int a, int b)
{
     int i;
     
     while (a < b) {    //... until it equals b
       if (i = 1, i <= a, i++)
         cout << "*";   //puts one star per value until int a is reached
       else
         cout << endl;  //goes to the next line
         starPut((a + 1), b);   //adds one to a and repeat... (see while declaration)
         }
}


What I want is something that looks like this (using the values I had given starPut in main):
1
2
3
4
****
*****
******
*******


But I instead get infinitely repeating asterisks. So! Any help?
just couldn't resist
1
2
3
4
5
6
7
8
9
10
11
12
13
void starPut(int a, int b)
{
  if(a < ((2 * b) - 1))
  {
    const int c = ((a >= b) ? (((2 * b) - a) - 1) : (a + 1));
    for(int i = 0; i < c; ++i)
    {
      cout << "*";
    }
    cout << endl;
    starPut(a + 1, b);
  }
}

if you starPut(4, 7); it starts at the 5. line. This starPut(0, 7); would start at the first line
Last edited on
1
2
while (a < b)
  //code where no a or b change -> infinite loop 

if (i = 1, i <= a, i++)What do you want to do here? the if is evaluating i++ so will always be true.
The if...else statement was originally a for loop. I didn't change all I needed to. Plus, it looks like I was right the first time. Ah well. I didn't realize it was going to be this complicated; I usually overthink these things.

I actually has starPut accept two integers like that on purpose in case somebody had a burning urge to start it on a different row (the original assignment was to make a whole half diamond, though this isn't a formal homework assignment, so I would put my own twists on their exercises).
I didn't realize it was going to be this complicated; I usually overthink these things.
What? The coder777's code is a little obfuscated.
How about something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void stars(int count=1)
{
     //print count stars;
     
     if (count<5) stars(count+1);
     //add something here,
     //so that the five star line
     //is printed only once

     //print count stars;
}

int main()
{
     stars();
     return 0;
}


* <------------- stars(1)

** <------------ stars(2)

*** <----------- stars(3)

**** <---------- stars(4)

***** <--------- stars(5)

**** <---------- stars(4)

*** <----------- stars(3)

** <------------ stars(2)

* <------------- stars(1)
Sorry, I totally misread the original post...
I thought you wanted the half diamond and ended up with the other thing,
but it turns out you actually wanted that other thing...

Think about it for a while, how would you do this if you were to do it with loops?
It would be something like this, right?

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
#include <iostream>
using namespace std;

void stars_loop(int, int);

int main()
{
    stars_loop(4,7);

    cout << "\nhit enter to quit...\n";
    cin.get();
    return 0;
}

void stars_loop(int a, int b)
{
    for (int i=a; i<=b; i++)
    {
        for (int j=0; j<i; j++)
            cout << '*';

        cout << '\n';
    }
    cout << flush;
}

I'll show you how you can turn a loop into a recursive function,
and then you can use this to turn the above example into what you want.

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
#include <iostream>
using namespace std;

void loop(int a, int b);
void rec_func_wrapper(int a, int b);
void actual_rec_func(int a, int b, int i);

int main()
{
    loop(2,5);
    rec_func_wrapper(2,5);

    cout << "\nhit enter to quit...\n";
    cin.get();
    return 0;
}

void loop(int a, int b)
{
    for (int i=a; i<=b; i++)
        cout << i << ' ';

    cout << endl;
}

void rec_func_wrapper(int a, int b)
{
    actual_rec_func(a,b,a);
}

void actual_rec_func(int a, int b, int i)
{
    if (i>b) return;

    cout << i << ' ';

    actual_rec_func(a,b,i+1);

    if (i==a) cout << endl;
}
Last edited on
Think about it for a while, how would you do this if you were to do it with loops?

Is it just me or did others notice this too? :P
Lol... I didn't do it on purpose...
Wow! That is epic, lol! :D
I didn't notice it no, but it's pretty cool! xD
Topic archived. No new replies allowed.