program that displays pyramid of asterisks

#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
int rows, cols, numOfAster;

cout << "Enter the level of of the asterisks:";
cin >> numOfAster;
cout << "Press any key to see the array:\n";
getch();


then what should i do next???
the output should look like a pyramid or a tree in form.
1
2
3
4
5
6
7
Use a nested for loop (for loop inside another for loop). 

In the first loop, control the number of lines.

In the nested loop, control what's on the lines. Use if statements or switch statements to control what to output.

Print a newline after the second loop is complete. 


P.S. Draw an example pyramid to use as a base first (I suggest drawing it as a comment just above your code for easy reference)
Last edited on
*
***
*****
*******
*********
***********
*************
***************


the program output should look like this
so think about it, blue suggested that you you need nested for loops, what if you say to print out a star depending on the current count, of that loop. and a number of spaces depending on the count of the outside loop.... well I said think about it, but really I just solved your problem :P
ah, the mystery of the pyramids! examine the evidence!
each level is made of asterisks and spaces.
they printed spaces, stars, and a newline. but how many spaces and stars?

your example has 8 levels.

how many spaces?

1234567*       = 7
123456***      = 6
12345*****     = 5
1234*******    = 4
123*********   = 3
12***********  = 2
1************* = 1
***************= 0

the number of spaces start at 7 and decrease -1 each level.
7 = numberoflevels - 1. what a surpise! did you see that coming? i didn't.

how many stars?

       1       = 1
      123      = 3
     12345     = 5
    1234567    = 7
   123456789   = 9
  12345678901  = 11
 1234567890123 = 13
123456789012345= 15

the number of stars start at 1 and increase +2 each level. odd numbers!


we can produce the same results!

// our important variables
numberoflevels = 8
spaces = numberoflevels - 1
stars = 1

// begin level loop
print spaces, print stars.

// change stuff
then get decrease the number of spaces and add some stars.
spaces = spaces - 1
stars = stars + 2

// check condition, end loop
repeat printing and changing the spaces and stars until we have done numberoflevels.


that's one way to build a pyramid. another way has to do with aliens. scary.

you know what to do next?
Topic archived. No new replies allowed.