# Inverted triangle and inverted pyramid

Page 1 of 1

## 10 Replies - 27842 Views - Last Post: 11 November 2012 - 11:11 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=299546&amp;s=a7893165ba5840715eb7d9c4758a879d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Ms.RMASO

Reputation: 0
• Posts: 5
• Joined: 27-October 12

# Inverted triangle and inverted pyramid

Posted 10 November 2012 - 07:20 AM

i create a program to form a inverted triangle but my prof want it align to the right like this
*****
****
***
**
*
but what i do is like this
****
***
**
*
so, what i should add to my program to create a inverted triangle align to the right?
and a inverted pyramid also. i think there are many missing codes to my program.

i mean my prof want this
******
!!****
!!!***
!!!!**
!!!!!*

i mean my prof want this
******
!!****
!!!***
!!!!**
!!!!!*

Is This A Good Question/Topic? 0

## Replies To: Inverted triangle and inverted pyramid

### #2 aresh

• It's a 16-Bit World!

Reputation: 273
• Posts: 4,258
• Joined: 08-January 12

## Re: Inverted triangle and inverted pyramid

Posted 10 November 2012 - 07:20 AM

Well, the alignment is because of spaces. As you can see, first line has 4 stars and no spaces. The next one has 3 stars and 1 space. The next one has 2 stars and 2 spaces. So, how can you add the spaces?

### #3 Ms.RMASO

Reputation: 0
• Posts: 5
• Joined: 27-October 12

## Re: Inverted triangle and inverted pyramid

Posted 10 November 2012 - 07:22 AM

(THIS IS MY CODE)what shoud i add to become my triangle like that?
```else if(ch==2)
{
cout<<"\nYou selected Inverted Triangle";
cout<<"\nEnter a number: ";
cin>>n;
for(i=1;i<=n;i++)
{

for(j=n;j>=i;j--)

cout<<"* ";
cout<<"\n";
}
}

else if (ch==3)
{
cout<<"\nYou selected Inverted Pyramid";
cout<<"\nEnter a number: ";
cin>>n;
for(k=n;k>=1;k--)
{
for(i=k;i>=1;i--)
cout<<" ";
if(l<n)
{ for(j=1;j<=l;j++)
cout<<"*"<<" ";
}
l++;
cout<<endl;
}
```

This post has been edited by jimblumberg: 10 November 2012 - 08:34 AM
Reason for edit:: Added missing Code Tags, Please learn to use them.

### #4 aresh

• It's a 16-Bit World!

Reputation: 273
• Posts: 4,258
• Joined: 08-January 12

## Re: Inverted triangle and inverted pyramid

Posted 10 November 2012 - 07:24 AM

I did not ask for your code, since I have no need for it. I am not going to add code for you, you have to do it yourself. At least try, because we will not write your code for you, we will just help you with it.

### #5 Ms.RMASO

Reputation: 0
• Posts: 5
• Joined: 27-October 12

## Re: Inverted triangle and inverted pyramid

Posted 10 November 2012 - 07:28 AM

but MR i dont know how i can make fix that thing i try many but even one doesnt work. i dint know what i will put to get those spaces

### #6 aresh

• It's a 16-Bit World!

Reputation: 273
• Posts: 4,258
• Joined: 08-January 12

## Re: Inverted triangle and inverted pyramid

Posted 10 November 2012 - 07:40 AM

Let me explain it to you once again. Inside your outer loop, you have a nested loop which prints the asterisks. Along with that nested loop, you another nested loop, which will print the spaces.
```outer for loop
{
nested for loop to print spaces
{
//code here
}
nested for loop to print asterisks
{
//code here
}
}
```

This post has been edited by aresh: 10 November 2012 - 07:41 AM

### #7 Ms.RMASO

Reputation: 0
• Posts: 5
• Joined: 27-October 12

## Re: Inverted triangle and inverted pyramid

Posted 10 November 2012 - 07:42 AM

ok Thank you MR! for a big help =)

• D.I.C Lover

Reputation: 331
• Posts: 1,168
• Joined: 01-April 11

## Re: Inverted triangle and inverted pyramid

Posted 10 November 2012 - 10:48 AM

Any problem in logic that deals with rows and columns - like your triangle printing, is usually best handled with nested for loops (while loops are OK, but a bit more difficult in practice).

The outer for loop will control the rows, and the inner for loops will control the column printing.

In your prof's triangle, the row variable corresponds to the number of spaces you will print before any *'s are printed.
Maybe your *'s are spaced further apart, but the idea is the same - there is a relationship between these two things.

THAT's what you want to see - that corresponding relationship, between what you have available, and what you need. The row number, becomes a tool for your printing.

```for each row to be printed
for each column to be printed
if(the column is less than or equal to the row value) print a space
else print a *

increment column
end of for each column
print a newline
increment row
end of for each row

```

Typically, these diagrams need tinkering to get them just right, but using terms like the above, you can usually get what you need with simple addition or subtraction, sometimes (rarely) multiplication.

### #9 aresh

• It's a 16-Bit World!

Reputation: 273
• Posts: 4,258
• Joined: 08-January 12

## Re: Inverted triangle and inverted pyramid

Posted 10 November 2012 - 10:51 AM

Actually, you have given her the whole code, albeit in the form of pseudo-code. Try not to do it, because programming is not writing code, it is writing the logic behind the code.

• D.I.C Lover

Reputation: 331
• Posts: 1,168
• Joined: 01-April 11

## Re: Inverted triangle and inverted pyramid

Posted 10 November 2012 - 11:13 AM

I agree, but counterpoint: when you buy a programming book, you see full example programs showing how to solve the problem - and you learn from that, as well. Where would we be if Newton had said "I figured out the laws of basic physics, now you do it as well", instead of publishing those laws?

But I hear ya, and I do it a bit much. When I read a low level of English, I tend to be a bit more generous.

That's my defense, and I'm stickin' to it! < smile >

### #11 vividexstance

• Tiocfaidh ár lá

Reputation: 792
• Posts: 2,873
• Joined: 31-December 10

## Re: Inverted triangle and inverted pyramid

Posted 11 November 2012 - 11:11 AM

Adak, on 10 November 2012 - 02:13 PM, said:

I agree, but counterpoint: when you buy a programming book, you see full example programs showing how to solve the problem - and you learn from that, as well. Where would we be if Newton had said "I figured out the laws of basic physics, now you do it as well", instead of publishing those laws?

But I hear ya, and I do it a bit much. When I read a low level of English, I tend to be a bit more generous.

That's my defense, and I'm stickin' to it! < smile >

Your comparing apples and oranges. Examples are fine, but most people who post for help are posting about a homework problem and when someone just posts a working version, that's not helping the original poster, its just giving them the answer. There's an old Chinese proverb:

Quote

Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime.
We try to help people become better programmers, not just to give them the answer. It's also against the rules to give code when the other person hasn't shown the necessary effort.