Posted on by Kalkicode
Code Pattern

Print X inside rectangular box

In this article, we will discuss a program that prints an 'X' pattern inside a rectangular box. The program takes the height of the box as input and generates the desired pattern using asterisks ('*'). We will explain the problem statement, provide a suitable example, discuss the algorithm and pseudocode, and analyze the time complexity of the code. Let's dive in!

Problem Statement

The problem is to create a program that can generate an 'X' pattern inside a rectangular box of a given height. The 'X' pattern is formed by placing asterisks ('*') in a specific arrangement. The height of the box represents the number of rows and columns in the pattern. The program should be able to handle different heights and produce the corresponding 'X' pattern.

Example

Let's consider an example to better understand the problem. Suppose we want to create an 'X' pattern inside a rectangular box with a height of 9. The output would look as follows:

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

Algorithm and Pseudocode

To solve this problem, we can use nested loops to iterate over each element of the box and determine whether to print an asterisk or a blank space. The algorithm can be summarized as follows:

  1. Accept the height of the box as input.
  2. Check if the height is less than or equal to 2. If so, return.
  3. Print the height value as a header for clarity.
  4. Initialize two loop control variables, 'i' and 'j'.
  5. Iterate over the rows using the 'i' variable from 0 to height-1.
  6. For each row, iterate over the columns using the 'j' variable from 0 to height-1.
  7. Check the conditions for printing an asterisk:
    • If i is 0 (top row), j is 0 (left column), i is equal to j, i is height-1 (bottom row), height-1-i is equal to j, or j is height-1 (right column), print an asterisk.
    • Otherwise, print a blank space.
  8. Print a newline character after each row is completed.
  9. Repeat steps 5-8 until all rows and columns are printed.
  10. End of the program.

Here is the pseudocode representing the algorithm:

function print_x(height):
    if height <= 2:
        return
    print "Height: height"
    for i in range(height):
        for j in range(height):
            if i == 0 or j == i or i == height - 1 or height - 1 - i == j or j == 0 or j == height - 1:
                print "* "
            else:
                print "  "
        print newline
X symbol inside rectangular box

Code Solution

Here given code implementation process.

//C Program 
//Print X inside rectangular box
#include <stdio.h>

//Print X star pattern of given height
void print_x(int height)
{
  if (height <= 2)
  {
    return;
  }
  printf("\nHeight : %d  \n\n", height);
  //Loop controlling variables
  int i = 0, j = 0;
  //Loop, which is control the printing row operations
  for (i = 0; i < height; ++i)
  {
    //Loop, which is control the printing column operations
    for (j = 0; j < height; ++j)
    {
      //Condition which is printing a star element
      if (i == 0 || j == i || i == height - 1 || height - 1 - i == j || j == 0 || j == height - 1)
      {
        printf("* ");
      }
      else
      {
        printf("  ");
      }
    }
    printf("\n");
  }
}
int main()
{
  //Simple test
  print_x(9);
  print_x(7);
  print_x(10);
  return 0;
}

Output

Height : 9

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

Height : 7

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

Height : 10

* * * * * * * * * *
* *             * *
*   *         *   *
*     *     *     *
*       * *       *
*       * *       *
*     *     *     *
*   *         *   *
* *             * *
* * * * * * * * * *
/*
  Java Program
  Print X inside rectangular box
*/
class MyPattern
{
  //Print X star pattern of given height
  public void print_x(int height)
  {
    if (height <= 2)
    {
      return;
    }
    System.out.print("\nHeight : " + height + " \n\n");
    //Loop controlling variables
    int i = 0, j = 0;
    //Loop, which is control the printing row operations
    for (i = 0; i < height; ++i)
    {
      //Loop, which is control the printing column operations
      for (j = 0; j < height; ++j)
      {
        //Condition which is printing a star element
        if (i == 0 || j == i || i == height - 1 || height - 1 - i == j || j == 0 || j == height - 1)
        {
          System.out.print("* ");
        }
        else
        {
          System.out.print("  ");
        }
      }
      System.out.print("\n");
    }
  }
  public static void main(String[] args)
  {
    MyPattern obj = new MyPattern();
    //Simple test
    obj.print_x(9);
    obj.print_x(7);
    obj.print_x(10);
  }
}

Output

Height : 9

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

Height : 7

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

Height : 10

* * * * * * * * * *
* *             * *
*   *         *   *
*     *     *     *
*       * *       *
*       * *       *
*     *     *     *
*   *         *   *
* *             * *
* * * * * * * * * *
/*
  C++ Program
  Print X inside rectangular box
*/
#include<iostream>

