Found 7346 Articles for C++

What are copy elision and return value optimization in C++?

George John
Updated on 30-Jul-2019 22:30:25

201 Views

The Copy Elision is also known as the Copy Omission. This is one of the compiler optimization technique. It avoids the unnecessary copying of objects. Almost any current compiler uses this Copy Elision technique.Let us see how it works by the help of one example code.Example Code#include using namespace std; class MyClass { public: MyClass(const char* str = "\0") { //default constructor cout

How can I profile C++ code running on Linux?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

341 Views

In Linux platform there are many great profiling tools for profiling C++ programs. Valgrind is one of them. It is widely used. It is a programming tool for memory debugging, memory leak detection, and profiling. We can use Valgrind by passing the binary to it and setting the tool to callgrind. First generate the binary by compiling the program$ g++ -o abc.cpp abcNow use valgrind to profile it$ valgrind --tool=callgrind ./abcThis will generate a file called callgrind.out.x. You can read this file using a tool called kcachegrind.If you're using gcc, you can use the inbuilt profiling tool, gprof. You can ... Read More

Why is iostream::eof inside a loop condition considered wrong?

Chandu yadav
Updated on 30-Jul-2019 22:30:25

140 Views

The iostream::eof in a loop is considered as wrong because we haven’t reached the EOF. So it does not mean that the next read will succeed.When we want to read a file using file streams in C++. And when we use a loop to write in a file, if we check the end of file using stream.eof(), we are actually checking whether the file has reached end or not.Example Code#include #include using namespace std; int main() { ifstream myFile("myfile.txt"); string x; while(!myFile.eof()) { myFile >> ... Read More

C++ Program to Implement Sieve of eratosthenes to Generate Prime Numbers Between Given Range

Daniol Thomas
Updated on 30-Jul-2019 22:30:25

2K+ Views

This is C++ program to implement Sieve of Eratosthenes to Generate Prime Numbers Between Given Range. In this method, an integer array with all elements initializes to zero.It follows where each non-prime element’s index is marked as 1 inside the nested loops. The prime numbers are those whose index is marked as 0.AlgorithmBegin    Declare an array of size n and initialize it to zero    Declare length, i, j    Read length    For i = 2 to n-1 do       For j = i*i to n-1 do          Arr[j-1]=1       Done ... Read More

C++ Program to Implement Wheel Sieve to Generate Prime Numbers Between Given Range

Daniol Thomas
Updated on 30-Jul-2019 22:30:25

250 Views

Wheel Sieve method is used to find prime number between a given range. Wheel factorization is a graphical method for manually performing a preliminary to the Sieve of Eratosthenes that separates prime numbers from composites.In this method, Prime numbers in the innermost circle have their Multiples in similar positions as themselves in the other circles, forming spokes of primes and their multiples. Multiple of these prime numbers in the innermost circle form spokes of composite numbers in the outer circles.AlgorithmBegin    Define max number    gen_sieve_primes()    Declare c    Assign c = 2    For p = 2 to ... Read More

C++ Program to Emulate N Dice Roller

Nancy Den
Updated on 30-Jul-2019 22:30:25

890 Views

Here is the code to emulate N dice roller. This can be done by generating random number between 1-6.AlgorithmBegin    Declare n    Read n    For i = 0 to n-1 do       Generate sequence with rand() mod 6 + 1       Print the sequence    Done EndExample Code#include using namespace std; int main(int argc, char **argv) {    cout > n;    cout

C++ Program to Generate Random Numbers Using Multiply with Carry Method

Nancy Den
Updated on 30-Jul-2019 22:30:25

209 Views

The multiply-with-carry method is a variant of the add-with-carry generator introduced by Marsaglia and Zaman (1991). The main advantages of this method are that it invokes simple computer integer arithmetic and leads to very fast generation of sequences of random numbers with immense periods, ranging from around 260 to 22000000.In MWC base b is chosen to equal to the computer word size and multiplier a and lag r determine the modulus p = abr−1. Here, a is chosen so the modulus is prime and the multiplier has long period.AlgorithmBegin Declare maximum _sequence _elements, b, r, c[maximum _sequence ... Read More

C++ Program to Implement Naor-Reingold Pseudo Random Function

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

116 Views

Naor-Reingold Pseudo Random Function is another method of generating random numbers.Moni Naor and Omer Reingold described efficient constructions for various cryptographic primitives in private key as well as public-key cryptography, in 1997. Let p and l be prime numbers with l |p−1. Select an element g ε Fp* of multiplicative order l. Then for each n-dimensional vector a = (a0, a1, ..., an).They define the functionfa(x)=ga0.a1x1a2x2…..anxn ε Fpwhere x = x1 … xn is the bit representation of integer x, 0 ≤ x ≤ 2 n−1This function can be used as the basis of many cryptographic schemes including symmetric encryption, ... Read More

C++ Program to Implement Park-Miller Random Number Generation Algorithm

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

353 Views

Park-Miller Random Number Generation Algorithm is another method of generating random numbers.A general formula of a random number generator (RNG) of this type is: X_{k+1} = g X(k) mod nWhere the modulus n is a prime number or a power of a prime number, the multiplier g is an element of high multiplicative order modulo n, and the seed X0 is coprime to n.AlgorithmBegin    Declare variables n, a, b, c and seed    Read variables n, a, b, c and seed    Uniform()    Declare variable hi, lo, t    hi=seed divided by b    lo = seed - ... Read More

C++ Program to Generate Random Numbers Using Probability Distribution Function

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

1K+ Views

Probability Density Function (pdf), is a function that describes the relative likelihood for this random variable to take on a given value. It is also called as density of a continuous random variable.The probability of the random variable fall within a particular range of values is given by the integral of this variable’s density over that range, So, it is given by the area under the density function but above the horizontal axis and between the lowest and greatest values of the range. Probability Distribution is based upon this probability density function.AlgorithmBegin Declare n ... Read More

Advertisements