CODING JOY

The stunning potpourri of coding and mundane life

ANIMATED HOURGLASS PATTERN IN C LANGUAGE



AMRUHA AHMED
17th June,2023.



This article will help you create an animated hourglass pattern in C language.This pattern can be generated using nested loops and the animated effect is provided by the usage of threads.

blog1

DELVING DEEPER:


BREAK-UP OF THE UPPER HALF OF THE PATTERN:

blog1

BREAK-UP OF THE LOWER HALF OF THE PATTERN:

blog1

All these instructions of pattern creation are present in the runner method and get executed when a thread is created in the main method.


FUNCTIONS REQUIRED:

PROGRAM:

The following program in C language prints the desired pattern on the screen and adds animations to it:

                
                    #include<stdio.h>
                    #include<stdlib.h>
                    #include<pthread.h>
                    #include<unistd.h>
                    void *runner();
                    void main()
                    {
                        pthread_t tid;//thread id
                        int ret;//to store a return value
                        ret=pthread_create(&tid,NULL,runner,0);//creating a thread
                        
                        if(ret==-1)
                        {
                            printf("\n Thread has not been created");
                            exit(1);
                        }
                        pthread_join(tid,0);
                     
                    }
                    
                    void *runner()
                    {
                            int m=11;//number of asterisks to be printed
                            int s=0;// number of spaces
                            int i,j;//i-outer loop counter, j-inner loop counter
                            //upper half of the pattern
                            for(i=1;i<=6;i++)
                            {
                                for(j=1;j<=s;j++)
                                printf("  ");
                                for(j=1;j<=m;j++)
                                printf("* ");
                                sleep(1);
                                s++;
                                m-=2;
                                printf("\n");
                            }
                            m=3;
                            s=4;
                            //lower half of the pattern
                            for(i=1;i<=5;i++)
                            {
                                for(j=1;j<=s;j++)
                                printf("  ");
                                for(j=1;j<=m;j++)
                                printf("* ");
                                sleep(1);
                                s--;
                                m+=2;
                                printf("\n");
                            }
                            pthread_exit(0);
                    }
                     

             
         
CODE COPIED

OUTPUT: