FOR FREE CONTENT

Sandglass Star Pattern

Back to Programming

Description

Sandglass Star Pattern

 

For printing the pattern the number of lines is taken as input. The pattern is divided into two parts. In the first half, the numbers of stars are decreased and in the 2nd part, the numbers of stars are increased. Two extra variables are taken here to count the number of stars and the number of spaces for each line. 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<-n/2+1
	Set sp<-0
	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
				Print "* "
			[End of ‘for’ loop]
			Move to the next line
			Set sp<-sp+1
			Set st<-st-1
		[End of ‘for’ loop]
		Set sp<-sp-2
		Set st<-st+2
		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
				Print "* "
			[End of ‘for’ loop]
			Move to the next line
			Set sp<-sp-1
			Set st<-st+1			
		[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

                                                                printf("* ");

                                                printf("\n");

                                                sp++;

                                                st--;

                                }

                                //printing the second half of the pattern where the number of stars

                                //are increasing

                                sp=sp-2;

                                st=st+2;

                                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

                                                                printf("* ");

                                                printf("\n");

                                                sp--;

                                                st++;     }

 

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