Creating Isoceles Triangle

aliz_khanz's Avatar, Join Date: Apr 2011
Go4Expert Member
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's Avatar, Join Date: Aug 2004
Mentor
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's Avatar, Join Date: Apr 2011
Go4Expert Member
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 shabbir; 8Apr2011 at 20:04.. Reason: Code blocks
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Just run the display loop in the opposite direction, i.e.
Code:
for(j=height-1;j>=0;j--)
aliz_khanz's Avatar, Join Date: Apr 2011
Go4Expert Member
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's Avatar, Join Date: Aug 2004
Mentor
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's Avatar, Join Date: Apr 2011
Go4Expert Member
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 shabbir; 9Apr2011 at 20:52.. Reason: Code blocks
virxen's Avatar, Join Date: Nov 2009
Pro contributor
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's Avatar, Join Date: Apr 2011
Go4Expert Member
sir i really appreciate your help but , allow me to ask , how does it help me with creating the programme ?
virxen's Avatar, Join Date: Nov 2009
Pro contributor
Quote:
Originally Posted by aliz_khanz View Post
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?

Last edited by virxen; 10Apr2011 at 05:24..