Patterns!

Hello World!

Welcome to BitXorBytes!

This is our first post in the series “Explaining Algorithms”. We start by discussing various interesting patterns!

Right-Angled Perimeter Triangle

*
*  *
*     *  
*        *             
* *  *  *
Algorithm: 
1: Start
2: Declare variable i, j rows;
3: Initialize variables
      i=1;
      j=1;
4: Read value of rows
5: Repeat the steps until i=rows
   5.1: Repeat the steps until j=i
          5.1.1 If i==j OR j=1 OR i==rows
                    Display "*"
                   Else
                    Display " "
         5.1.2 j++ 
   5.2:  Start new line
   5.3: i++
6: End

Explanation: Since we want to print only the perimeter of the triangle, one must first figure out where exactly should an ” * ” be printed.  On analysis, you would get to know that there are three cases which are the conditions for the perimeter:

1) When variable i is equal to variable j: This is the condition for the diagonal elements (hypotenuse) 2) When variable j=1: This condition gives the vertical line in the triangle (perpendicular) 3) When variable i is equal to the total number of rows: This condition gives the last row of a triangle (base)   In all other combinations of i and j, one would get the interior of the triangle.

   
Isosceles Perimeter Triangle

           *
         *    *
       *        *                    
     *   *    *   *  
Algorithm: 
1: Start
2: Declare variable i, j rows,space  ;
3: Initialize variables
      i=1;
      j=1;
      space=1;
4: Read value of rows
5: Repeat the steps until i=rows
   5.1: Repeat the steps until space=(rows-i)
          5.1.1 Display " "
          5.1.2 space++ 
   5.2: Repeat the steps until j=(2*rows -1)
          5.2.1 If i==rows OR j=1 OR j==(2*i-1)
                    Display "*"
                    Else
                    Display " "
          5.2.2 j++ 
   5.3:  Start new line
   5.4: i++
6: End

Explanation: Here,to print only the perimeter of the center aligned triangle, one has to see where ” * ” has to be printed.  So, first the cursor is set to the right place by using an additional variable called space.  After setting the cursor to the right place, there are three cases where a “*” must be printed: 1) When variable j is equal to (2*i-1): This is the condition for the right side perimeter of the triangle 2) When variable j=1: This condition gives the left side perimeter of the triangle  3) When variable i is equal to the total number of rows: This condition gives the last row of a triangle or the base   In all other combinations of i and j, one would get the interior of the triangle.

Numbers & Stars

1 * * * *
1 2 * * *
1 2 3 * *
1 2 3 4 *
1 2 3 4 5
Algorithm: 
1: Start
2: Declare variables i, j ,k ,rows;
3: Initialize variables
      i=1;
      j=1;
      k=rows;
4: Read value of rows
5: Start a for loop till  i<=rows 
  5.1: Start another for loop till j<=i
       5.1.1 Display j;
5.2 Start a for loop for k till k>= j, decrementing k by 1
         5.2.1 Display “*“;
5.3 Start a new line 
6: End

Explanation: Here we are required to print Number pattern and star pattern in a given orientation. Hence, we require 3 for loops 1) Till I != Number of rows (initially entered by user) 2) Till j<= I ,we display the number pattern through this loop 3)  Starting value of k from rows,till k>=j,and decrementing its value by 1,we display the “*” pattern. Hence the number and star pattern is printed.

Number-Mountain

1           1
1 2       2 1
1 2 3   3 2 1
1 2 3 4 3 2 1 
Algorithm: 
1: Start
2: Declare variables i, j, k, rows;
3: Initialize variables
      i=1;
      j=1;
      k=1;
4. Read value of rows
5: Start a for loop till  i<=rows
5.1: Start another for loop till j<=i
       5.1.1 Display k “ “ ;
        5.1.2 Increment k by 1;
5.2 Start a for loop till j<=2*n -1-2*i
         5.2.1 Display “ “;
5.3 Start another for loop till j<=i
	5.3.1 Decrement k by 1;
        5.3.2 if k == rows ,then continue the loop 
               Else Display k “ “;
5.4 Start a new line 
6: End

Explanation: Here we are required to print the pattern mountain according to the number of rows entered by the user. Hence, there will be 4 for loops for: 1) Till I != Number of rows (initially entered by user), 2)  Displaying Value of variable k  (Left side pattern) 3) Displaying space (condition is j<=2*n -1-2*I ) 4)  Displaying Value of variable k (Right side pattern)     4.1) Here there are 2 conditions 4.1.1) If k is equal to the number of rows (initially entered by user), then continue the loop       4.1.2) Else Display variable k (Right side pattern) Now the pattern mountain will be printed.

It was so much fun writing this post and brainstorming the algorithms for different patterns. Hope you enjoy reading them! And do create your own algorithms also. Happy Programming!

For any doubts or ambiguities, mail us at everythinggeeky.101@gmail.com

Don’t forget to Like, Comment and Share with your friends.

Stay Tuned for Everything Geeky!

Cheers! 🙂

Team Bitxorbytes

Image Credits: https://www.pinterest.com/pin/689121180456329700/

Published by BitXorBytes

A technical blog which features articles about anything and everything related to computer science and programming! Learn something new and interesting with every post!

Leave a comment

Design a site like this with WordPress.com
Get started