Draw shapes (square), Draw any Letter Ex:( X,Z,N,E,F)

Estimated reading: 4 minutes 110 Views

Nested loops in C++ are a powerful tool for drawing different shapes to the console. By using nested loops, we can iterate over multiple dimensions and print different characters to create different patterns.

Example 1:

#include <iostream>

using namespace std;

int main()
{
    for (size_t i = 1; i <= 10; i++)
    {
        for (size_t j = 1; j <= 10; j++)
        {
            if (i == 1 || i == 10)
                cout << "*";
            else if (j == 1 || j == 10)
                cout << "*";
            else
                cout << " ";
        }
        cout << endl;
    }

    return 0;
}

This code prints a hollow square of asterisks to the console. The outer for loop iterates from 1 to 10, inclusive, and the inner for loop iterates from 1 to 10, inclusive.

The if statement inside the inner for loop checks if i is equal to 1 or 10, or if j is equal to 1 or 10. If any of these conditions are met, the program prints an asterisk to the console. Otherwise, the program prints a space to the console.

Here is a step-by-step explanation of what happens when you run this code:

  1. The program declares two variables i and j, and initializes them to 1.
  2. The program starts the outer for loop.
  3. The outer for loop checks if i is less than or equal to 10. If it is, the inner for loop is executed.
  4. The inner for loop checks if j is less than or equal to 10. If it is, the program prints an asterisk to the console if i is equal to 1 or 10, or if j is equal to 1 or 10. Otherwise, the program prints a space to the console.
  5. The inner for loop then increments j by 1.
  6. The inner for loop repeats steps 4 and 5 until j is greater than 10.
  7. After the inner for loop has terminated, the program prints a newline character to the console.
  8. The outer for loop then increments i by 1.
  9. The outer for loop repeats steps 3-8 until i is greater than 10.

Here is the output of the code:

**********
*        *
*        *
*        *
*        *
*        *
*        *
*        *
*        *
**********

Example 2:

If we want to print a letter like X to the console, expecting an output like this:

*   *
 * * 
  *  
 * * 
*   *

We need to set our logic.
To understand how we can put the asterisks in the previous shape, let’s present them this way:

/*
 1 2 3 4 5
1* * * * *
2* * * * *
3* * * * *
4* * * * *
5* * * * *
*/

Now let’s think about it looking at this shape, when the column meets the row we get a star, so we need a loop that goes through all the meeting points between every row and column and we need an if condition to specify our exact locations to make two diagonal lines cross forming our X letter shape.

all of this is represented in the following code:

#include <iostream>

using namespace std;

int main()
{
    for (size_t i = 1; i <= 5; i++)
    {
        for (size_t j = 1; j <= 5; j++)
        {
            if (i == j || j == 6-i)
                cout << "*";
            else
                cout << " ";
        }
        cout << endl;
    }

    return 0;
}

Example 3:

By understanding how we asterisks are lined here:

/*
 1 2 3 4 5 
1* * * * * 
2* * * * * 
3* * * * * 
4* * * * * 
5* * * * * 
*/

We can think of multiple ways to manipulate our code producing different letters and shapes..

Look at this code:

#include <iostream>

using namespace std;

int main()
{
    for (size_t i = 1; i <= 5; i++)
    {
        for (size_t j = 1; j <= 5; j++)
        {
            if (j == (6) - i || i == 1 || i == 5)
                cout << "*";
            else
                cout << " ";
        }
        cout << endl;
    }

    return 0;
}

Guess what this shape is?!

Output:

*****
   *
  *  
 *   
*****

Example 4:

Now let’s make our shape more complicated

**********
**      **
* *    * *
*  *  *  *
*   **   *
*   **   *
*  *  *  *
* *    * *
**      **
**********

Applying this code which is no different than previous examples

#include <iostream>

using namespace std;

int main()
{
    for (size_t i = 1; i <= 10; i++)
    {
        for (size_t j = 1; j <= 10; j++)
        {
            if (i == j || j == 11 - i || i == 1 || i == 10 || j == 1 || j == 10)
                cout << "*";
            else
                cout << " ";
        }
        cout << endl;
    }

    return 0;
}

 

Share this

Draw shapes (square), Draw any Letter Ex:( X,Z,N,E,F)

Or copy link

CONTENTS
English