Diamond Shape in Console : The logic behind it


     Further after understanding core concepts of loops in programming, we have some very interesting practice programs which can boost your programming understanding and concepts more then any thing in my view. Because programming includes art + math hence their practice makes one a good programmer. Let's have a look at some of these.

  • Square         - Hollow Square
  • Triangle       - Hollow Triangle
  • Diamond      - Hollow Diamond

       These shapes are based on 2D matrix of characters hence to print out these we use nested loops. If you are not familiar with loops or nested loops, read this article.
     Here is how loops take care of these problems, nested loops have 2 directions (assume). Main loop runs once and nested one runs more times. In the context of shape let's say a square of 5 x 5, we have to print 5 lines and in each line we have to print 5 characters. Hence the functionality can be divided between main loop and nested loop accordingly. That is:

  • Main loop will track number of lines
  • Nested loop will print those lines

     And that's the core idea, to divide work between loops. Let's have a look on code to print out a custom square:
 #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");  
 }  












    Squares are easy, triangles are just little tricky but they are also easy. You just have to determine the way you will change the size of lines in sequence. And if you have practiced enough, you will be able to implement it right away.
     In triangle, we can just edit the code of a square a little bit and we'll have a code for triangle..!! So what is difference between these 2 shapes? Sequential size of lines printed out by nested loop. That is in square of for example 5 x 5 we print out 5 lines of exactly 5 characters each time. But in triangle we need to print out lines from 1 - 5 or 5 - 1 order that is if first line have a size of 1, then 2nd will have a size of 2 and so on until last line is reached which will have the largest size that of base.
     If we look at values of main loop, they can help use directing nested loop size. i.e if we want size to increase 1 - max, we may just use current value of main loop that would be increasing form 1 - max. Similarly if we want reverse order, we can get same values but this time we will run main loop inversely, that won't effect any thing but inner loop. Because main loop's job is to run nested loop a certain time. So if we run main loop 1 - 5 or 5 - 1 it will run 5 times. But we can get a triangle upside down or simple shape.
 #include <iostream>  
 using namespace std;  
 void main(){  
  int width = 8, height = 5;  
  char custom_character = '*';  
  for (int i = 1; i <= height; i++)  
  {  
  for (int j = 0; j < i; j++)  
  {  
   cout << custom_character;  
  }  
  cout << endl;  
  }  
  system("pause");  
 }  














With a little change:
 #include <iostream>  
 using namespace std;  
 void main(){  
  int width = 8, height = 5;  
  char custom_character = '*';  
  for (int i = height; i > 0; i--)  
  {  
  for (int j = 0; j < i; j++)  
  {  
   cout << custom_character;  
  }  
  cout << endl;  
  }  
  system("pause");  
 }  










Diamond is not a single solid shape, instead it is a combination of shapes like this:
Combining shapes in coding like this is tricky but it is easy than making some newer code for diamond which will once for all print a diamond. That is we will make code of square, convert it into that of triangles as required, and then print all shapes altogether. This is not hard, only change will be that we will be using only one main loop but in main loop we will have more nested loops then 1 because we want to print more then 1 shapes once. In this way we have an easy diamond code.






Let's try to combine 2 triangles and see what happens:

 #include <iostream>  
 using namespace std;  
 void main(){  
  int width = 10, height = 10;  
  char custom_character_1 = '*';  
  char custom_character_2 = '#';  
  for (int i = 1; i <= height; i++)  
  {  
  for (int j = 0; j < i; j++)  
  {  
   cout << custom_character_1;  
  }  
  for (int j = 0; j < i; j++)  
  {  
   cout << custom_character_2;  
  }  
  cout << endl;  
  }  
  system("pause");  
 }  











Now with a little change:
 #include <iostream>  
 using namespace std;  
 void main(){  
  int width = 10, height = 10;  
  char custom_character_1 = '_';  
  char custom_character_2 = '#';  
  for (int i = 1,j = height; i <= height; i++,j--)  
  {  
  for (int x = 0; x < i; x++)  
  {  
   cout << custom_character_1;  
  }  
  for (int y = 0; y < j; y++)  
  {  
   cout << custom_character_2;  
  }  
  cout << endl;  
  }  
  system("pause");  
 }  










See how it is becoming more beautiful?
Now that we have a code which explains how to combine shapes, we can modify it a little bit to make upper half of diamond like this:
 #include <iostream>  
 using namespace std;  
 void main(){  
  int width = 10, height = 10;  
  char custom_character_1 = '#';  
  char custom_character_2 = '_';  
  for (int i = 1,j = height; i <= height; i++,j--)  
  {  
  for (int y = 0; y < j; y++)  
  {  
   cout << custom_character_2;  
  }  
  for (int x = 0; x < i; x++)  
  {  
   cout << custom_character_1;  
  }  
  for (int x = 0; x < i; x++)  
  {  
   cout << custom_character_1;  
  }  
  cout << endl;  
  }  
  system("pause");  
 }  











Similarly for lower half:

 #include <iostream>  
 using namespace std;  
 void main(){  
  int width = 10, height = 10;  
  char custom_character_1 = '#';  
  char custom_character_2 = '_';  
  for (int i = 1,j = height; i <= height; i++,j--)  
  {  
  for (int x = 0; x < i; x++)  
  {  
   cout << custom_character_2;  
  }  
  for (int y = 0; y < j; y++)  
  {  
   cout << custom_character_1;  
  }  
  for (int y = 0; y < j; y++)  
  {  
   cout << custom_character_1;  
  }  
  cout << endl;  
  }  
  system("pause");  
 }  











And if we run it one by one, we have a full diamond like this:
TA-DA...
 #include <iostream>  
 using namespace std;  
 void main(){  
  int width = 10, height = 10;  
  char custom_character_1 = '#';  
  char custom_character_2 = '_';  
  for (int i = 1, j = height; i <= height; i++, j--)  
  {  
   for (int y = 0; y < j; y++)  
   {  
   cout << custom_character_2;  
   }  
   for (int x = 0; x < i; x++)  
   {  
   cout << custom_character_1;  
   }  
   for (int x = 0; x < i; x++)  
   {  
   cout << custom_character_1;  
   }  
   cout << endl;  
  }  
  for (int i = 1,j = height; i <= height; i++,j--)  
  {  
  for (int x = 0; x < i; x++)  
  {  
   cout << custom_character_2;  
  }  
  for (int y = 0; y < j; y++)  
  {  
   cout << custom_character_1;  
  }  
  for (int y = 0; y < j; y++)  
  {  
   cout << custom_character_1;  
  }  
  cout << endl;  
  }  
  system("pause");  
 }  

     Final hint: try replacing custom_character_2 with ' ' (space) and you have a fine diamond. Tune the loops little ant you will have sharp edges.
      Simple wasn't it? Try it yourself because this is the basic rule of learning anything. Good luck.
Have a look at 2nd part of this post, we will discuss how to make these shapes that we just made, hollow. That's right, just how to eliminate all those characters withing walls of shape. It won't be as hard as making shape at first time. See Ya.

Popular posts from this blog

C++ Console : Basic Graphic Techniques

C++ Console : Change Font Size

C++ Console : Color