Inverse pyramid

I can't figure out how to make the pyramid only showing its outline

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

The code that I have outputs the filled pyramid, any ideas?

1
2
3
4
5
6
7
8
9
10
11
12
13
int a, b, c = 4, d=0;
  
    for(a=1;a<=c;a++){ 
	   for(b=1;b<=c*2-1;b++)  
           if (b<=c+d&&b>=c-d)
               cout<<"*";
           else
               cout<<" ";
               cout<<"\n";
		       d++;
    }
}
    
Last edited on
You can start by having a variable storing the length of the base of the pyramid (Base). Then having two variables 'a' and 'b' start with a = b = Base/2.

Then go in to a loop each time increasing b by 1 and decreasing a by 1. Then you can have for loops within this loop outputting a space " " unless the loop index corresponds to 'a' or 'b'. Finally when 'a' = 0 have it output '*' all the way from 'a' to 'b' inclusive.

Note that you can't have an even base it will always be odd.

OUTLINE

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int iBaseLength = 6;
int a = (int)(iBaseLength/2.0);
int b = (int)(iBaseLength/2.0);

do{

if(a==0){
////////////////output '*'s all from 'a' through 'b'
}
else if(a!=b){
///////////////output '*'s only for position 'a' and 'b'
}
else{
///////////////Here there is only the case 'a'='b' and there will only be one '*'
}

a--; ///////////decrement 'a'
b++; //////////// increment 'b'
}while(a >= 0);


Reply back if you need more help.
Last edited on
Topic archived. No new replies allowed.