Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Creating Isoceles Triangle (http://www.go4expert.com/forums/creating-isoceles-triangle-t25416/)

aliz_khanz 4Apr2011 04:52

Creating Isoceles Triangle
 
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.

xpi0t0s 4Apr2011 20:57

Re: Creating Isoceles Triangle
 
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.

aliz_khanz 8Apr2011 18:48

Re: Creating Isoceles Triangle
 
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");
    }                   
}


xpi0t0s 8Apr2011 21:48

Re: Creating Isoceles Triangle
 
Just run the display loop in the opposite direction, i.e.
Code:

for(j=height-1;j>=0;j--)

aliz_khanz 8Apr2011 22:03

Re: Creating Isoceles Triangle
 
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.

xpi0t0s 9Apr2011 05:16

Re: Creating Isoceles Triangle
 
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:

    *
    *
  * * *
  * * *
* * * * *
* * * * *

?

aliz_khanz 9Apr2011 20:36

Re: Creating Isoceles Triangle
 
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:

    *
  *****
 *********


virxen 10Apr2011 05:08

Re: Creating Isoceles Triangle
 
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)

aliz_khanz 10Apr2011 05:10

Re: Creating Isoceles Triangle
 
sir i really appreciate your help but , allow me to ask , how does it help me with creating the programme ? :)

virxen 10Apr2011 05:18

Re: Creating Isoceles Triangle
 
Quote:

Originally Posted by aliz_khanz (Post 81753)
sir i really appreciate your help but , allow me to ask , how does it help me with creating the programme ? :)

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?


All times are GMT +5.5. The time now is 18:37.