FOR FREE CONTENT

Matrix Hollow Star Pattern

Back to Programming

Description

Matrix Hollow Star Pattern:

 

 

The length and breadth of the rectangle is taken as input. A hollow rectangle is printed using two for loops. The outer for loop is calculating the breadth (the number of lines of the pattern) of the rectangle and the inner for loop is used for calculating the length (the elements of each line). the stars are printed only at the boundary line which makes the pattern hollow.

Algorithm

INPUT: the length and the breadth of the rectangle
OUTPUT: the aforesaid pattern
PROCESS:
Step 1: [Taking the inputs]
	Read l, b [length & breadth of the rectangle]
Step 2: [printing the pattern]
	For i=1 to b repeat
		For j=1 to l repeat 
			If i=1 or i=b or j=1 or j=l then
				Print “*  “
			Else
				Print “   “
			[End of ‘if’]
		[End of ‘for’ loop]
	[End of ‘for’ loop]
Step 3: Stop.

Code

Time Complexity:

for(i=1;i<=b;i++)---------------------------------------- b

                {               for(j=1;j<=l;j++)------------------------------- l

                                {              if(i==1||i==b||j==1||j==l)

                                                                printf("*   ");

                                                else

                                                                printf("    ");

                                }

                                printf("\n"); 

                }

The complexity is: O(b*l)=O(b2)

If we consider the variable b as n then the complexity can be written as O(n2).