CREATE OWN LIBRARY

Hollow Star Diamond

Back to Programming

Description

Hollow Star Diamond 

 

For printing the pattern the number of lines is taken as input. Two extra variables are taken here to count the number of stars and the number of spaces for each line. The stars are printed on the boundary line which makes the pattern hollow. For printing, this pattern the number of lines should be odd. So after taking the number of lines as input checking is done. If it is odd, then only the pattern can be printed else an error message is shown.

Algorithm

INPUT: number of lines
OUTPUT: the aforesaid pattern
PROCESS:
Step 1: [taking the input]
	Read n [number of lines]
Step 2: [printing the pattern]
	Set st<-1
	Set sp<-n/2+1
	If n mod 2≠0 then
For i=1 to n/2+1 repeat
			For j=1 to sp repeat
				Print " "
			[End of ‘for’ loop]
			For k=1 to st repeat
				If k=1 or k=st then
					Print "*"
				Else
					Print “ “
			[End of ‘for’ loop]
			Move to the next line
			Set sp<-sp-1
			Set st<-st+2
		[End of ‘for’ loop]
		Set sp<-sp+2
		Set st<-st-4
		For i=n/2+2 to n repeat
			For j=1 to sp repeat
				Print " "
			[End of ‘for’ loop]
			For k=1 to st repeat
				If k=1 or k=st then
					Print "*"
				Else
					Print “ “
			[End of ‘for’ loop]
			Move to the next line
			Set sp<-sp+1
			Set st<-st-2			
		[End of ‘for’ loop]
	else
		print “Please give correct input(no. of lines should be odd)"
	[End of ‘if]
Step3: Stop.

Code

Time Complexity:

for(i=1;i<=n/2+1;i++)--------------------------------- n/2+1

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

                                                                printf(" ");

                                                for(k=1;k<=st;k++)-------------------------- st

                                                {             if(k==1||k==st)

                                                                                printf("*");

                                                                else

                                                                                printf(" ");           

                                                }

                                                printf("\n");

                                                sp--;

                                                st+=2;   }

                                sp=sp+2;

                                st=st-4;

                                for(i=n/2+2;i<=n;i++)----------------------------------- n/2

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

                                                                printf(" ");

                                                for(k=1;k<=st;k++)---------------------------- st

                                                {              if(k==1||k==st)

                                                                                printf("*");

                                                                else

                                                                                printf(" ");                           

                                                }

                                                printf("\n");

                                                sp++;

                                                st-=2;    }

 

The complexity is: O(((n/2+1)*(sp+st))+((n/2)*(sp+st)))=O(n2).