The fmax() and fmin() functions in C++ are used to check the maximum or minimum of two floating-point numbers. These functions are defined under the header file of the C++ standard library. C++ fmax() function The fmax() is used to compare and return the larger of two floating-point values. These floating point values can be float, double, and long double. Syntax data_type fmax(data_type value_1, data_type value_2); Here, data_type could be float, double and long double. C++ fmin() function The fmin() is used to compare and return the smaller of two floating-point values. These floating point values can be float, ... Read More
The Naor-Reingold pseudo-random function uses a mathematical formula for generating random numbers using an array of secret keys('a') and bits of an input number('x'). The generated random numbers can be repeated based on the array of secret keys. In this article, our task is to generate random numbers using the Naor-Reingold pseudo-random function. Formula of Naor-Reingold function The formula for generating random numbers using Naor-Reingold Pseudo-Random Function is given below: Example Here is an example of generating 5 random numbers using Naor-Reingold Function: Input: p = 31, g = 3, n = 4, a = [1, ... Read More
Exceptions are problems that arise at the time of execution of a program. They are events that are thrown at runtime. They protect the code and allow the program to run even after an exception is thrown. Exception handling is used to handle the exceptions. Catching All Exceptions To handle all exceptions in C++, use try and catch blocks. Write the code that may generate exceptions inside a try block (try { ... }), and handle or print the exception inside a corresponding catch block (catch(...) { ... }). Syntax Following is the syntax of catch block in C++: ... Read More
There are two time periods provided in the form of hours, minutes and seconds. Then their difference is calculated. For example: Time period 1 = 8:6:2 Time period 2 = 3:9:3 Time Difference is 4:56:59 C++ Program to Find Difference Between Two Times To find the time differences then check whether we need to borrow time, for example, adding 60 seconds if the second time has more seconds. Then just subtract the hours, minutes, and seconds of the second time from the first time to get the result. Example In this example, you will see the time difference of ... Read More
Pure functions always return the same result for the same argument values. They only return the result and there are no extra side effects like argument modification, I/O stream, output generation etc. Following are the examples of pure vs impure functions: Pure Function: sin(), strlen(), sqrt(), max(), pow(), floor() etc. Impure Function: rand(), time(), etc. There are many pure functions in C language such as strlen(), strcmp(), strchr(), strrchr(), strstr(), toupper(), tolower(), abs(), sin(), cos(), sqrt(), exp(), log(), pow(), fabs(), etc. Here we are explaining a few pure functions with ... Read More
To print random numbers within a range, we will have two input variables where we need to set the maximum and minimum values. Using these two values, we can display a random value within the specified range. Example Following is the input-output statement to understand the range of a random number: Input: min = 4, max = 14 Output: 8, 10, 7 Explanation: Any numeric value between 4 and 14 can be displayed in the specified range. Generate Random Number Within a Range Using rand() with Modulus The rand() function generates the random number while modulus operator return ... Read More
Python uses the try-except block to handle errors during runtime. But as your program grows, handling exceptions can get messy with repeated code or too many nested blocks. Using cleaner methods for exception handling can reduce duplication, and make your code easier to manage. In this article, we explore better ways to handle exceptions in Python. Using Specific Exception Types It is best to catch specific exceptions like ValueError or FileNotFoundError instead of using a general except: block. This prevents hiding unexpected errors and improves debugging This approach avoids catching unrelated exceptions like KeyboardInterrupt or TypeError. Example In this ... Read More
Custom exceptions in Python allow you to create meaningful error types that fit your application's needs. By adding error codes and error messages to custom exceptions, you can provide structured (organized) information when errors occur. Although Python has many built-in exceptions, custom exceptions are well-suited to explain specific problems in your program. Including error codes and messages, handling errors, and debugging your code. Creating a Custom Exception with Error Code You can create a custom exception by defining a class that inherits from Python's built-in Exception class. This allows you to include additional details, such as a custom error message ... Read More
In C++, a global variable is a variable that is declared outside of any function or class. It can be accessible from any part of the function. Declaration of a Global Variable The global variables are declared after the heading file inclusions section and before starting the main() function. The declaration is simple just like a normal variable declaration. You need to use data type and variable name. You can also initialize them if required. Syntax The following is the basic syntax of declaring global variable: datatype global_var_name = var_value; Example of Global Variable Declaration In this example, we ... Read More
The queue which is implemented as FIFO where insertions are done at one end (rear) and deletions are done from another end (front). The first element that entered is deleted first. Queue operations EnQueue: Insertion at rear end. DeQueue(): Deletion from front end. But a priority queue doesn’t follow First-In-First-Out, but rather than each element has a priority based on the level of importance. Items with the same priority are processed on First-In-First-Out service basis. An item with higher priority ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP