| hazda (14) | |
|
I posted a question on how to create a triangle of asteriks and i was replied by "muzhogg".With the help he gave me i was only able to create a right angled triange of asteriks and here is the code:(please help me transform this code into an equilateral triangle) [ #include <iostream> using namespace std; int main() { for ( int y=0; y<=10; y++) { { for ( int x=0; x<=y; x++) cout << "*"; } cout <<endl; } system("pause>nul"); } ] | |
|
Last edited on
|
|
| AzraelUK (57) | |||
What you'd want to do is pad the asterik with space to the left. This might sound mean, but this would be better off in the Beginners forum.
Things you'll notice: * Instead of using 10 throughout the code, I used #define to give the 'magic number' a proper name. This also means that instead of having to change it in every place it appears, you can just change it at the top. http://en.wikipedia.org/wiki/Magic_number_(programming) * The line 'typedef unsigned char byte' lets you use individual bytes of memory - they can only go from 0 to 255, and they take up less memory than regular ints. http://en.wikipedia.org/wiki/Byte * Instead of system("pause >nul"); I included conio.h and used getch();. This is a much better, more cross-platform solution. Technically, the best way would be to leave out any way to make the program pause, and run it from the console. | |||
|
Last edited on
|
|||
| Aepos (3) | |
Good job explaining it, Azrael. As for hazda's code, however, shouldn't you have a#include <cstdlib> or #include <stdlib> before you can use the "system()" function, or is it included in the iostream header file? | |
|
|
|