how to draw a triangle in c++
08-13-2006 #2
and the chapeau of int overfl
> cout << "Height of triangle? ";
> std::cin >> lengthLine;
So why didn't you phone call your variable heightOfTriangle ?And so derive from that some other variable chosen lengthRow?
> How can I make my program better?
Both press for loops do the same thing, so create a function, sayWhich would simplify your main loop toCode:
void draw ( char ch, int len ) { for ( int i = 0 ; i < len ; i++ ) cout << ch; }
Amend is subjective, and measured in many different ways rather than merely counting how many characters are in your source file.Code:
describe( ' ', line ); describe( '*', lengthRow );
08-xiii-2006 #iv
and the chapeau of int overfl
08-13-2006 #five
(?<!re)tired
You can look at the exercise from a conceptual fashion besides.The commencement thing that can strike you lot is that you are being asked to draw spaces and asterisks. So in fact you are being asked to describe a rectangle. And yous fifty-fifty already have one of the sizes; the number of lines gives y'all the summit. You can then expand on this concept to devise the formula that will enable you to know the width. For that, you now need to look at the triangle itself.
Y'all demand to detect a pattern you can utilize equally a formula. For that, why not offset with the concluding line and see what properties nosotros can notice as nosotros motion up the triangle? Nosotros could also kickoff from the top. However, patterns are usually less challenging when working with bigger numbers and they can also intermission when we work with the number 1 or 0. e.q. More often than not we demand to create an exception when the significant element of the formula (the chemical element that grows or shrinks) is ane or 0. So it'south easier to start from the bottom where the bigger numbers are and then meet if that exception needs to be created.
So... the concluding line in your case is line 5 and it has ix asterisks. The height of your rectangle is 5 and the width is 9. Next...
What can we run into every bit a pattern? Before yous read further on, take some fourth dimension to try and meet it yourself...Code:
(line 5, height = v, width = 9) line 4, tiptop = four, width = 7 line 3, height = 3, width = 5 line 2, top = two, width = 3 line 1, height = one, width = 1Well, each line width is equal to line_height * 2 - 1. And it even works with one! Although it will break with 0. You cannot have -i asterisks.
Then... with that formula you can now easily create your triangle with but one for loop. The all-time mode is to first store the width in an integer before y'all enter the loop. Each line will exist equal to:
(overall_width - line_width) / 2 in spaces concatenated to,
line_width in asterisks concatenated to,
(overall_width - line_width) / 2 in spaces.The only other care y'all will need to have is to either not accept 0 equally an input for the peak of the triangle or take it, merely output an empty string.
Originally Posted by brewbuck:
Reimplementing a large system in some other language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
08-13-2006 #7
(?<!re)tired
Employ append(), for instance, and work with the formulas you discovered.I'm not writing it all to you on purpose. But practice let me know if at that place is anything specific yous didn't understand on my previous mail.Code:
int elevation = 5 // this value you get from the user input int width = pinnacle * two - 1 // this is the formula you discovered std::cord yourString = ""; // initialize the string for(int i = 1; i <= height; ++i ) { yourString.append(spaces_formula_result, ' ') yourString.append(asterisks_formula_result, '*') yourString.append(spaces_formula_result, ' ') yourString.append('\due north') }EDIT: you don't need to utilize strings if you don't desire to. You can still utilise std::cout.
Code:
#include <iomanip> for(int i = i; i <= superlative; ++i ) { std::cout << setw(spaces_formula_result) << setfill(' '); std::cout << setw(asterisks_formula_result) << setfill('*'); std::cout << setw(spaces_formula_result) << setfill(' '); std::cout << std::endl; }
Final edited by Mario F.; 08-xiii-2006 at 09:49 AM.
Originally Posted by brewbuck:
Reimplementing a large system in another linguistic communication to get a 25% operation boost is nonsense. It would exist cheaper to simply get a computer which is 25% faster.
08-13-2006 #eight
and the lid of int overfl
> But doesn't that still exercise the aforementioned thing?
Of course it does, the signal was to show that you tin excerpt similarity in duplicate code by adding parameters.> I thought that there would be a way to cull where to draw on the screen
Merely where do you stop?
- position on screen
- change the font
- modify colours
- animation / audio
Yeah y'all can add all these things (and more), simply the code volition grow to do this.Book exercises test agreement of cardinal ideas, and this one is well-nigh loops.
Source: https://cboard.cprogramming.com/cplusplus-programming/81830-draw-triangle.html
Posted by: williamsalannow.blogspot.com
0 Response to "how to draw a triangle in c++"
Post a Comment