Pyramid

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>

int main()
{
int input_rows, rows, output_spaces;

std::cout << "Enter the number of rows: ";
std::cin >> input_rows;

output_spaces = input_rows;

for (rows=1; rows<=input_rows; rows++)
{
    for (int space=1; space<= output_spaces; space++)
    {
        std::cout << " ";
     
        output_spaces--;
        
        for (space=1;space<= 2*rows -1; space++)
        {
            std::cout << "*" << std::endl;
        }
       
    }    
    
    
}


return 0;
}


This should print out
*
* *
* * *
it should print out one row for how many ever the user inputs.

What is wrong?
Last edited on
hi hunterb24! actually u shud have the for loop like this...
1
2
3
4
5
6
for(int i=1;i<=n;i++) //i decides the number of lines and n is the input from the user
{
  for(int j=1;j<=i;j++)
  cout<<"* ";
  cout<<endl;
}

if u include the above loop, with changes ofcourse according to ur program, u wud get this O/P(depends upon value of n according to my program. the below O/p is for n=4):
*
* *
* * *
* * * *
Last edited on
hey man... first take out that
std::cout << " ";
statement as its not required to give spaces with loops separately... include a space in
std::cout << "*" << std::endl;
by giving a space bar aftr the *..
for (space=1;space<= 2*rows -1; space++)
- change it with space=2 and space<=2*rows... u wud get a O/P like this :
*
*  *  *
*  *  *  *  *
*  *  *  *  *  *  *

u can do changes and modify to ur use!
Last edited on
how can i align the whole triangle to the center to create an equilateral triangle?

such as :
*
* * *
* * * * *

except centered
Last edited on
i am trying to take this slow and go from an easy program to a slightly more difficult one, as my final product will be:
1
2
3
4
5
   
         1
      2 1 2
   3 2 1 2 3
4 3 2 1 2 3 4


which needs to be centered too with as many rows as the user specifies.

Thanks so much for your all your help.
Last edited on
ok so u want it to be centers right? its actually very simple... i expect u wud know about the setw() function. no big changes to our program, just one line to be added to make it centred! first #include <iomanip.h>. actually i insist u not to use THREE for loops for this pattern printing... leads to a lot of confusion... so just try to chnge ur program like how i posted, the very first post of mine... have the control variables in terms of i and j... that reduces time of typing the variable names and doesnt have any special characters or confusion between uppercase or lowercase(hope u understood what i blabbered here :P )... well am gonna post my code, try changing the control variables to ur program or BETTER u follow the method i do cause its a lot more easier than ur way of thinking. its too simple just add very little things...
1
2
3
4
5
6
7
for(int i=1,x=n-1;i<=2*n;i+=2,x--)
{
  cout<<setw(x);
  for(int j=1;j<=i;j++)
  cout<<"* ";
  cout<<endl;
}

the bold highlighted ones are the little changes to be added so as to print the pattern like a equi-triangle...
*
***
*****
*******
Last edited on
Topic archived. No new replies allowed.