make a diamond

I was just curious but how to make a program that would display this in c++
using iostream and "name space std;"
(i replaced the spaces(" ") with a "_")

***********
*****_*****
****___****
***_____***
**_______**
*_________*
**_______**
***_____***
****___****
*****_*****
***********

i know that i can just use "cout<< "*";" to make that image but
is there a more efficient way of doing this or would i just need to use
cout?

is there a more efficient way of doing this or would i just need to use
cout?

First of all, you're still going to use std::cout to print the diamond, no matter what method you use to create it.

Secondly, plainly using std::cout is already efficient. The problem is that if the diamond is to change size, you'd have to rewrite everything.

So perhaps the word you're looking for is not "efficient", but "smart".
We must figure out a smart method to generate the diamond.

Well in the end, the diamond must be printed line by line. So let's generate the lines one by one.

To do that, write a function generate_line() that looks a bit like this:

1
2
3
4
5
void generate_line(int length, int fill_length, char rc, char fc);
// length == the total length of the line
// fill_length == the number of filled characters symmetrically towards the center
// rc == regular character
// fc == filling character 


Here are some sample outputs to show how the function should behave:

1
2
3
4
5
generate_line(5, 0, '*', 'A');
generate_line(5, 1, '*', 'A');
generate_line(5, 2, '*', 'A');
generate_line(6, 1, '0', '.');
generate_line(3, 3, 'u', '!');
*****
A***A
AA*AA
.0000.
!!!


Now all you have to do is write that function, then I'll help you further if needed.

Edit: mistakes.
Last edited on
Topic archived. No new replies allowed.