Hi i'm writing this program where the user chooses whether to print out a square, a forward triangle, and a backwards triangle using the "*" character. I got the square and the backwards triangle to work and the forward triangle, but i cant figure out how to do the back wards one.
*
**
***
**** is how the foward triangle looks when the user enters the size of 4 the backwards one should look like

*
**
***
**** when the user enters 4 for the size. my code for the normal triangle is below(for some reason this wont show up how its suppose to but it should look the same as the normal triangle just slanting to the left instead of to the right.)

cout << "enter the size of your triangle." << endl;
cin >> size;

for(line = 1;line <= size; line++)
{
stars = line;
for(loop = 1;loop <= stars;loop++)
{
cout << "*";
}
cout << endl;
}

If anyone could help me to print out the backwards triangle with just if -else statements and loops that would be great.

I'm assuming you really want the backwards triangle to look like:

*
  **
 ***
****

(ignoring the line numbers, of course.)
So consider what will be on each line. The first must have blanks, up to the point where you want the star. The next line has one less blank than that, and the third one less....

In terms of loops, you'll need a loop that prints blank spaces, the number of which are related to the current line, followed by a loop that prints the stars, again related to the current line.

Think on that a while.

Hi i'm writing this program where the user chooses whether to print out a square, a forward triangle, and a backwards triangle using the "*" character. I got the square and the backwards triangle to work and the forward triangle, but i cant figure out how to do the back wards one.
*
**
***
**** is how the foward triangle looks when the user enters the size of 4 the backwards one should look like

*
**
***
**** when the user enters 4 for the size. my code for the normal triangle is below(for some reason this wont show up how its suppose to but it should look the same as the normal triangle just slanting to the left instead of to the right.)

cout << "enter the size of your triangle." << endl;
cin >> size;

for(line = 1;line <= size; line++)
{
stars = line;
for(loop = 1;loop <= stars;loop++)
{
cout << "*";
}
cout << endl;
}

If anyone could help me to print out the backwards triangle with just if -else statements and loops that would be great.

The forward and backward triangles look the same to me. Perhaps you mean the backward one should look like this:
****
***
**
*

in any case, the code is simple. In the easiest version to code, you should have two loops. One will count down from your size to 1, one will count up from 1 to your size. An if statement will decide which loop to run. If you want to get more sophisticated, you can have 1 loop that has a starting value, and ending value, and an increment value. An if statement would choose the values for this loop. To count up, the starting value would be 1, ending would be size, and the increment would be 1. To count down, the starting value would be size, the ending value would be1, and the increment would be -1.

Ok I've been tryin to figure this out for about two hours now and i cant seem to get the spaces right to print out a reverse triangle, which is the one vmanes posted earlier.

i keep getting the foward triangle with screwed up spacing, is there any way i can modify the code i posted earlier to make the reverse triangle.

The easy way is to use the setw method or the cout.width method.
Take a look :

void printChars(const char whatToPrint, const size_t howMany){
	for(int i = 0; i < howMany; ++i)
		cout << whatToPrint;
}
void printTri(const size_t height){	
	for(int i = 0; i < height; ++i){
		std::cout.width(height-i);
		printChars('*',(i+1));
		cout << endl;
	}
}

But go ahead and try it without setw. Just make sure that enough
spaces are present at each height. Actually, this code here :

std::cout.width(height-i);

should be a hint on how
to print the spaces.

Edited 6 Years Ago by firstPerson: n/a

This article has been dead for over six months. Start a new discussion instead.