Found 7197 Articles for C++

How to get time in milliseconds using C++ on Linux?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

5K+ Views

Here we will see how to get time (the elapsed time for the program or any other kind of time).Here we are using linux library for C++. There is a structure called timeval. This timeval stores the time in seconds, milliseconds. We can create two time for start and end, then find the difference from them.Example#include #include #include using namespace std; main() {    struct timeval start_time, end_time;    long milli_time, seconds, useconds;    gettimeofday(&start_time, NULL);    cout > ch;    gettimeofday(&end_time, NULL);    seconds = end_time.tv_sec - start_time.tv_sec; //seconds    useconds = end_time.tv_usec - start_time.tv_usec; //milliseconds ... Read More

Handling large numbers in C++?

Samual Sam
Updated on 30-Jul-2019 22:30:26

12K+ Views

In C++, we can use large numbers by using the boost library. This C++ boost library is widely used library. This is used for different sections. It has large domain of applications. For example, using boost, we can use large number like 264 in C++.Here we will see some examples of boost library. We can use big integer datatype. We can use different datatypes like int128_t, int256_t, int1024_t etc. By using this we can get precision up to 1024 easily.At first we are multiplying two huge number using boost library.Example#include #include using namespace boost::multiprecision; using namespace std; int128_t large_product(long ... Read More

Generate random numbers following a normal distribution in C/C++

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

2K+ Views

Here we will see how to generate random numbers, which are following in a normal distribution. For the normal random, the formula is like below.𝑧 = √−2 ln 𝑥1 cos (2𝜋𝑥2)Here x1 and x2 are chosen randomly.Example#include #include #include #include using namespace std; double rand_gen() {    // return a uniformly distributed random value    return ( (double)(rand()) + 1. )/( (double)(RAND_MAX) + 1. ); } double normalRandom() {    // return a normally distributed random value    double v1=rand_gen();    double v2=rand_gen();    return cos(2*3.14*v2)*sqrt(-2.*log(v1)); } main() {    double sigma = 82.0;    double Mi = 40.0;    for(int i=0;i

How to get memory usage at runtime using C++?

Samual Sam
Updated on 30-Jul-2019 22:30:26

8K+ Views

We can get the memory usage like virtual memory usage or resident set size etc. at run time. To get them we can use some system libraries. This process depends on operating systems. For this example, we are using Linux operating system.So here we will see how to get the memory usage statistics under Linux environment using C++. We can get all of the details from “/proc/self/stat” folder. Here we are taking the virtual memory status, and the resident set size.Example#include #include #include #include #include using namespace std; void mem_usage(double& vm_usage, double& resident_set) {   ... Read More

How to print Unicode character in C++?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

6K+ Views

To print the Unicode characters, the process is very similar to the actual printing process in C++.We can put the Unicode value with the prefix \u. Thus we can successfully print the Unicode character.Note: If the console does not support Unicode, then you cannot get the correct result. Here we have used the Linux system to solve this problem.Example#include using namespace std; int main() {    cout

How to make the program sleep for x milliseconds in C++?

Samual Sam
Updated on 30-Jul-2019 22:30:26

527 Views

Here we will see how to sleep for x (given by user) milliseconds in C++ program.To do this thing we can use different libraries. But here we are using the clock() function. The clock() will return the current CPU time. Here we will try to find the ending time from the clock, and the given x value. Then for that amount of time, we will run one blank while loop to take the time. Here one macro is used called CLOCKS_PER_SEC, this finds the number of clock ticks per second.Let us see the code to get the better idea about ... Read More

What is the difference between static_cast<> and C style casting?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

4K+ Views

Here we will see what are the differences between static_cast and normal C style cast.The normal cast like (int)x is C style typecasting where static_cast(x) is used in C++.This static_cast() gives compile time checking facility, but the C style casting does not support that. This static_cast() can be spotted anywhere inside a C++ code. And using this C++ cast the intensions are conveyed much better.In C like cast sometimes we can cast some type pointer to point some other type data.Like one integer pointer can also point character type data, as they are quite similar, only difference is character has ... Read More

How do I generate random floats in C++?

Samual Sam
Updated on 30-Jul-2019 22:30:26

4K+ Views

In C or C++, we cannot create random float directly. We can create random floats using some trick. We will create two random integer values, then divide them to get random float value.Sometimes it may generate an integer quotient, so to reduce the probability of that, we are multiplying the result with some floating point constant like 0.5.Example#include #include #include using namespace std; main() {    srand((unsigned int)time(NULL));    float a = 5.0;    for (int i=0;i

How to automatically generate a stacktrace when a gcc C++ program crashes?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

2K+ Views

For Linux and we can use gcc to compile C/C++ codes. This compiler uses glibc library. We can use the backtrace() function to trace the error. This function is present inside the execinfo.h header file. In this example, we are going to display Segmentation fault error using the stack trace feature.Example#include #include #include #include #include using namespace std; void error_handler(int sig) {    void *array[10];    size_t size;    size = backtrace(array, 10); //get the void pointers for all of the entries    cout

How to execute a command and get output of command within C++ using POSIX?

Samual Sam
Updated on 30-Jul-2019 22:30:26

865 Views

Here we will see how to use the POSIX command through C++. The process is very simple, we have to use the function called system(). Inside this we have to pass string. That string will contain the POSIX command. The syntax is like below.system(“command”)Example#include using namespace std; int main () {    cout

Advertisements