Found 7346 Articles for C++

What's the difference between assignment operator and copy constructor in C++?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

564 Views

The Copy constructor and the assignment operators are used to initialize one object to another object. The main difference between them is that the copy constructor creates a separate memory block for the new object. But the assignment operator does not make new memory space. It uses reference variable to point to the previous memory block.Copy Constructor (Syntax)classname (const classname &obj) { // body of constructor }Assignment Operator (Syntax)classname Ob1, Ob2; Ob2 = Ob1;Let us see the detailed differences between Copy constructor and Assignment Operator.Copy ConstructorAssignment OperatorThe Copy constructor is basically an overloaded constructorAssignment operator is basically ... Read More

Pre & post increment operator behavior in C, C++, Java, and C#

George John
Updated on 30-Jul-2019 22:30:25

3K+ Views

The Pre increment and post increment both operators are used as increment operations. The pre increment operator is used to increment the value of some variable before using it in an expression. In the pre increment the value is incremented at first, then used inside the expression.if the expression is a = ++b; and b is holding 5 at first, then a will hold 6. Because increase b by 1, then set the value of a with it.Example Code#include using namespace std; main () { int a, b = 15; a = ++b; cout

How do I use the conditional operator in C/C++?

Chandu yadav
Updated on 30-Jul-2019 22:30:25

213 Views

This conditional operator is also known as the Ternary Operator. This operator has three phase.Exp1 ? Exp2 : Exp3;where Exp1, Exp2, and Exp3 are expressions. Notice the use and placement of the colon. The value of a ? expression is determined like this: Exp1 is evaluated. If it is true, then Exp2 is evaluated and becomes the value of the entire ? expression. If Exp1 is false, then Exp3 is evaluated and its value becomes the value of the expression.The ? is called a ternary operator because it requires three operands and can be used to replace if-else statements, which ... Read More

C++ Program to Find Transpose of a Graph Matrix

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

249 Views

In this program we take a matrix and prints the transpose of the matrix. In a transpose matrix, rows become columns and vice versa.AlgorithmBegin Take number of rows and columns of the matrix. Take The elements of the matrix and stored in the matrix ‘A’. The transpose matrix is found by exchanging the rows with columns and columns with rows. Print both the original matrix and the transpose. End.Example Code#include using namespace std; int main () {    int A[10][10], a, b, i, j;    cout > a>> b;    cout > A[i][j];       cout

C++ Program to Represent Graph Using 2D Arrays

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

1K+ Views

This is a C++ program represents a graph using 2D arrays.The time complexity of this algorithm is O(v*v).AlgorithmBegin Take the input of the number of vertex ‘v’ and edges ‘e’. Assign memory to the graph[][] matrix. Take the input of ‘e’ pairs of vertexes of the given graph in graph[][]. For each pair of connected vertex(v1, v2), store 1 in the graph[][] at the index (v1, v2) and (v2, v1). Print the matrix using PrintMatrix(). EndExample Code#include #include using namespace std; void PrintMatrix(int **matrix, int n) {    int i, j;       cout

C++ Program to Represent Graph Using Incidence List

George John
Updated on 30-Jul-2019 22:30:25

220 Views

This program represents a graph using incidence list and the time complexity of this algorithm is O(e).AlgorithmBegin Take the input of the number of vertex ‘v’ and edges ‘e’ and also take the input of ‘e’ pairs of vertexes of the given graph in e[][]. For each edge print the corresponding vertex involved in that connection. EndExample Code#include using namespace std; int main() { int i, v, e, j, c; coutv; coute; int edge[e][2]; for(i = 0; i < e; i++) { cout

C++ Program to Generate a Graph for a Given Fixed Degree Sequence

Chandu yadav
Updated on 30-Jul-2019 22:30:25

195 Views

This is a C++ program represents a undirected graph for the given degree sequence. The time complexity of this algorithm is O(v*v) and it does not include self-edge and multiple edges.AlgorithmBegin    To create the graph,    Create the first loop to connect each vertex ‘i’.    Create second nested loop to connect the vertex ‘i’ to every valid vertex ‘j’, next to it.    If the degree of vertex ‘i’ and ‘j’ are more than zero, then connect them.    Print the adjacency matrix using PrintMatrix(). EndExample Code#include #include using namespace std; void PrintMatrix(int matrix[][20], int n) {   ... Read More

C++ Program to Generate a Random Directed Acyclic Graph DAC for a Given Number of Edges

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

436 Views

In this program we generate a random directed acyclic graph for the given edges ‘e’. The time complexity of this program is O(e*v*e).AlgorithmBegin    function GenerateRandomGraphs(), has ‘e’ as the number edges in the argument list.    generate a connection between two random numbers, for sample a small case, limit the number of vertex to 20.    Check for the cycle in the graph Using CheckAcyclic(),    if it returns false discard this edge.       Else maintain the edge in the graph.    Print all the directed connection of each vertex.    If the in+out degree of each ... Read More

C++ Program to Find the peak element of an array using Binary Search approach

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

769 Views

In this C++ program, we find out one of the peaks in the array can be found Using binary search approach. This algorithm returns the first peak found as a result with time complexity of the algorithm is O(log(n)).AlgorithmBegin    PeakElement() function has ‘arr’ the array of data, start and end index in the argument list.    Assign the mid of subpart of the array.    If mid is at the boundary index and value at mid is higher than its neighbor then return mid as peak.    If the value at mid is greater than both of its neighbors ... Read More

C++ Program to Find the Minimum element of an Array using Binary Search approach

George John
Updated on 30-Jul-2019 22:30:25

175 Views

This is a C++ Program to find the minimum element of an array using Linear Search approach. The time complexity of this program is O(log(n)).AlgorithmBegin Construct binary search tree for the given unsorted data array. To find out the minimum element move the pointer to the leftmost child node. Print this value as minimum value among the given data. EndExample Code#include using namespace std; struct node {    int d;    node *left;    node *right; }; node* CreateNode(int d) {    node *newnode = new node;    newnode->d = d; ... Read More

Advertisements