To calculate the execution time of a program, use the std::chrono library, which was introduced in C++11. This library has two distinct objects: time_point and duration. The time_point represents the variable names that store the start and end times of specific algorithms, functions, or loops used in the program. While duration represents the interval between two different times. The chrono library allows us to subtract these time intervals. Here, we use the high_resolution_clock::now() function, which provides accurate units of time. Calculate the Execution Time of a C++ Program To get the execution time of a program use the chrono header ... Read More
Global variables are declared and defined outside any function in the program. They hold their values throughout the lifetime of the program and are accessible throughout its execution. When we use our non-constant global variable in our programs, it becomes harder to manage. So, it is better to use local variables because of specific functions and easier understanding. You can use the prefix g_ for global variable names to avoid naming conflicts and to indicate that the variable is global. There is another way to encapsulate the global variable by defining the variable static. Therefore, these are the reasons behind ... Read More
In C++, a vector is like a array which can be used accordingly. If you want to combine (append) two vectors, you need to add all the elements of one vector to the end of another. This is called appending a vector in a vector. To append a vector in a vector can simply be done by vector insert() method and moreover by using loops and std::move() to transfer elements. There are different ways to append one vector into another in C++. The most common ones are: Using insert() method Using ... Read More
In Python programming, it is important to check whether a given path points to a file or a directory. This check is very useful before performing operations such as reading, writing or listing contents. In this article, we’ll see the different methods to identify file types using Python. Why should we check the file type? Before interacting with a file path, it is important to confirm the type of the file to make sure that the code behaves as expected. If the code treats a directory as a file or file as a directory, it can cause errors or data ... Read More
Complex numbers are numbers that are expressed as a+bi, where i is an imaginary number and a and b, are real numbers. Some of the example of complex numbers are 2 + 5i, 3 - 9i, 8 + 2i, etc. How to add complex number? Here, we will explain how to add two complex numbers. Let us assume we have two complex numbers, Z1 and Z2. // a and b are the real and imaginary parts of Z1 Z1 = a + bi // c and d are the real and imaginary parts of Z2 Z2 = c + ... Read More
Combination and permutation are a part of combinatorics. The permutation is known as the different arrangements that a set of elements can make if the elements are taken one at a time, some at a time, or all at a time. Combination is the different ways of selecting elements if the elements are taken one at a time, some at a time, or all at a time. In this article, we have the value of 'n' and 'r' such that 'r' should be less than n. Our task is the find the permutation and combination using the value of 'n' ... Read More
In C/C++, both exit() and _Exit() are used to terminate a program. The exit() performs cleanup like flushing output, closing files, and calling functions, while _Exit() ends the program immediately without doing any cleanup. Now, let us learn the difference between exit() and _Exit() individually in C/C++. C++ exit() Function The exit() function is used to clean up before terminating the program. It calls functions registered with atexit(), flushes file buffers, and returns control to the operating system. Syntax Following is the syntax for exit() function: void exit(int status_value); Example In this example, we print "Program is running..." and ... Read More
The fastest algorithm to find the prime numbers is the Sieve of Eratosthenes algorithm. It is one of the most efficient ways to find the prime numbers smaller than n when n is smaller than around 10 million. In this article, we have a given number as 'num'. Our task is to find all the prime numbers less than or equal to num using Sieve of Eratosthenes algorithm in C++. Example Here is an example to find prime numbers less than 10: Input: num = 10 Output: 2 3 5 7 The explanation of the above ... Read More
A subsequence is a sequence that can be derived from another sequence by deleting some elements and without changing the order of elements in sequence. For example, the sequences [3, 10], [3, 2, 20] and [3, 10, 20] are some of the subsequences of [3, 10, 2, 1, 20]. Longest Increasing Subsequence(LIS) is the longest of all the subsequences that are having elements in increasing order. In this article, we will learn how to write a C++ program to find the length of longest increasing subsequence for a given sequence. In other words, we are provided with a sequence ... Read More
In C++, both relational Operators (==) with std::string and std::string::compare() are used to compare two strings for equality, but there's a minor difference in both of these; == compares and returns the results in Boolean, whereas compare() checks lexicographically and returns the result in integers. In this article, we will see a more detailed comparison between these two methods and their uses in different scenarios. The == Operator The relational operator (==) is used to compare two values; it checks if the two given values are equal or not and returns the result in Boolean (True ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP