I STILL DON'T GET IT

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
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <conio.h>
#define LINES 10

using namespace std;

typedef unsigned char byte;
// let us define single bytes - not really necessary in a small program like
// this, but it's a good practice.

int main()
{
	for (byte line = 0; line <= LINES; line++) {
		for (byte x = 0; x <= LINES - line; x++) cout << " ";
		for (byte x = 0; x <= (line * 2); x++) cout << "*";
		cout << endl;
	}
	getch();
}


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
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?
Topic archived. No new replies allowed.