Diamond and other Shapes in Console : Make them hollow
To get the idea of printing a shape in c++ console, read this article. Here we'll discuss how will we implement hollowness in the shapes that we made in previous article.
First revise the code of square, easy wasn't it? So is to implement hollowness in it. We just need some if statements to bypass the process of filling the shape.
OK, have a look in nested loop of square code: #include <iostream>
using namespace std;
void main(){
int width = 8, height = 5;
char custom_character = '*';
for (int i = 0; i < height; i++)
{
for (int i = 0; i < width; i++)
{
cout << custom_character;
}
cout << endl;
}
system("pause");
}
What we need to do here is that when this loop starts, it should print 1 character and then for rest n-2 character it should print some character which will have no shape like SPACE. It'll show hollowness in shape and similarly for last character we want it again to print default character. It's very simple, just put if condition for first and last iteration of loop. In these 2 iteration we want normal character printed and else wise we want to print SPACE. Like this:
#include <iostream>
using namespace std;
void main(){
int width = 8, height = 5;
char custom_character = '*';
for (int i = 0; i < height; i++)
{
for (int i = 0; i < width; i++)
{
if (i > 0 && i < width - 1)
cout << ' ';
else
cout << custom_character;
}
cout << endl;
}
system("pause");
}#include <iostream>
using namespace std;
void main(){
int width = 8, height = 5;
char custom_character = '*';
for (int i = 0; i < height; i++)
{
for (int i = 0; i < width; i++)
{
if (i > 0 && i < width - 1)
cout << ' ';
else
cout << custom_character;
}
cout << endl;
}
system("pause");
}
Now only one thing is left, that is first line of shape and last line of shape should be intact, that is it should be printed normally, filled with characters because we want to cover all sides of shape with characters and just empty inside of the shape. Just enhance above if condition a little bit and we are good to go like this:
#include <iostream>
using namespace std;
void main(){
int width = 8, height = 5;
char custom_character = '*';
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (j > 0 && j < width - 1 && i > 0 && i < height - 1)
cout << ' ';
else
cout << custom_character;
}
cout << endl;
}
system("pause");
}
It was simple wasn't it? So when you will edit this code for triangle same effect will happen to triangle too like this:
#include <iostream>
using namespace std;
void main(){
int width = 10, height = 10;
char custom_character = '*';
for (int i = 1; i <= height; i++)
{
for (int j = 0; j < i; j++)
{
if (j > 0 && j < i - 1 && i > 0 && i < height )
cout << ' ';
else
cout << custom_character;
}
cout << endl;
}
system("pause");
}
With this done, now we have basic code ready for basic shapes that help in printing a diamond:
#include <iostream>
using namespace std;
void main(){
int width = 10, height = 10;
char custom_character_1 = '#';
for (int i = 0, j = height; i < height; i++, j--)
{
for (int y = 0; y < j; y++)
{
cout << ' ';
}
for (int x = 0; x < i; x++)
{
if (x > 0 && i > 0)
cout << ' ';
else
cout << custom_character_1;
}
for (int x = 0; x < i; x++)
{
if (x < i -1 && i > 0)
cout << ' ';
else
cout << custom_character_1;
}
cout << endl;
}
for (int i = 0, j = height; i < height; i++, j--)
{
for (int x = 0; x < i; x++)
{
cout << ' ';
}
for (int y = 0; y < j; y++)
{
if (y > 0 && j > 0)
cout << ' ';
else
cout << custom_character_1;
}
for (int y = 0; y < j; y++)
{
if (y < j - 1 && j > 0)
cout << ' ';
else
cout << custom_character_1;
}
cout << endl;
}
system("pause");
}
Please leave comment if any part of code wasn't understood of ill explained and do read the basic article to get an idea how these shapes are printed in first hand. Thank you for reading. Good luck. And do practice this code yourself. For error analysis, you can share your codes with me on my email, or more preferably on facebook (in case your email get's in spam folder).
Comments
Post a Comment