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.

Replies To: Inverted triangle and inverted pyramid

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?

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.

Re: Inverted triangle and inverted pyramid

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.

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.

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.