C++ Articles

Page 589 of 597

C++ Program to Find SSSP (Single Source Shortest Path) in DAG (Directed Acyclic Graphs)

Arjun Thakur
Arjun Thakur
Updated on 30-Jul-2019 2K+ Views

This is a C++ program to find SSSP (Single Source Shortest Path) in DAG (Directed Acyclic Graphs) using Dijkstra Algorithm to find out from the first node in graph to every other node with the shortest path length showed beside each pair of vertices.AlgorithmBegin    Take the elements of the graph as input.    function shortestpath():    Initialize the variables    a[i] = 1    d[i] = 0    s[i].from = 0    Initialize a loop for i = 0 to 3 do       if b[0][i] == 0          continue       else   ...

Read More

C++ Program to Find the Edge Connectivity of a Graph

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

In this program we need to find the Edge Connectivity of a Graph. An Edge Connectivity of a Graph of a graph means it is a bridge, removing it graph will be disconnected. Number of connected components increases with the removing of bridge in a disconnected undirected graph.Functions and pseudocode:Begin    Function connections() is a recursive function to find out the connections:    A) Mark the current node un visited.    B) Initialize time and low value    C) Go through all vertices adjacent to this    D) Check if the subtree rooted with x has a connection to one ...

Read More

Compile 32-bit program on 64-bit gcc in C and C++

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

Nowadays the compiler comes with default 64-bit version. Sometimes we need to compile and execute a code into some 32bit system. In that time, we have to use thisS feature.At first, we Shave to check the current target version of the gcc compiler. To check this, we have to type this command.gcc –v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/7/lto-wrapper OFFLOAD_TARGET_NAMES=nvptx-none OFFLOAD_TARGET_DEFAULT=1 Target: x86_64-linux-gnu ........... ........... ...........Here it is showing that Target is x86_64. So we are using the 64-bit version of gcc. Now to use the 32-bit system, we have to write the following command.gcc –m32 program_name.cSometimes this command may generate ...

Read More

C++ Program to Generate All Subsets of a Given Set in the Lexico Graphic Order

Paul Richard
Paul Richard
Updated on 30-Jul-2019 518 Views

This is C++ Program to Generate All Subsets of a Given Set in the Lexico Graphic Order. This algorithm prints all the possible combination of each length from the given set of array in increasing order. The time complexity of this algorithm is O(n*(2^n)).AlgorithmBegin    For each length ‘i’ GenAllSubset() function is called:    1) In GenAllSubset(), if currLen is more than the reqLen then return.    2) Otherwise, if currLen is equal to reqLen then there will be a new sequence generated, print it.    3) If proceed with a start as ‘true’ and recursively call GenAllSubset() with incremented ...

Read More

Type difference of character literals in C vs C++

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

In C++ the size of the character constants is char. In C the type of character constant is integer (int). So in C the sizeof(‘a’) is 4 for 32bit architecture, and CHAR_BIT is 8. But the sizeof(char) is one byte for both C and C++.Example#include main() {    printf("%d", sizeof('a')); }Output4Example#include using namespace std; main(){    cout

Read More

C++ Program to Perform Edge Coloring of a Graph

Paul Richard
Paul Richard
Updated on 30-Jul-2019 726 Views

In this program, we will perform Edge Coloring of a Graph in which we have to color the edges of the graph that no two adjacent edges have the same color. Steps in Example.AlgorithmBegin    Take the input of the number of vertices, n, and then number of edges, e, in the graph.    The graph is stored as adjacency list.    BFS is implemented using queue and colors are assigned to each edge. EndExample#include using namespace std; int n, e, i, j; vector g; vector color; bool v[111001]; void col(int n) {    queue q;    int c = ...

Read More

Difference between Structures in C and C++

Smita Kapse
Smita Kapse
Updated on 30-Jul-2019 593 Views

Here we will see what are the differences between structures in C and structures in C++. The C++ structures are mostly like classes in C++. In C structure, all members are public, but in C++, they are private in default. Some other differences are listed below.C StructureC++ StructureStructures in C, cannot have member functions inside structures.Structures in C++ can hold member functions with member variables.We cannot initialize the structure data directly in C.We can directly initialize structure data in C++.In C, we have to write ‘struct’ keyword to declare structure type variables.In C++, we do not need to use ‘struct’ ...

Read More

C++ Program to Perform Edge Coloring on Complete Graph

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

A complete graph is a graph which has a connecting edge between any pair of vertices. This is a C++ Program to Perform Edge Coloring on Complete Graph.AlgorithmBegin    Take the input of the number of vertices ‘n’.    Construct a complete graph using e=n*(n-1)/2 edges, in ed[][].    Function EdgeColor() is used to Color the graph edges.    A) Assign color to current edge as c i.e. 1 initially.    B) If the same color is occupied by any of the adjacent edges, then       discard this color and go to flag again and try next color. ...

Read More

Foreach in C++ vs Java

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

In C++ and Java, there is another kind of loop, called the foreach loop. This is basically a modification of for loop. This loop is used to access the data from some container. This can access the elements of some array quickly without performing initialization. This loop is used to do something for each element of a container, not doing things n times.Now let us see how the foreach loop is used in C++ and Java.Example#include using namespace std; int main() {    int arr[] = { 11, 22, 33, 44, 55, 66, 77, 88, 99 };    for (int a : arr) //foreach loop    cout

Read More

Convert C/C++ code to assembly language

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

Here we will see how to generate assembly language output from C or C++ source code using gcc.The gcc provides a great feature to get all intermediate outputs from a source code while executing. To get the assembler output we can use the option ‘-S’ for the gcc. This option shows the output after compiling, but before sending to the assembler. The syntax of this command is like below.gcc –S program.cppNow, let us see how to output will be look like. Here we are using a simple program. In this program two numbers are stored into the variables x and ...

Read More
Showing 5881–5890 of 5,961 articles
« Prev 1 587 588 589 590 591 597 Next »
Advertisements