Found 7401 Articles for C++

fork() to execute processes from bottom to up using wait() in C++

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

2K+ Views

We know that the fork() system call is used to divide the process into two processes. If the function fork() returns 0, then it is child process, and otherwise it is parent process.In this example we will see how to split processes four times, and use them in bottom up manner. So at first we will use fork() function two times. So it will generate a child process, then from the next fork it will generate another child. After that from the inner fork it will automatically generates a grandchild of them.We will use wait() function to generate some delay ... Read More

Why we should avoid using std::endl in C++

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

556 Views

In this section we will see why we should avoid the std::endl while printing lines into console or a file. We use std::endl for creating a newline after the current line. For few lines of IO operations, it is not making any problems. But for large amount of IO tasks, it decreases the performance.The endl is used to create new lines, but it does not send to the new line only, after sending the cursor to the next line it flushes the buffer each time.The flushing of buffers is not the programmers task; the operating system is responsible for it. ... Read More

Calculate range of data types using C++

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

3K+ Views

In C++ we have different datatypes like int, char, double etc. In this section we will see how to get the size of them programmatically.We can get the size of datatypes in byte, so we can simply multiply them into 8 to get the values in bits. Now we know that if the number of bits are n, then the minimum range will be – 2^(n-1), and maximum range will be 2^(n-1) – 1 for signed numbers. For unsigned numbers it will be 2^n – 1 as there are no negative numbers.Example Code#include #include #define SIZE(x) sizeof(x) * ... Read More

Print a character n times without using loop, recursion or goto in C++

Nitya Raut
Updated on 30-Jul-2019 22:30:25

887 Views

In this section we will see how to print a character n times without using loops and recursion in C++. We can solve this problem by using string class constructors. There is a constructor where we are taking the character that will be printed multiple times, and the number of times it will be printed.Example Code#include using namespace std; void print_char_n_times(char my_char, int count) {    cout

C++ Program to Construct an Expression Tree for a given Prefix Expression

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

4K+ Views

An expression tree is basically a binary tree which is used to represent expressions. In an expression tree, internal nodes correspond to operators and each leaf nodes correspond to operands. Here is a C++ program to construct an expression tree for a prefix Expression in inorder, preorder and postorder traversals.AlgorithmBegin    class ExpressionTree which has following functions:    function push() to push nodes into the tree:    If stack is null       then push the node as first element    Else       push the node and make it top        function pop() to pop ... Read More

Array class in C++

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

3K+ Views

Array class in C++ are efficient enough and also it knows its own size.Functions used to perform operations on array aresize() = To return the size of array i.e. returns the no of elements of the array.max_size() = To return maximum number of elements of the array.get(), at(), operator[] = To get access of the array elements.front() = To return front element of the array.back() = To return last element of the array.empty() = Returns true if array size is true otherwise false.fill() = To fill the entire array with a particular value.swap() = To swap the elements of one ... Read More

Difference between std::vector and std::array in C++

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

21K+ Views

The following are the differences between vector and array −Vector is a sequential container to store elements and not index based.Array stores a fixed-size sequential collection of elements of the same type and it is index based.Vector is dynamic in nature so, size increases with insertion of elements.As array is fixed size, once initialized can’t be resized.Vector occupies more memory.Array is memory efficient data structure.Vector takes more time in accessing elements.Array access elements in constant time irrespective of their location as elements are arranged in a contiguous memory allocation.Vectors and arrays can be declared with the following syntax −Vector declaration:vectorarray ... Read More

Difference between namespace and class in C++

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

5K+ Views

In this section we will see what are the differences between namespace and class in C++. The namespace and classes are two different concepts. Classes are datatypes. Classes are basically extended version of structures. Classes can contain data members and functions as members, but namespaces can contain variables and functions by grouping them into one.The namespaces cannot be created as objects. This concept is used as additional information to differentiate similar functions, classes, variables etc. Variables, functions with same name can be placed in different namespaces.Now let us point out some important differences of namespaces and classes.The namespaces are used ... Read More

C++ Program to implement Gift Wrapping Algorithm in Two Dimensions

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

381 Views

We shall develop a C++ program to implement Gift Wrapping Algorithm in two dimensions. The Gift Wrapping Algorithm is an algorithm for computing the convex hull of a given set of points.AlgorithmBegin    function convexHull() to find convex hull of a set of n points:    There must be at least three points.    Initialize the result.    Find the leftmost point.    Starting from leftmost point, keep moving counterclockwise    until reach the start point again.    Print the result. EndExample Code Live Demo#include using namespace std; #define INF 10000 struct P {    int x;    int y; ... Read More

C++ Program to Check if a Point d lies inside or outside a circle defined by Points a, b, c in a Plane

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

201 Views

We shall consider a C++ Program to check if a point d lies inside or outside a circle defined by points a, b, c in a plane by using equations = (x-xt)^2 + (y-yt)^2 – r*rWhere, For any point t (xt, yt) on the plane, its position with respect to the circle defined by 3 points (x1, y1), (x2, y2), (x3, y3).for s < 0, t lies inside the circle.For s >0, t lies outside the circle.For s = 0, t lies on the circle.AlgorithmBegin    Take the points at input.    Declare constant L = 0 and H = ... Read More

Advertisements