Hello everyone, i have another assignments that i can't deal with my ability yet.
This is the assignment :
Make a pyramid from a character where you can define it's height !
Example : 4 characters height pyramid from "x" character

``````X
XXX
XXXXX``````

6 characters height

``````x
xxx
xxxxx
xxxxxxx
xxxxxxxxx
xxxxxxxxxxx``````

then so on. I have difficulty to solve this assignment. However, this one of my attempt, please give some suggestion.

``````#include <iostream>
using namespace std;
int main (){
int a,z,x;
cout<<"Insert pyramid height  :";cin>>a;
for (int b=a; b>=1; b--){
for (int c=1;c<=b;c++){
z=b-1;
if (c<=z){
cout<<" ";
}
for (int y=a;y>=1;y--){
for (int d=0;d<=a*2-1;d=d*2+1){
x=y+d;
if ((c>=b)&&(c<=x)){
cout<<"x";
}
}
}
}
cout<<endl;
}
return 0;
}``````

## All 13 Replies

A pyramid such as that grows at a rate of (2x - 1), or one less than 2 times x.

``````***X                                        2*1 = 2 -1 = 1
**XXX                                      2*2 = 4 - 1 = 3
*XXXXX                                    2*3 = 6 - 1 = 5
XXXXXXX                                   2*4 = 8 - 1 = 7``````

A possible way to get what you're looking for is to output a blank space " " in front of each row.

And I'm guessing from looking the asterisks that you're going to want to start at one less than the height (so in the previous example, 3) then decrement it until you're left with 0 and output the last line without any spaces.

And since it is an assignment, I would recommend changing your variable names to something meaningful, (ie height, blankSpaces, etc.)

>However, this one of my attempt, please give some suggestion.
The usual pattern is a loop for the rows and two loops inside of it, one for the leading spaces and one for the triangle body:

``````for i = 0 to N
for j = 0 to nspace
print ' '
for j = 0 to nbody
print 'X'``````

From there it's simply a matter of getting the expressions right for calculating nspace and nbody.

Or you can just turn in something that will shut your teacher up, like this:

``````#include <iostream>

int main()
{
for ( int i = 0; i < 32; i++ ) {
for ( int j = 0; j <= i; j++ )
std::cout.put ( ~i & j ? ' ' : '1' );
std::cout.put  ( '\n' );
}
}``````

;)

commented: ran the pyramid code cause i wasn't following it, had a great laugh ty +1

Or you can just turn in something that will shut your teacher up, like this:

``````#include <iostream>

int main()
{
for ( int i = 0; i < 32; i++ ) {
for ( int j = 0; j <= i; j++ )
std::cout.put ( ~i & j ? ' ' : '1' );
std::cout.put  ( '\n' );
}
}``````

;)

That's cool!

:P

hmm if about how to make the blank space i already figured it out...The one that troubled me is how to write the "x" after the blank spaces. How can I do it ?

@narue : i can't try that code of yours >.< I am using MinGw Developer Studio. Can you make the code so it can be executed in MinGw ?

>The one that troubled me is how to write the "x" after the blank spaces. How can I do it ?
That's actually the easier part. You just need a second loop that prints x's and increments by two with each row.

>Can you make the code so it can be executed in MinGw ?
Not really. The code is already conforming to the C++ standard, so unless you're doing something wrong (the most likely case) trying to execute it, tweaks would be necessary to handle implementation quirks. For example, on Dev-C++ I would have to change it to this to keep the execution window open and force cout to flush just for good measure:

``````#include <iostream>

int main()
{
for ( int i = 0; i < 32; i++ ) {
for ( int j = 0; j <= i; j++ )
std::cout.put ( ~i & j ? ' ' : '1' );
std::cout.put  ( '\n' );
}

std::cin.get();
}``````
``````get input height
nunmberOfBlanks = height - 1
numberOfX = 1
level = 1
for 1 to height
for 1 to numberOfBlanks
print " "
endfor
for 1 to numberOfX
print "X"
endfor

numberOfBlanks = numberOfBlanks - 1
level++
numberOfX = 2 * level
endfor``````

I think that would do it

Hmm...thank you for your suggestion, i already found some way^^. But i have difficulty combine this two code
This for make the blank spaces :

``````for ( int b=x;b>=0;b--){
for (int z=1;z<=b;z++){
cout<<"a";
}<<endl
cout
}
and this is for the pyramid
for (int c=1;c<=height*2-1;c=c+2){
for(int d=1;d<=c;d++){
cout<<"x";
}
cout<<endl;
}
``````

somehow they just don't mix together. Can you help me fix it >.< ?

``````for ( int b=x;b>=0;b--)
{
for (int z=1;z<=b;z++)
{
cout<<"a";
}

<<endl
cout
}
// and this is for the pyramid

for (int c=1;c<=height*2-1;c=c+2)
{
for(int d=1;d<=c;d++)
{
cout<<"x";
}
cout<<endl;
}``````

Is the "a" what is meant to be your blank spaces? If so I think you're looking at it the wrong way.

The first line will have (height - 1) blank spaces, because it will be half of the bottom row (excluding the middle X), then decreases by 1 for each new row.

THIS IS UNTESTED BUT SHOULD WORK

``````for (int level=1;level<=height; level--)
{
for(int blanks= height-1; blanks > 0; blanks--)
{
cout << " ";
}
for(int d=0;d < (level*2 -1); d++)
{
cout<<"x";
}
cout << endl;
}``````

Example #1:
number of lines in pyramid = 3
line number 1 has 2 prefix spaces followed by 1 *
line number 2 has 1 prefix spaces followed by 3 *
line number 3 has 0 prefix spaces followed by 5 *

Example #2:
number of lines in pyramid = 4
line number 1 has 3 prefix spaces followed by 1 *
line number 2 has 2 prefix spaces followed by 3 *
line number 3 has 1 prefix spaces followed by 5 *
line number 4 has 0 prefix spaces followed by 7 *

Patterns established in examples:
1) line number increases from 1 to number of lines in pyramid
2) the number of spaces to print on each line is the number of lines in pyramid minus the line number
3) the number of * to print on each line is one less than the line number times 2

Pseudocode:

``````outer loop controls line number
inner loop #1 prints spaces
inner loop #2 prints *``````

Did not Nature give a wonderful explanation about making the pyramid. Did you try what Nature said?
Modify her code just a little to suit your problem.
Its as simple as

``````#include <iostream>

int main()
{
using namespace std;
int n;
cin >> n ;
for ( int i = 1; i <= n; i++ ) {
for ( int j = 2*n-1; j ; j-- )
cout.put ( j>(2*i-1) ? ' ' : 'X' );
cout.put  ( '\n' );
}
}``````

or perhaps this would do

``````#include <iostream>

int main()
{
int n;
std::cin >> n ;
for ( int j=10,i = 1; i <= n; )
std::cout.put ( j==2*n+9?j=10+(i++*0):j++>=(2*(n-i)+10) ? 'X' : ' ' );
}``````
``````#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;

int main (int argc, char * const argv[])
{
int row = 0;
int numbers;

for ( int a = 1; a <= 1000; a++ )
{
numbers[ a - 1 ] = a;
}
cout << "Enter number of pyramid\'s rows:";
cin >> row;

for ( int i = 0; i < row; i++ )
{
for ( int j = 0; j < i + 1; j++ )
{
cout << numbers[ j ];
}

cout << '\n';
}

return 0;
}``````
commented: no tags and a year late -4
commented: Don't bump old threads -1
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.