CREATE OWN LIBRARY

Solid Half Diamond Left

Back to Programming

Description

Solid Half Diamond left:

 

Here the number of lines is taken as input. The aforesaid pattern is divided into two halves while implemented. In the first half, the numbers of stars are increased, the spaces before the stars are decreased and in the second half, the number of stars decreased, the numbers of spaces are increased. For each half two loops are used to print the stars. The numbers of stars are increased by 1 and in the second half, the numbers of stars are decreased by 1. The pattern is only possible if the number of lines is odd.

Algorithm

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

Code

Time Complexity:

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

                                {     for(k=1;k<=sp;k++)---------------------------------------------O(sp)

                                                printf(" ");

                                      for(j=1;j<=st;j++)------------------------------------------------O(st)

                                                                printf("*");

                                                printf("\n");

                                                sp--;

                                                st+=1;   }

                                st=st-2;

                                sp+=2;

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

                                {              for(k=1;k<=sp;k++)----------------------------------------O(sp)

                                                                printf(" ");

                                                for(j=1;j<=st;j++)------------------------------------------O(st)

                                                                printf("*");

                                                printf("\n");

                                                st-=1;    

                                                sp+=1;  }

The complexity of this program=O((n/2+1)*(sp+st)+ (n/2)*(sp+st))=O(n).