new exercise

Dec 19, 2020 at 9:24pm
hi every one .
im begginer in c++ programing.
our master ask a question about printing a double diamond next to each other.
if we write 2 in cmd :

* *
* * * *
* *
* * <--------------:or tree
* * * *
* * * * * *
* * * *
* *
i will be tnxful :)
unfortunately i cant paint two diamond next to each other
Last edited on Dec 19, 2020 at 9:28pm
Dec 19, 2020 at 9:33pm
Hello alabhastam,

So what have you tried?

Post what code you have.

Andy
Dec 20, 2020 at 12:10pm
What should the display look like? What are you meaning by a 'double diamond'?
Dec 20, 2020 at 1:34pm
@seeplus, you can look at the page source to see how he meant it to look:

         *    *
        * *  * *
         *    *
        *     *   <--------------:or tree
       * *   * *
      * * * * * *
       * *   * *
        *     *

Still looks a little off, though.
And I have no idea what "or tree" means.

@alabhastam, "new exercise" is a useless title. Something like "printing a double diamond" would be better.
Last edited on Dec 20, 2020 at 1:37pm
Dec 20, 2020 at 2:45pm
Neat tip, didn't know the whitespace was preserved client-side.
Dec 20, 2020 at 3:02pm
For anything more complicated than a simple geometric shape, it's better to start with something like
 
char screen[25][80];


Then you can 'draw' into any screen[y][x] you like in any order that makes your code life easy for you.

I mean, once you've figured out how to put one diamond at some screen[y][x] coordinate, doing the same 4 times is a doddle.

When you've finished 'drawing', you just print the whole grid.
Dec 21, 2020 at 10:51am
Maybe:

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
#include <iomanip>
#include <iostream>

void diamond(size_t width, size_t no)
{
	width += 1 - width % 2;

	const size_t rows {(width + 1) / 2};

	const auto outline {[no, rows](auto r) {
		for (size_t n = 0; n < no; ++n)
			std::cout << std::setw(rows - r) << "" << std::setw(r * 2 - 1) << std::setfill('*') << "" << std::setw(rows - r) << std::setfill(' ') << "";
		std::cout << '\n';
	}};

	for (size_t i = 1; i <= rows; ++i)
		outline(i);

	for (size_t i = rows - 1; i > 0; --i)
		outline(i);
}

int main()
{
	size_t width {}, across {}, deep {};

	std::cout << "Enter width, no across, no deep: ";
	std::cin >> width >> across >> deep;

	for (size_t n {}; n < deep; ++n)
		diamond(width, across);
}



Enter width, no across, no deep: 9 4 3
    *        *        *        *
   ***      ***      ***      ***
  *****    *****    *****    *****
 *******  *******  *******  *******
************************************
 *******  *******  *******  *******
  *****    *****    *****    *****
   ***      ***      ***      ***
    *        *        *        *
    *        *        *        *
   ***      ***      ***      ***
  *****    *****    *****    *****
 *******  *******  *******  *******
************************************
 *******  *******  *******  *******
  *****    *****    *****    *****
   ***      ***      ***      ***
    *        *        *        *
    *        *        *        *
   ***      ***      ***      ***
  *****    *****    *****    *****
 *******  *******  *******  *******
************************************
 *******  *******  *******  *******
  *****    *****    *****    *****
   ***      ***      ***      ***
    *        *        *        *
Dec 21, 2020 at 11:42am
For a tree, consider:

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
#include <iomanip>
#include <iostream>

void tree(size_t width, size_t no)
{
	width += 1 - width % 2;

	const auto outline {[no](auto r, auto pad, auto rows) {
		for (size_t n = 0; n < no; ++n)
			std::cout << std::setw(rows - r + pad) << "" << std::setw(r * 2 - 1) << std::setfill('*') << "" << std::setw(rows - r + pad) << std::setfill(' ') << "";
		std::cout << '\n';
	}};

	for (size_t t = width + 1; t > 0; t -= 2) {
		const size_t rows {t / 2};

		for (size_t i = 1; i <= rows; ++i)
			outline(i, (width - t + 1) / 2, rows);
	}
}

int main()
{
	size_t width {}, across {};

	std::cout << "Enter width, no across: ";
	std::cin >> width >> across;

	tree(width, across);
}



Enter width, no across: 9 3
    *        *        *
   ***      ***      ***
  *****    *****    *****
 *******  *******  *******
***************************
    *        *        *
   ***      ***      ***
  *****    *****    *****
 *******  *******  *******
    *        *        *
   ***      ***      ***
  *****    *****    *****
    *        *        *
   ***      ***      ***
    *        *        *

Topic archived. No new replies allowed.