10 Replies - 10957 Views - Last Post: 11 November 2012 - 11:11 AM Rate Topic: -----

#1 Ms.RMASO  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,172
  • 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?
Was This Post Helpful? 0
  • +
  • -

#3 Ms.RMASO  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.

Was This Post Helpful? 0
  • +
  • -

#4 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,172
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 Ms.RMASO  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#6 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,172
  • 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

Was This Post Helpful? 0
  • +
  • -

#7 Ms.RMASO  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 =)
Was This Post Helpful? 0
  • +
  • -

#8 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#9 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,172
  • 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.
Was This Post Helpful? 0
  • +
  • -

#10 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • 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 >
Was This Post Helpful? 0
  • +
  • -

#11 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 660
  • View blog
  • Posts: 2,270
  • Joined: 31-December 10

Re: Inverted triangle and inverted pyramid

Posted 11 November 2012 - 11:11 AM

View PostAdak, 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1