Signup/Sign In

C++ Program To Print Perfect Square Of Program Using Sleep And Delay Function

This tutorial will see how we can print the perfect square using the sleep and delay function. Before moving towards the program, let's see a brief about what these functions do.delay() function requires a parameter which should be a number, defining the milliseconds for the delay. sleep() function requires a parameter which should be a number, defining the seconds to sleep. These functions are pretty useful for showing the user multiple outputs for a given period.

The difference between sleep() and delay() function is that although both delay() and sleep() function suspends the system for some amount of time, delay takes a millisecond as an argument while sleep takes second as an argument.

Here is one example of how we are expecting our output forthe given program:-

1 2 3 4 5 6 7 8 9 10
36 37 38 39 40 41 42 43 44 11
35 64 65 66 67 68 69 70 45 12
34 63 84 85 86 87 88 71 46 13
33 62 83 96 97 98 89 72 47 14
32 61 82 95 100 99 90 73 48 15
31 60 81 94 93 92 91 74 49 16
30 59 80 79 78 77 76 75 50 17
29 58 57 56 55 54 53 52 51 18
28 27 26 25 24 23 22 21 20 19

C++ Program Implementing The Sleep() and Delay() Function

#include<iostream>
#include <chrono>
#include <thread>

using namespace std;

int main()
{
    int a[10][10]={0},i,j,low=0,top=9,n=1;
    for(i=0;i<5;i++,low++,top--)
    {
        for(j=low;j<=top;j++,n++)
            a[i][j]=n;
        for(j=low+1;j<=top;j++,n++)
            a[j][top]=n;
        for(j=top-1;j>=low;j--,n++)
            a[top][j]=n;
        for(j=top-1;j>low;j--,n++)
            a[j][low]=n;
    }
    cout<<("\t\t\t\tPerfect Square\n");
    for(i=0;i<10;i++)
    {
        cout<<("\n\n\t");
        for(j=0;j<10;j++)
        {
            cout<<("%6d",a[i][j]);
            sleep(1);
        }
    }
    return 0;
}


1 2 3 4 5 6 7 8 9 10
36 37 38 39 40 41 42 43 44 11
35 64 65 66 67 68 69 70 45 12
34 63

Conclusion

In this tutorial, we have seen how we can be able to find the perfect square using a sleep function and delay method.



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.