using namespace std;
class MyPattern
{
  public:
    //Print X star pattern of given height
    void print_x(int height)
    {
      if (height <= 2)
      {
        return;
      }
      cout << "\nHeight : " << height << " \n\n";
      //Loop controlling variables
      int i = 0, j = 0;
      //Loop, which is control the printing row operations
      for (i = 0; i < height; ++i)
      {
        //Loop, which is control the printing column operations
        for (j = 0; j < height; ++j)
        {
          //Condition which is printing a star element
          if (i == 0 || j == i || i == height - 1 || height - 1 - i == j || j == 0 || j == height - 1)
          {
            cout << "* ";
          }
          else
          {
            cout << "  ";
          }
        }
        cout << "\n";
      }
    }
};
int main()
{
  MyPattern obj;
  //Simple test
  obj.print_x(9);
  obj.print_x(7);
  obj.print_x(10);
  return 0;
}

Output

Height : 9

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

Height : 7

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

Height : 10

* * * * * * * * * *
* *             * *
*   *         *   *
*     *     *     *
*       * *       *
*       * *       *
*     *     *     *
*   *         *   *
* *             * *
* * * * * * * * * *
/*
  C# Program
  Print X inside rectangular box
*/
using System;
class MyPattern
{
  //Print X star pattern of given height
  public void print_x(int height)
  {
    if (height <= 2)
    {
      return;
    }
    Console.Write("\nHeight : " + height + " \n\n");
    //Loop controlling variables
    int i = 0, j = 0;
    //Loop, which is control the printing row operations
    for (i = 0; i < height; i++)
    {
      //Loop, which is control the printing column operations
      for (j = 0; j < height; j++)
      {
        //Condition which is printing a star element
        if (i == 0 || j == i || i == height - 1 || height - 1 - i == j || j == 0 || j == height - 1)
        {
          Console.Write("* ");
        }
        else
        {
          Console.Write("  ");
        }
      }
      Console.Write("\n");
    }
  }
  public static void Main(String[] args)
  {
    MyPattern obj = new MyPattern();
    //Simple test
    obj.print_x(9);
    obj.print_x(7);
    obj.print_x(10);
  }
}

Output

Height : 9

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

Height : 7

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

Height : 10

* * * * * * * * * *
* *             * *
*   *         *   *
*     *     *     *
*       * *       *
*       * *       *
*     *     *     *
*   *         *   *
* *             * *
* * * * * * * * * *
<?php
/*
  Php Program
  Print X inside rectangular box
*/
class MyPattern
{
  //Print X star pattern of given height
  function print_x($height)
  {
    if ($height <= 2)
    {
      return;
    }
    echo "\nHeight : ". $height ." \n\n";
    //Loop controlling variables
    $i = 0;
    $j = 0;
    //Loop, which is control the printing row operations
    for ($i = 0; $i < $height; ++$i)
    {
      //Loop, which is control the printing column operations
      for ($j = 0; $j < $height; ++$j)
      {
        //Condition which is printing a star element
        if ($i == 0 || $j == $i || $i == $height - 1 || $height - 1 - $i == $j || $j == 0 || $j == $height - 1)
        {
          echo "* ";
        }
        else
        {
          echo "  ";
        }
      }
      echo "\n";
    }
  }
}

function main()
{
  $obj = new MyPattern();
  //Simple test
  $obj->print_x(9);
  $obj->print_x(7);
  $obj->print_x(10);
}
main();

Output

Height : 9

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

Height : 7

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

Height : 10

* * * * * * * * * *
* *             * *
*   *         *   *
*     *     *     *
*       * *       *
*       * *       *
*     *     *     *
*   *         *   *
* *             * *
* * * * * * * * * *
/*
  Node Js Program
  Print X inside rectangular box
*/
class MyPattern
{
  //Print X star pattern of given height
  print_x(height)
  {
    if (height <= 2)
    {
      return;
    }
    process.stdout.write("\nHeight : " + height + " \n\n");
    //Loop controlling variables
    var i = 0;
    var j = 0;
    //Loop, which is control the printing row operations
    for (i = 0; i < height; ++i)
    {
      //Loop, which is control the printing column operations
      for (j = 0; j < height; ++j)
      {
        //Condition which is printing a star element
        if (i == 0 || j == i || i == height - 1 || height - 1 - i == j || j == 0 || j == height - 1)
        {
          process.stdout.write("* ");
        }
        else
        {
          process.stdout.write("  ");
        }
      }
      process.stdout.write("\n");
    }
  }
}

function main()
{
  var obj = new MyPattern();
  //Simple test
  obj.print_x(9);
  obj.print_x(7);
  obj.print_x(10);
}
main();

Output

Height : 9

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

Height : 7

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

Height : 10

* * * * * * * * * *
* *             * *
*   *         *   *
*     *     *     *
*       * *       *
*       * *       *
*     *     *     *
*   *         *   *
* *             * *
* * * * * * * * * *
#   Python 3 Program
#   Print X inside rectangular box

class MyPattern :
  # Print X star pattern of given height
  def print_x(self, height) :
    if (height <= 2) :
      return
    
    print("\nHeight : ", height ," \n\n", end = "")
    # Loop controlling variables
    i = 0
    j = 0
    # Loop, which is control the printing row operations
    while (i < height) :
      # Loop, which is control the printing column operations
      j = 0
      while (j < height) :
        # Condition which is printing a star element
        if (i == 0 or j == i or i == height - 1 or height - 1 - i == j or j == 0 or j == height - 1) :
          print("* ", end = "")
        else :
          print("  ", end = "")
        
        j += 1
      
      print("\n", end = "")
      i += 1
    
  

