How to Draw a diamond in c++

How to Draw a diamond in c++

this

..^
.....^ ^
....^ * ^
...^ * * ^
..^ * * * ^
.^ * * * * ^
^ * * * * * ^
.^ * * * * ^
..^ * * * ^
...^ * * ^
....^ * ^
.....^ ^
......^
Last edited on
 
std::cout << "..^\n.....^ ^\n....^ * ^\n...^ * * ^\n..^ * * * ^\n.^ * * * * ^\n^ * * * * * ^\n.^ * * * * ^\n..^ * * * ^\n...^ * * ^\n....^ * ^\n.....^ ^\n......^\n";
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <cmath>
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

template <int R, char B = '*', char I = ' ', char O = ' '> class Diamond {
 public:
	string draw() const {
		ostringstream s;
		int x, y = R + 1;
		while( y --> -(x = R + 1) + 1 ) {
			while( x --> -R ) {
				int taxi = abs(x) + abs(y);
				s << ( taxi == R ? B : taxi < R ? I : O);
			}
			s << '\n';
		}
		return s.str();
	}
};

template <int F, char A, char I, char L>
ostream& operator<<(ostream &out, const Diamond<F,A,I,L> &d) {
	return out << d.draw();
}

//------------------------------------------------------------------------------

int main() {
	return !( cout << Diamond<10, '^', '*'>() );
}
THANK YOU FOR ALL
Topic archived. No new replies allowed.