Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Articles - Page 666 of 719
648 Views
The Sieve of Atkin is a modern algorithm for finding all prime numbers up to a specified integer. It follows three simple steps to find the prime numbers. It uses three quadratic expressions that remove the composite numbers. After this, we remove the multiples of squares of already existing prime numbers and at the end, we return the prime numbers. In this article, we have set the limit to '30'. Our task is to implement the Sieve of Atkin method to generate all the prime numbers up to the limit in C++. Here is an example of prime numbers up ... Read More
911 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
347 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
485 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
209 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
3K+ Views
The Sieve of Eratosthenes algorithm is one of the most efficient ways to find prime numbers smaller than n when n is smaller than around 10 million. It follows a simple process of marking the multiples of already prime numbers as false i.e. non-prime numbers. In this article, we have a given number as 'num'. Our task is to find all the prime numbers less than or equal to num using the Sieve of Eratosthenes algorithm in C++. Example Here is an example to find prime numbers less than 10: Input: num = 10 Output: 2 3 5 7 ... Read More
472 Views
The wheel Sieve method is used to find the prime numbers within 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. In this article, we have set the limit to 100. Our task is to implement the ... Read More
1K+ Views
Emulating an n dice roller refers to rolling n number of dice simultaneously. In each roll, all the dice will return a different value from 1 to 6. In this article, we are having 'n' number of dices, our task is to emulate rolling n dices simultaneously. The approaches are mentioned below: Using Loop with rand() Function Using Recursion Using Loop with rand() Function In this approach, we have used the rand() function to generate a random value of dice in each roll. The srand() function with time() is ... Read More
390 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 a very fast generation of sequences of random numbers with immense periods, ranging from around 260 to 22000000. In this article, our task is to generate random numbers using the multiply-with-carry method. Here is the formula of multiply-with-carry method: Multiply With Carry (MWC) Formula The multiply-with-carry formula is as follows: Xn = (a * Xn-1 + Cn-1) mod 232 Cn = (a * Xn-1 + Cn-1) ... Read More
230 Views
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