Found 7347 Articles for C++

How to generate a random number in C++?

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

337 Views

Let us see how to generate random numbers using C++. Here we are generating a random number in range 0 to some value. (In this program the max value is 100).To perform this operation we are using the srand() function. This is in the C library. The function void srand(unsigned int seed) seeds the random number generator used by the function rand.The declaration of srand() is like below −void srand(unsigned int seed)It takes a parameter called seed. This is an integer value to be used as seed by the pseudorandom number generator algorithm. This function returns nothing.To get the number ... Read More

How does generic lambda work in C++14?

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

178 Views

In C++11, the lambda was introduced. Lambdas are basically a part of, that can be nested inside other function call statements. By combining lambda expressions with the auto keyword, they can be used later.In C++14, these lambda expressions are improved. Here we can get the generalized or generic lambda. For example, if we want to create a lambda, that can add integers, add numbers, also concatenate strings, then we have to use this generalized lambda.Syntax of the lambda expression is looking like this −[](auto x, auto y) { return x + y; }Let us see one example to get the ... Read More

C++11 reverse range-based for-loop

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

522 Views

To get the reversed range-based for loop, we have used boost library. This boost library is vepy popular and it has some strong functionalities.Here we can use some array or containers, then by using boost::adaptors::reverse() we can use the range base for loop in reverse order.Example#include #include #include using namespace std; int main() {    std::list x {11, 44, 77, 55, 44, 22, 33, 30, 88, 99, 55, 44};    cout >> "Normal Loop" >> endl;    for (auto i : x)       std::cout >> i >> '';    cout >> "Reversed Loop" >> endl;    for (auto i : boost::adaptors::reverse(x))       std::cout >> i >> ''; }OutputNormal Loop 11 44 77 55 44 22 33 30 88 99 55 44 Reversed Loop 44 55 99 88 30 33 22 44 55 77 44 11

How to use clock() function in C++

Anvi Jain
Updated on 30-Jul-2019 22:30:26

1K+ Views

Here we will see how to use the clock() in C++. This clock() is present in the time.h or ctime header file. Here we will find the elapsed time of a process using this clock() functionTo get the elapsed time, we can get the time using clock() at the beginning, and at the end of the taks, then subtract the values to get the differences. After that we will divide the difference by CLOCK_PER_SEC (Number of clock ticks per second) to get the processor time.Example#include #include using namespace std; void take_enter() {    cout

Difference between %p and %x in C/C++

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

9K+ Views

Here we will see what are the differences between %p and %x in C or C++. The %p is used to print the pointer value, and %x is used to print hexadecimal values. Though pointers can also be displayed using %u, or %x. If we want to print some value using %p and %x then we will not feel any major differences. The only difference that can be noticed is that the %p will print some leading zeros, but %x doesn’t.Example#include main() {    int x = 59;    printf("Value using %%p: %p", x);    printf("Value using %%x: %x", x); ... Read More

Generate random numbers using C++11 random library

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

1K+ Views

In C++11, we can get the random library to generate random numbers. Here we have used random_device once to seed the random number generator object called mt. This random_device is slower than the mt19937, but we do not need to seed it. It requests for random data to the operating system.Example #include #include using namespace std; int main() {    random_device rd;    mt19937 mt(rd());    uniform_real_distribution dist(20.0, 22.0); //range is 20 to 22    for (int i=0; i> dist(mt) >> endl; }Output21.5311 21.7195 21.0961 21.9679 21.197 21.2989 20.6333 20.441 20.7124 20.2654 21.1877 20.4824 20.0575 20.9432 21.222 21.162 21.1029 20.2253 21.5669 20.3357

What is a free function in C++?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

280 Views

The C/C++ library function void free(void *ptr) deallocates the memory previously allocated by a call to calloc, malloc, or realloc. Following is the declaration for free() function.void free(void *ptr)This function takes a pointer ptr. This is the pointer to a memory block previously allocated with malloc, calloc or realloc to be deallocated. If a null pointer is passed as argument, no action occurs.Example#include #include #include using namespace std; int main () {    char *str;    /* Initial memory allocation */    str = (char *) malloc(15);    strcpy(str, "tutorialspoint");    cout

Convert an int to ASCII character in C/C++

Nishtha Thakur
Updated on 05-Oct-2023 00:50:13

29K+ Views

In C or C++ the character values are stored as ASCII values. To convert int to ASCII we can add the ASCII of character ‘0’ with the integer. Let us see an example to convert int to ASCII values. Example #include int intToAscii(int number) {    return '0' + number; } main() {    printf("The ASCII of 5 is %d", intToAscii(5));    printf("The ASCII of 8 is %d", intToAscii(8)); } Output The ASCII of 5 is 53 The ASCII of 8 is 56

How to use Reference Parameters in C++?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

151 Views

Here we will see how to pass reference of some variable in C++. Sometimes we call it as “Call by Reference”.The call by reference method of passing arguments to a function copies the reference of an argument into the formal parameter. Inside the function, the reference is used to access the actual argument used in the call. This means that changes made to the parameter affect the passed argument.To pass the value by reference, argument reference is passed to the functions just like any other value. So accordingly you need to declare the function parameters as reference types as in ... Read More

Static Data Member Initialization in C++

Anvi Jain
Updated on 30-Jul-2019 22:30:26

3K+ Views

Here we will see how to initialize the static member variables initialization in C++. We can put static members (Functions or Variables) in C++ classes. For the static variables, we have to initialize them after defining the class.To initialize we have to use the class name then scope resolution operator, then the variable name. Now we can assign some value.The following code will illustrate the of static member initializing technique.Example#include using namespace std; class MyClass{    private:       static int st_var;    public:       MyClass() {          st_var++; //increase the value of ... Read More

Advertisements