Help needed urgently

Can anyone help me write a program that will promt the user to input a number,n,and generate a "hallow pyramid of asterisks with a height of n(any number from 1 to 100).The program is required to make a meaningful use of atleast one function and the entire program must not be more than 60 lines of code.
I would realy appreciate it if someone helps me
Last edited on
Hmm...

Should be relatively simple...

Basic program structure is;

1. User input
2. Error checking (if desired)
3. A simple loop to print out each line of a pyramid

Two refinements to make re-running the program easier could be (a) enclosing 1-3 in a loop; and/or (b) allowing entry of the pyramid height as a command line parameter.

I'm not going to worry about error checking or the refinements. And as user input via "cout" and "cin" is pretty simple, the major bulk of the work centres on the algorithm to display the pyramid.

Let's look at same for a SOLID pyramid...

The height of the pyramid is given by the user (int height;)

Let "line_no" be an integer representing the line number.

All we want is a loop to print lines each consisting of;

1. enough spaces to make the pyramid symmetrical
2. the correct number of asterix
3. a line return

Calculating the number of spaces and asterix is the tricky bit;

Consider a specific example: a SOLID five line pyramid (int height = 5), lines numbered from 0 to (height-1)

line_no 1 : 4 spaces; 1 asterix
line_no 2 : 3 spaces; 3 asterix
line_no 3 : 2 spaces; 5 asterix
line_no 4 : 1 spaces; 7 asterix
line_no 5 : 0 spaces; 9 asterix

or, as a general rule for a pyramid of "height" each line contains;

(height - line_no) spaces
(2*line_no + 1) asterix


All we need now is a loop to print out our pyramid;
for (;;)
{
print out (height-line_no) spaces
print out (2*line_no - 1) asterix
print out a new line
}

printing the spaces would seem an ideal opportunity to introduce a simple function call as stipulated in your requirements.


As stated this prints out a SOLID pyramid - but the basic concept can easily be extended to a HOLLOW pyramid - although it will require a little bit of thought. I'd suggest you sit down with a sheet of paper, draw out a few hollow pyramids of, say, 5, 7 and 9 lines and think about what patterns you see (Hints: a. one and two line pyramids may need to be special cases; b. treat the base separately from the rest). I'm not, however, going to do the work on this as I don't want to rob you of the fun!

PS: I wrote the code to generate a hollow pyramid in 57 lines.

Kindest regards,
muzhogg


Last edited on
Topic archived. No new replies allowed.