- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 7299 Articles for C++

516 Views
The size of a struct type element taken by sizeof() is not always equal to the size of each individual member. Sometimes the compilers add some padding to avoid alignment issues. So the size may change. The padding is added when a structure member is followed by a member with a larger size or at the end of the structure. Different compiler has different types of alignment constraints. In C standard, the total alignment structure depends on the implementation.Case 1In this case the double z is 8-byte long, which is larger than x (4-byte). So another 4-byte padding is added. ... Read More

124 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

261 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

110 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

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

199 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

641 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

142 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

73 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

261 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