def main() :
  obj = MyPattern()
  # Simple test
  obj.print_x(9)
  obj.print_x(7)
  obj.print_x(10)

if __name__ == "__main__": main()

Output

Height :  9

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

Height :  7

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

Height :  10

* * * * * * * * * *
* *             * *
*   *         *   *
*     *     *     *
*       * *       *
*       * *       *
*     *     *     *
*   *         *   *
* *             * *
* * * * * * * * * *
#   Ruby Program
#   Print X inside rectangular box

class MyPattern

  # Print X star pattern of given height
  def print_x(height)
  
    if (height <= 2)
    
      return
    end
    print("\nHeight : ", height ," \n\n")
    # Loop controlling variables
    i = 0
    j = 0
    # Loop, which is control the printing row operations
    while (i < height)
    
      # Loop, which is control the printing column operations
      j = 0
      while (j < height)
      
        # Condition which is printing a star element
        if (i == 0 || j == i || i == height - 1 || height - 1 - i == j || j == 0 || j == height - 1)
        
          print("* ")
        else
        
          print("  ")
        end
        j += 1
      end
      print("\n")
      i += 1
    end
  end
end
def main()

  obj = MyPattern.new()
  # Simple test
  obj.print_x(9)
  obj.print_x(7)
  obj.print_x(10)
end
main()

Output

Height : 9 

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

Height : 7 

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

Height : 10 

* * * * * * * * * * 
* *             * * 
*   *         *   * 
*     *     *     * 
*       * *       * 
*       * *       * 
*     *     *     * 
*   *         *   * 
* *             * * 
* * * * * * * * * * 
/*
  Scala Program
  Print X inside rectangular box
*/
class MyPattern
{
  //Print X star pattern of given height
  def print_x(height: Int): Unit = {
    if (height <= 2)
    {
      return;
    }
    print("\nHeight : " + height + " \n\n");
    //Loop controlling variables
    var i: Int = 0;
    var j: Int = 0;
    //Loop, which is control the printing row operations
    while (i < height)
    {
      //Loop, which is control the printing column operations
      j = 0;
      while (j < height)
      {
        //Condition which is printing a star element
        if (i == 0 || j == i || i == height - 1 || height - 1 - i == j || j == 0 || j == height - 1)
        {
          print("* ");
        }
        else
        {
          print("  ");
        }
        j += 1;
      }
      print("\n");
      i += 1;
    }
  }
}
object Main
{
  def main(args: Array[String]): Unit = {
    var obj: MyPattern = new MyPattern();
    //Simple test
    obj.print_x(9);
    obj.print_x(7);
    obj.print_x(10);
  }
}

Output

Height : 9

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

Height : 7

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

Height : 10

* * * * * * * * * *
* *             * *
*   *         *   *
*     *     *     *
*       * *       *
*       * *       *
*     *     *     *
*   *         *   *
* *             * *
* * * * * * * * * *
/*
  Swift Program
  Print X inside rectangular box
*/
class MyPattern
{
  //Print X star pattern of given height
  func print_x(_ height: Int)
  {
    if (height <= 2)
    {
      return;
    }
    print("\nHeight : ", height ," \n\n", terminator: "");
    //Loop controlling variables
    var i: Int = 0;
    var j: Int = 0;
    //Loop, which is control the printing row operations
    while (i < height)
    {
      //Loop, which is control the printing column operations
      j = 0;
      while (j < height)
      {
        //Condition which is printing a star element
        if (i == 0 || j == i || i == height - 1 || height - 1 - i == j || j == 0 || j == height - 1)
        {
          print("* ", terminator: "");
        }
        else
        {
          print("  ", terminator: "");
        }
        j += 1;
      }
      print("\n", terminator: "");
      i += 1;
    }
  }
}
func main()
{
  let obj: MyPattern = MyPattern();
  //Simple test
  obj.print_x(9);
  obj.print_x(7);
  obj.print_x(10);
}
main();

Output

Height :  9

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

Height :  7

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

Height :  10

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

Time Complexity

The time complexity of the program can be analyzed by examining the nested loops. The outer loop runs 'height' number of times, and the inner loop also runs 'height' number of times. Therefore, the total number of iterations is 'height' * 'height', which gives us a time complexity of O(height^2).

Finally

In this article, we discussed a C program that generates an 'X' pattern inside a rectangular box of a given height. We explained the problem statement, provided a suitable example, described the algorithm and pseudocode, and analyzed the time complexity of the code. The program can produce visually appealing 'X' patterns for various input heights, and the algorithm used allows for easy understanding and implementation. Feel free to try out the program with different input values and explore further modifications to enhance its functionality.

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment