Creating Isoceles Triangle

Discussion in 'C' started by aliz_khanz, Apr 4, 2011.

  1. aliz_khanz

    aliz_khanz New Member

    Joined:
    Mar 31, 2011
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    I have to write a programme for an isoceles triangle ( two sides equal ) . The programme should ask the user to give the length of base and height . It should be in * characters. And the length of base is always an odd number. If it is not an odd number , triangle shouldnt be drawn. Any help guys ?

    I am using Visual Studio with C ++ coding.
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Where are you stuck?

    You need to prompt the user (do you know how to write a message to the screen?)
    then get a number (do you know how to do that?)
    twice (once for base and once for height)
    then for each line (do you know about for loops?)
    work out the number of spaces to display (can you figure out how to do that?)
    and the number of asterisks (can you figure it out?)

    and you're done! Simple.

    Have a go and see how far you get. Try not to get stuck; think about it instead. If you are really stuck, post the code you've got and we'll see how to get you unstuck.
     
  3. aliz_khanz

    aliz_khanz New Member

    Joined:
    Mar 31, 2011
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    Thanks so much for your help .... I have made a code and everything is going just fine with only last one problem.... I cannot seem to make the triangle correctly..... it shows up inverted....the top vertex is facing downwards... Can you please solve this problem for me and write the code correctly ?

    here's my code


    Code:
    #include<stdio.h>
    #define Mx 20
    #define My 20
    void draw(int base,int height);
    
    int main()
    {
     //   draw(5,4);
    //    draw(5,4);
    //    draw(15,6);
    	int a,h;
    	char c;
        printf("Enter the base ,height :\n");
    	scanf("%d%d",&a,&h);
    	draw(a,h);
    
    	c=getchar();
    	c=getchar();
    }
    void draw(int base,int height)
    {
         if(base%2==0) {printf("The length of the base its not an odd number \n");return ;}
         int i=0,j;
         int a=i;
         int b=base,t;
    //     (base>height)?t=base:t=height;
         char tab[Mx][My];
         for(j=0;j<height;j++)
          for(i=0;i<base;i++)  tab[j][i]=' ';
          for(j=0;j<height;j++)
          {
          a=j;
          for(i=0;i<base;i++) if(i>=a && i<b ) tab[j][i]='*';
          b--;
          }
         for(j=0;j<height;j++)
         {
         printf("\t\t");
                              for(i=0;i<base;i++)
                              {
                              printf("%c",tab[j][i]);
                              printf(" ");
                              }
         printf("\n");
         }                    
    }
     
    Last edited by a moderator: Apr 8, 2011
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Just run the display loop in the opposite direction, i.e.
    Code:
    for(j=height-1;j>=0;j--)
    
     
  5. aliz_khanz

    aliz_khanz New Member

    Joined:
    Mar 31, 2011
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for that !!! Okay , question ! :)

    now i get the triangle right but the problem now persists is when i type 5 as base and 3 height , it displays a perfect triangle , but , when i type 5 as base and 4 as height , it displays the same triangle while it should say that this is not an odd number as required in the question.
     
  6. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    The question as you've copied it only says the base cannot be even, not the height. However there is a problem in that you don't get the correct height. If you specify 5,6, you get
    Code:
        *
      * * *
    * * * * *
    
    but shouldn't you get something like
    Code:
        *
        *
      * * *
      * * *
    * * * * *
    * * * * *
    
    ?
     
  7. aliz_khanz

    aliz_khanz New Member

    Joined:
    Mar 31, 2011
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    I am sorry I miswrote the height and base thing. you are right with your statement. Only base should be odd number and something is going on with the height as well .... Please help me fix it .

    An example of a triangle of height 3 and length of its base = 9
    Code:
         *
       *****
     *********
    
     
    Last edited by a moderator: Apr 9, 2011
  8. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    http://mathworld.wolfram.com/IsoscelesTriangle.html

    if you know the length of the base and the height then you can calculate
    the length of the equal sides of the triangle.

    check that, the values entered ,are suitable for creating a triangle(the sum of the lengths of any two sides must be greater than the length of the remaining side)
     
  9. aliz_khanz

    aliz_khanz New Member

    Joined:
    Mar 31, 2011
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    sir i really appreciate your help but , allow me to ask , how does it help me with creating the programme ? :)
     
  10. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    if the user enters values that are not suitable for creating a triangle
    then how you can draw it?


    and another thing....
    if i enter base=3 and height=9 what you draw?
     
    Last edited: Apr 10, 2011
  11. aliz_khanz

    aliz_khanz New Member

    Joined:
    Mar 31, 2011
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    Sir , its less and less related with the complexities of mathematics. I understand your concern but please remember that we are drawing particular letters(in this case asterik *) and with the help of that , we are making an isoceles triangle. the limit is not given in the question for base and height.hope you get me ! :)
     
  12. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    if i enter base=3 and height=9 what you draw?

    i know its just an exercise but there must be some restrictions

    for example base length must be greater than the height.
    are you sure that there are not any more restrictions in your exercise that you forgot to mention?
     
  13. aliz_khanz

    aliz_khanz New Member

    Joined:
    Mar 31, 2011
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    Sir I am 110% positive that there are no other restrictions in my question. :)
     
  14. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    you can try this

    Code:
    #include<stdio.h>
    #define Mx 20
    #define My 20
    void draw(int base,int height);
    
    int main()
    {
     //   draw(5,4);
    //    draw(5,4);
    //    draw(15,6);
        int a=2,h=0;
        char c;
        while(a%2==0){
            printf("Enter the base:");
            scanf("%d",&a);
            if (a%2==0)
                printf("only odd length is allowed\n");
            getchar();
        }
        while (h==a || h<2){
           printf("Enter the height :");
           scanf("%d",&h);
           if (h==a)
               printf("\nthe height you entered makes an Equilateral triangle.\n");
           if (h<2)
               printf("\nheight must be greater than 1.\n");
           getchar();
        }
        
        draw(a,h);
        getchar();
    }
    void draw(int base,int height){
         if(base%2==0) {
                printf("The length of the base its not an odd number \n");
                return ;
         }
         int i=0,j;
         int a=i;
         int b=base,t;
    //     (base>height)?t=base:t=height;
    int howmanystars=(base)/(height-1);
    int h=1;
    for (i=0;i<height;i++){
        for (int k=0;k<(base-h+1)/2;k++)
            printf(" ");
        for (j=0;j<h;j++)
            printf("*");
        h=h+howmanystars;
        printf("\n");
    }
                       
    }
    
    
     
    Last edited: Apr 10, 2011
  15. aliz_khanz

    aliz_khanz New Member

    Joined:
    Mar 31, 2011
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    Ma Man , Do I Love you or what ???? :D :D :D Thanks Heaps !!!!!!!
     
  16. aliz_khanz

    aliz_khanz New Member

    Joined:
    Mar 31, 2011
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    just one problem. when i type the legth of base greater than the height , it draws a straight line .......... can you please fix it ?
     
  17. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    what base and height did you give?
     
  18. aliz_khanz

    aliz_khanz New Member

    Joined:
    Mar 31, 2011
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    I placed 11 as base and 7 as height then again 15 as base and 9 as height !
     
  19. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    as i told you before ,what you want isn't easy to accomplish.
    it works only in some cases not for all.
    for example you said base=11 and height=7 can you post the right shape you
    expect to get?

    i made some changes.
    Code:
    #include<stdio.h>
    #define Mx 20
    #define My 20
    void draw(int base,int height);
    
    int main()
    {
     //   draw(5,4);
    //    draw(5,4);
    //    draw(15,6);
        int a=2,h=0;
        char c;
        while(a%2==0){
            printf("Enter the base:");
            scanf("%d",&a);
            if (a%2==0)
                printf("only odd length is allowed\n");
            getchar();
        }
        while (h==a || h<2){
           printf("Enter the height :");
           scanf("%d",&h);
           if (h==a)
               printf("\nthe height you entered makes an Equilateral triangle.\n");
           if (h<2)
               printf("\nheight must be greater than 1.\n");
           getchar();
        }
        
        draw(a,h);
        getchar();
    }
    void draw(int base,int height){
         if(base%2==0) {
                printf("The length of the base its not an odd number \n");
                return ;
         }
         int i=0,j;
         int a=i;
         int b=base,t;
    //     (base>height)?t=base:t=height;
    int howmanystars=(base)/(height-1);
    float stars=height-1;
    stars=(float)(base)/stars-0.5;
    if (howmanystars<stars)
        howmanystars++;
    int h=1;
    for (i=0;i<height;i++){
            if (i==height-1)
            h=base;
        for (int k=0;k<(base-h+1)/2;k++)
            printf(" ");
    
        for (j=0;j<h;j++)
            printf("*");
        h=h+howmanystars;
    
        printf("\n");
    }
                       
    }
    
    
    
    
     
    aliz_khanz likes this.
  20. aliz_khanz

    aliz_khanz New Member

    Joined:
    Mar 31, 2011
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    Symmetry is whats lacking in the above programme. Also , when i Place 7 as base and 11 as height it gave me this


    HTML:
     
    
    
    B 7 
    H 11
    
                       *
                       *
                       *
                       *
                       *
                       *
                       *
                       *
                       *
                       *
                 * * * * * * *
    
    





    HTML:
     
    
    Height 7 Base 11
    
    
                         *
                        * *  
                       * * *
                      * * * *
                     * * * * *
                    * * * * * * 
                   * ********* * 
    
    
    
     
    Last edited: Apr 12, 2011

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice