Found 7347 Articles for C++

C++ Program to Implement Disjoint Set Data Structure

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

3K+ Views

Disjoint set is basically as group of sets where no item can be in more than one set. It supports union and find operation on subsets.Find(): It is used to find in which subset a particular element is in and returns the representative of that particular set.Union(): It merges two different subsets into a single subset and representative of one set becomes representative of other.Functions and pseudocodesBegin    Assume k is the element    makeset(k):       k.parent = k.    Find(x):    If k.parent == k       return k.    else    return Find(k.parent)    Union ... Read More

C++ Program to Implement Direct Addressing Tables

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

490 Views

This is a C++ program to implement Direct Addressing Tables. Direct Addressing Tables are used when each element has a key drawn from a universal set S = {0, 1, . . . ,n − 1} where n isn’t too large and each key is unique. It facilitates fast insertion, searching and deletion operations.Functions and pseudocodesBegin    insert():       Take the table variables word and key as argument.       T[ x.key ] = x, where x is a value of key.    delete():       Take the table variables word and key as argument.   ... Read More

How are virtual functions implemented in C++?

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

589 Views

Virtual functions in C++ used to create a list of base class pointers and call methods of any of the derived classes without even knowing kind of derived class object. Virtual functions are resolved late, at runtime.Here is an implementation of virtual function in C++ program −Example#include using namespace std; class B {    public:       virtual void s() { //virtual function          cout

Why C++ does not have a virtual constructor?

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

3K+ Views

The virtual mechanism works only when we have a base class pointer to a derived class object.In C++, constructor cannot be virtual, because when constructor of a class is executed there is no virtual table in the memory, means no virtual pointer defined yet. So, the constructor should always be non-virtual.But virtual destructor is possible. Here is an exampleExample#include using namespace std; class b {    public:    b()    { cout

C++ program to Check if a Given Binary Tree is an AVL Tree or Not

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

2K+ Views

AVL tree is a self-balancing Binary Search Tree where the difference between heights of left and right subtrees cannot be more than one for all nodes.This is a C++ program to check if a given Binary Tree is an AVL Tree or not.AlgorithmBegin function AVL() returns true if the given tree is AVL otherwise false.    if(root == NULL)       return 1    leftheight = height(root->left)    rightheight = height(root->right)    if(abs(leftheight-rightheight) left) && AVL(root->right))       return 1    return 0 EndExample#include using namespace std; class nod { //node declaration    public:    int data; ... Read More

How to convert string to char array in C++?

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

1K+ Views

This is a C++ program to convert string to char array in C++. This can be done in multiple different waysType1AlgorithmBegin    Assign a string value to a char array variable m.    Define and string variable str    For i = 0 to sizeof(m)       Copy character by character from m to str.       Print character by character from str. EndExample#include #include using namespace std; int main() {    char m[]="Tutorialspoint";    string str;    int i;    for(i=0;i

C++ Program to Find Transitive Closure of a Graph

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

358 Views

If a directed graph is given, determine if a vertex j is reachable from another vertex i for all vertex pairs (i, j) in the given graph. Reachable mean that there is a path from vertex i to j. This reachability matrix is called transitive closure of a graph. Warshall algorithm is commonly used to find the Transitive Closure of a Given Graph G. Here is a C++ program to implement this algorithm.AlgorithmBegin    1. Take maximum number of nodes as input.    2. For Label the nodes as a, b, c…..    3. To check if there any edge ... Read More

Check if a given graph is Bipartite using DFS using C++

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

146 Views

A bipartite graph is a graph in which if the graph coloring is possible using two colors only i.e.; vertices in a set are colored with the same color. This is a C++ program to check whether a graph bipartite or not using DFS.AlgorithmBegin    An array color[] is used to stores 0 or 1 for every node which    denotes opposite colors.    Call function DFS from any node.    If the node w has not been visited previously, then assign !    color[v] to color[w] and call DFS again to visit nodes connected    to w.    If ... Read More

C++ Program to Create a Random Graph Using Random Edge Generation

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

869 Views

In this program a random graph is generated for random vertices and edges. The time complexity of this program is O(v * e). Where v is the number of vertices and e is the number of edges.AlgorithmBegin    Develop a function GenRandomGraphs(), with ‘e’ as the    number of edges and ‘v’ as the number of vertexes, in the argument list.       Assign random values to the number of vertex and edges of the graph,    using rand() function.       Print the connections of each vertex, irrespective of the       direction.       ... Read More

C++ program to perform unique factorization of a Given Number

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

114 Views

Here is a C++ Program to get all the unique factorization of a given integer such that addition of a partition results an integer. In this program, a positive integer n is given, and we shall generate all possible unique ways to represent n as sum of positive integers.AlgorithmBegin function displayAllUniqueParts(int m):    1) Set Index of last element k in a partition to 0    2) Initialize first partition as number itself, p[k]=m    3) Create a while loop which first prints current partition, then generates next partition. The loop stops when the current partition has all 1s.   ... Read More

Advertisements