how could i make a triangle thatgot spaces/hole at it's centre.. :

for example like this :

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

-->> which the "------" is actually the space...

i've tried working with this code but its not working :( :

#include <iostream>
using namespace std;

int main ()
{
    int a,b;
    
    for (a=1; a<=10; a++)
    {
        for (b=1; b<=10-a; b++){
            cout<<" ";
            }
            for (b=1; b<=2*a-1; b++){
                
                if(b%2==0){
                cout<<"*";
                }else cout<<" ";
                
                }
                cout<<endl;
    }
system("pause");
}

what changes do i need to make this code working like what i desired??


Thank You for helping....

This won't solve your problem I guess. But this was my job interview question.. Here is what I proposed:

cout << "---------------- *" << endl;
cout << "--------------*----*" << endl;
cout << "------------*--------*" << endl;
cout << "----------*------------*" << endl;
cout << "---------***********" << endl;

Of course I didn't get the job.. :D

Create two meaningful variable names, like

int spacesBeforeFirstStar = 11;
int numSpacesBetweenStars = 0;

Then perhaps

for ( rows = 0 ; rows < 5 ; rows++ ) {
  // some stuff
  spacesBeforeFirstStar -= 2;
}

Not meaningless variable names like a and b.

Are you trying something like this?

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

If possible, try to generalize the program for a triangle of order n (where n could be the height).
One method of solving this could be for you to first develop a program for a triangle without spaces like this one

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

After that, you could modify the program to print stars only during starting and ending of loops, otherwise spaces. However, you should keep it in mind to print the entire line of stars in the last iteration for the base of the triangle.

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.