Hey guys, I'm wondering if anyone knows how to write a function that draws a hollow triangle pointing to the right made up of asterisks. The horizontal width of the triangle is passed as a parameter. The statement triangle(4); would display a triangle 4 stars wide:

* 
     **
     * * 
     *  *
     * *
     **
     *

The statement triangle(7); would display a triangle 7 stars wide:

*
     ** 
     * *
     *  * 
     *   *
     *    *
     *     *
     *    *
     *   *
     *  * 
     * *
     **
     *

The statement triangle(1); would print a single star

*

The statement triangle(2); would print

*
       **
       *

An argument less than one simply prints a blank line.

I have no idea how to do this... and any help ya'll could give would be appreciated :) -

It's for a open-book homework assignment, and I'm not understanding it.

void triangle(int n)
{
   
}

Edited 5 Years Ago by Nick Evan: Added CODE-tags, to preserve formatting

Hint: you need a for loop or two (one for each of the lines, and one for the spaces, which vary based on how many lines down you are).

How would you do it on paper (IOW, how did you generate the above patterns?) Think carefully about your decisions when and how to use spaces and stars. Look at the patterns and using jonsca's hint you can translate your pattern into code.

What type of triangle do you want to draw? There are many triangles in geometry. You can draw a triangle based on its size, side and angles.

The formula used for a triangle is:

A=bh.

Comments
It helps to read the thread before asking stupid questions...
This article has been dead for over six months. Start a new discussion instead.