Hi..

please help me with this problem
I want to output stars in this shape:

    *
   * *
  * * *
 * * * * 
  * * *
   * *
    *

just give me the idea to do this in an recursive function ??
i tried but its not showing me the right output..

this is my code:

#include<iostream>

using namespace std;

//function proto Type
void generateStars( int numofLns )
{

    if( numofLns==0 )
        return;
    else

        for( int i=1; i<=numofLns; i++ )
            for( int j=i; j<=0; j--)
            {
                generateStars(i-1);
                cout<<" * ";
            }

    cout<<endl;

}


int main()
{

    int numOflines;//number of lines in the pattern

    cout<<"\nPlease enter the number of lines of the stars:\n";
    cin>>numOflines;

    //Calling the recursive function to generates the stars
    generateStars(numOflines);

    return 0;

}//end of main

please help..

#include<iostream>

using namespace std;

//function proto Type
void generateStars( int numofLns )
{

if( numofLns==0 )
return;
else

for( int i=1; i<=numofLns; i++ )
for( int j=i; j<=0; j--)
{
generateStars(i-1);
cout<<" * ";
}

cout<<endl;

}


int main()
{

int numOflines;//number of lines in the pattern

cout<<"\nPlease enter the number of lines of the stars:\n";
cin>>numOflines;

//Calling the recursive function to generates the stars
generateStars(numOflines);

return 0;

}//end of main

this is the code..

#include<iostream>

using namespace std;

//function proto Type
void generateStars( int numofLns )
{

    if( numofLns==0 )
         return;
    else
         for( int i=1; i<=numofLns; i++ )
              for( int j=i; j<=0; j--)
              {
                    generateStars(i-1);
                    cout<<" * ";
              }
             
        cout<<endl;
   
}


int main()
{

      int numOflines;//number of lines in the pattern

      cout<<"\nPlease enter the number of lines of the stars:\n";
      cin>>numOflines;

      //Calling the recursive function to generates the stars
      generateStars(numOflines);

      return 0;

}//end of main

Well a start would be to change the <= on line 13 to >=. But then you still get weird output.

I'm not sure how exactly you want to break this problem down recursively. You could have one function for each line, as you're doing, but then you need to handle increasing the length and then decreasing it. You could also just have a recursive function to print some given number of asterisks, but that's very easy.

My suggestion would be to try writing this function:

void printUntil(int current, int until)

You could initially call printUntil(1, 5), for example, and it would print a line with one, then two, then three, etc. until five asterisks. As soon as it hit five, it would recurse with printUntil(0, 4), which would print four, then three, etc until printUntil(0, 0) is a special case that returns immediately to terminate the recursion.

If you don't like that function then think of another way to do it. But you have to somehow represent the increasing length and then the decreasing length.

i did not understand your idea
please explain..

Ok..

I tried it again,
this is my code:

#include <iostream>  

   

using namespace std;  

   

void star(int x)
{   

    if (x > 0) 
	{  

        cout << " *";  

        star(x-1);  

    }
	else
	{  

        cout << endl;  

    }  

}  

void space(int y)
{   

    if (y < 0) 
	{  

        cout << " ";  

        space(y+1);  

    }
	else
	{  

        cout << endl;  

    }  

}  

    
   

int main() 
{  
	int numOflns;

	cout<<"Printing a rectangular up and down using recursive function..\n\n";

	cout<<"Enter number of lines:\n";
	cin>>numOflns;

	//printing the up rectangle
	for(int i=1; i<=numOflns; i++ )
		star(i);
	//printing the down rectangle
	for( i=numOflns-1; i>=0; i--)	
		star(i);

	cout<<endl;

     return 0;  
 
}

Now, the problem is the shape.. I want it like this:

*
   * *
  * * *
 * * * *
  * * * 
   * * 
    *

but it apeare like this:

* 
 * *
 * * *
 * * * *
 * * *
 * * 
 *

I dont now how to use space function and if it is correct or not
please help..

Well with your current space() function you should be able to call space((i-numOflns)*2) or some such before each call to star(). That should print the required number of spaces.

I'm not sure why your space() function expects a negative number as input, but there you have it.

I corrected my space() function by change the < to >, and + to -
but the problem in main()..

I tried to output using this statement:

for(int i=1; i<=numOflns; i++ )
	{
		cout<<space(i)<<star(i);
	}

and also this way also:

for(int j=numOflns-1; j>0; j--)
	{
		space(j);
		for(int i=numOflns-1; i>=0; i--)	
			star(i);
	}

also this show wrong output..

I can overload insertion operator<<() but in this case i have to include class which i dont want.. dont we have any different ways???

for(int i=1; i<=numOflns; i++ )
	{
		cout<<space(i)<<star(i);
	}

This doesn't make sense. You want to print a lot of spaces on the first line, and then fewer spaces on successive lines; you want to print just one star on the first line, and then more stars on successive lines.

It would make sense if you passed some argument like (numOflins-i) to space and i to star -- for the top part of the diagram, and then the lower part of the diagram would be fairly straightforward to figure out from there.

Draw it out on paper, or in Notepad. Count how many spaces you want on each line, and how many stars. Figure out the relationship between the counts on different lines. Then write a loop which prints the correct line for each line number i. Try it, see what happens.

it's work and print the right number of spaces BUT not in the same line..
it's give me this output:

---
*
--
**
-
***

****
-
***
--
**
---
*
----

what can i do??

Well, how about not printing a newline after the spaces? . . . . (i.e. remove the cout<<endl line from space().)

it's work!!!!

thaaaaaaaaaaaaaaanks alot..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.