1,105,332 Community Members

Make pyramid

Member Avatar
akira_shinizaki
Newbie Poster
11 posts since Oct 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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;
}
Member Avatar
chococrack
Junior Poster
149 posts since Oct 2008
Reputation Points: 74 [?]
Q&As Helped to Solve: 16 [?]
Skill Endorsements: 0 [?]
 
0
 

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.)

Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 81 [?]
Team Colleague
 
1
 

>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' );
  }
}

;)

Member Avatar
mrboolf
Junior Poster
182 posts since Jun 2008
Reputation Points: 122 [?]
Q&As Helped to Solve: 19 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
akira_shinizaki
Newbie Poster
11 posts since Oct 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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 ?

Member Avatar
akira_shinizaki
Newbie Poster
11 posts since Oct 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

@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 ?

Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 81 [?]
Team Colleague
 
0
 

>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();
}
Member Avatar
chococrack
Junior Poster
149 posts since Oct 2008
Reputation Points: 74 [?]
Q&As Helped to Solve: 16 [?]
Skill Endorsements: 0 [?]
 
0
 
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

Member Avatar
akira_shinizaki
Newbie Poster
11 posts since Oct 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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 >.< ?

Member Avatar
chococrack
Junior Poster
149 posts since Oct 2008
Reputation Points: 74 [?]
Q&As Helped to Solve: 16 [?]
Skill Endorsements: 0 [?]
 
0
 
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;
}
Member Avatar
Lerner
Nearly a Posting Maven
2,416 posts since Jul 2005
Reputation Points: 579 [?]
Q&As Helped to Solve: 407 [?]
Skill Endorsements: 16 [?]
 
0
 

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 *
Member Avatar
Prabakar
Posting Whiz
342 posts since May 2008
Reputation Points: 77 [?]
Q&As Helped to Solve: 33 [?]
Skill Endorsements: 0 [?]
 
0
 

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' : ' ' );
}
Member Avatar
pouyan_objc
Newbie Poster
1 post since Apr 2010
Reputation Points: -5 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-2
 
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;

int main (int argc, char * const argv[]) 
{
	int row = 0;
	int numbers[1001];
	
	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;
}
Member Avatar
__avd
Posting Genius (adatapost)
6,703 posts since Oct 2008
Reputation Points: 1,811 [?]
Q&As Helped to Solve: 1,272 [?]
Skill Endorsements: 61 [?]
Moderator
 
2
 

@pouyan_objc

Do not resurrect old/solved thread. If you want to ask question, start your own thread.

Read member rules : http://www.daniweb.com/forums/faq.php?faq=daniweb_policies

Thread Closed.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article