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.
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.
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"); } }
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.
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: * * * * * * * * * * * * * * * * * * ?
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: * ***** *********
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)
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?
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 !
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?
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"); } }
just one problem. when i type the legth of base greater than the height , it draws a straight line .......... can you please fix it ?
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"); } }
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 * * * * * * * * * * * * * * * * * * * * * * ********* *