C++ Articles

Page 588 of 597

C++ set for user define data type

Anvi Jain
Anvi Jain
Updated on 30-Jul-2019 953 Views

Here we will see how we can make a set for user defined datatypes. The Set is present in C++ STL. This is a special type of data structure, it can store data in sorted order, and does not support duplicate entry. We can use set for any type of data, but here we will see how we can also use set for userdefined datatypes.To use user-defined datatypes into stack, we have to override < operator, that can compare two values of that type. If this is not present, it cannot compare two objects, so the set cannot store data ...

Read More

C++ Program to Perform Greedy Coloring

Daniol Thomas
Daniol Thomas
Updated on 30-Jul-2019 890 Views

Here is a C++ Program to Perform Greedy ColoringAlgorithm:Begin    Take the number of vertices and edges as input.    Create function greedyColoring() to assign color to vertices:    A) Assign the first color to first vertex.    B) Initialize the remaining vertices.    C) Declare a temporary array to store the available colors.    D) Assign color to the remaining vertices.    Print the solution. EndExample Code#include #include using namespace std; int n,e,i,j; vector g; vector col; bool visit[1001]; void greedyColoring() {    col[0] = 0;    for (i=1;i

Read More

Constructor Delegation in C++

Anvi Jain
Anvi Jain
Updated on 30-Jul-2019 1K+ Views

Here we will see what is the constructor delegation? When a constructor calls other constructor of the same class, then it is called the constructor delegation. This feature is present from C++11.Let us see the following program, and try to analyze what are the difficulties in this code.Example#include using namespace std; class MyClass {    int a, b, c;    public:    MyClass(){       a = b = c = 0;    }    MyClass(int c) {       // Initializing a and b are redundent, only c initialization is needed here       a = 0;       b = 0;       this->c = c;    }    void display(){       cout

Read More

C++ Program to Implement Network_Flow Problem

Daniol Thomas
Daniol Thomas
Updated on 30-Jul-2019 666 Views

This is a C++ Program to implement Network_Flow problem using Ford Fulkerson algorithm.Algorithms:Begin    function bfs() returns true if there is path from source s to sink t in    the residual graph which indicates additional possible flow in the graph. End Begin    function fordfulkarson() return maximum flow in given graph:    A) initiate flow as 0.    B) If there is an augmenting path from source to sink, add the path to flow.    C) Return flow. EndExample Code#include #include #include #include #define n 7 using namespace std; bool bfs(int g[n][n], int s, int ...

Read More

Using return value of cin to take unknown number of inputs in C++

Smita Kapse
Smita Kapse
Updated on 30-Jul-2019 1K+ Views

Sometimes we need variable number of inputs in our program. In this program we will see how to use cin to take variable number of inputs.The simple solution is run a loop, and when one specific value is pressed, it stops. The another idea is using the cin >> input. This will return false when value is non-numeric.Example#include using namespace std; main() {    int input;    int n = 0;    cout > input)    n++;    cout

Read More

C++ Program to Implement The Edmonds-Karp Algorithm

Daniol Thomas
Daniol Thomas
Updated on 30-Jul-2019 4K+ Views

This is a C++ Program to Implement the Edmonds-Karp algorithm to calculate maximum flow between source and sink vertex.Algorithm:Begin    function edmondsKarp() :       initiate flow as 0.       If there is an augmenting path from source to sink, add the path to flow.       Return flow. EndExample Code#include #include #include #include #include using namespace std; int c[10][10]; int flowPassed[10][10]; vector g[10]; int parList[10]; int currentPathC[10]; int bfs(int sNode, int eNode)//breadth first search {    memset(parList, -1, sizeof(parList));    memset(currentPathC, 0, sizeof(currentPathC));    queue q;//declare queue vector    q.push(sNode);    parList[sNode] = -1;//initialize parlist’s ...

Read More

C++ Program to Implement First Fit Decreasing for 1-D Objects and M Bins

Nancy Den
Nancy Den
Updated on 30-Jul-2019 214 Views

Here is a C++ Program to implement First Fit Decreasing for 1-D objects and M binsRequired functions and pseudocode:Begin    function binPack() returns number of bins required.    Initialize binC = 0    Initialize an array to store binVal.    Place items one by one.    function sort() to perform bubble sort in the descending order. EndExample Code#include using namespace std; void binPack(int *a, int s, int n) {    int binC = 0;    int binVal[n];    for (int i = 0; i < n; i++)    binVal[i] = s;    for (int i = 0; i < ...

Read More

Is there any need of &ldquo;long&rdquo; data type in C and C++?

Nishtha Thakur
Nishtha Thakur
Updated on 30-Jul-2019 215 Views

In C or C++, there are four different datatypes, that are used for integer type data. These four datatypes are short, int, long and long long. Each of these datatypes takes different memory spaces. The size varies in different architecture and different operating systems. Sometimes int takes 4-bytes or sometimes it takes 2-bytes. This also happen for the compilers. So we can use cross compilers.The cross compilers are basically a compiler, which is capable of compiling for a platform other than current platform.So if we want to compile the following code in 32bit system, and 64-bit system, it will generate ...

Read More

C++ Program to Check whether Graph is a Bipartite using DFS

Arjun Thakur
Arjun Thakur
Updated on 30-Jul-2019 331 Views

A bipartite graph is a graph in which if the graph coloring is possible using two colors 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    1. An array color[] is used to stores 0 or 1 for every node which denotes opposite colors.    2. Call function DFS from any node.    3. 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. ...

Read More

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

Nishtha Thakur
Nishtha Thakur
Updated on 30-Jul-2019 2K+ 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

Read More
Showing 5871–5880 of 5,961 articles
« Prev 1 586 587 588 589 590 597 Next »
Advertisements