Found 33676 Articles for Programming

C++ Program to Optimize Wire Length in Electrical Circuit

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

345 Views

This is a C++ Program to optimize Wire Length in Electrical Circuit.AlgorithmBegin    Function optimizeLength() :    1) Declare a array dist[N].    2) sptSet[i] will be true if component i is included in shortest    path tree or shortest distance from src to i is finalized.    3) Initialize all distances as INFINITE and stpSet[] as false    4) Distance of source component from itself will be always 0.    5) Run a for loop cnt = 0 to N-2, Find shortest path for all components.       A) Pick the minimum distance component from the set of ... Read More

C++ Program to Represent Linear Equations in Matrix Form

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

559 Views

This is a C++ program to represent Linear Equations in matrix form.AlgorithmBegin    1) Take the no of variables n and the coefficients of each variable as input.    2) Declare a matrix[n][n] and constant[n][1].    3) Make for loops i = 0 to n-1 and j = 0 to n-1    to take the coefficients of each variable as the elements of the matrix.    4) Display the matrix by using nested for loops. EndExample#include using namespace std; int main(void) {    char variable[] = { 'x', 'y', 'z', 'd' };    cout > n;    cout > matrix[i][j];       }       cin >> constant[i][0];    }    cout

C++ Program to Implement Gauss Seidel Method

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

4K+ Views

Gauss Seidel method is used to solve linear system of equations in iterative method. This is a C++ Program to Implement Gauss Seidel Method.AlgorithmBegin    Take the dimensions of the matrix p and its elements as input.    Take the initials values of x and no of iteration q as input.    While q>0       Make a for loop i = 0 to p-1          initialize n[i] = (b[i] / a[i][i]).             Make a for loop i = 0 to p-1             If (j == ... Read More

C++ Program to Implement Coppersmith Freivald’s Algorithm

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

527 Views

Freivalds' algorithm determines whether the matrices are equal for a chosen k value with a probability of failure less than 2^-k in O(kn^2).It is used to verify matrix multiplication.AlgorithmBegin    Take matrix1(n*n), matrix2(n*n), matrix3(n*n) as input.    // According to the algorithm we have to verify:    // matrix1 × matrix2 = matrix3.    1) Choose vector a[n][1] randomly and uniformly in which component will be 0 or 1.    2) Compute matrix2 * a, matrix3 * a and then matrix1 * (matrix2 * a) for evaluating the expression, matrix1 * (matrix2 * a) - matrix3 * a.    3) ... Read More

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

How are virtual functions implemented in C++?

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

713 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

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

515 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

209 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

Advertisements