C++ Articles

Page 588 of 597

Random number generation in C++

Vrundesha Joshi
Vrundesha Joshi
Updated on 30-Jul-2019 358 Views

Let us see how to generate random numbers using C++. Here we are generating a random number in range 0 to some value. (In this program the max value is 100).To perform this operation we are using the srand() function. This is in the C library. The function void srand(unsigned int seed) seeds the random number generator used by the function rand.The declaration of srand() is like below:void srand(unsigned int seed)It takes a parameter called seed. This is an integer value to be used as seed by the pseudo-random number generator algorithm. This function returns nothing.To get the number we ...

Read More

Is there a performance difference between i++ and ++i in C++ program?

Jennifer Nicholas
Jennifer Nicholas
Updated on 30-Jul-2019 2K+ Views

The effective result of i++ and ++i are same. The only difference is that the i++ increases the value of i after assigning it, and for ++i, it increases the value first, then assigns its value. We can see the difference in the following code.Example Code#include using namespace std; int main() {    int x = 3, y, z;    y = x++;    z = ++x;    cout

Read More

How to Parse Command Line Arguments in C++?

Nitya Raut
Nitya Raut
Updated on 30-Jul-2019 642 Views

It is possible to pass some values from the command line to your C++ programs when they are executed. These values are called command line arguments and many times they are important for your program especially when you want to control your program from outside instead of hard-coding those values inside the code.The command line arguments are handled using main() function arguments where argc refers to the number of arguments passed, and argv[] is a pointer array which points to each argument passed to the program. Following is a simple example which checks if there is any argument supplied from ...

Read More

How to generate different random numbers in a loop in C++?

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

Let us see how to generate different random numbers using C++. Here we are generating random numbers in range 0 to some value. (In this program the max value is 100).To perform this operation we are using the srand() function. This is in the C++ library. The function void srand(unsigned int seed) seeds the random number generator used by the function rand.The declaration of srand() is like below −void srand(unsigned int seed)It takes a parameter called seed. This is an integer value to be used as seed by the pseudo-random number generator algorithm. This function returns nothing.To get the number ...

Read More

C/C++ Struct vs Class

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

In C++ the structure and class are basically same. But there are some minor differences. These differences are like below.The class members are private by default, but members of structures are public. Let us see these two codes to see the differences.Example#include using namespace std; class my_class {    int x = 10; }; int main() {    my_class my_ob;    cout

Read More

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

Arjun Thakur
Arjun Thakur
Updated on 30-Jul-2019 335 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

C++ Program to Apply DFS to Perform the Topological Sorting of a Directed Acyclic Graph

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

Topological sorting of DAG (Directed Acyclic Graph) is a linear ordering of vertices such that for every directed edge uv, where vertex u comes before v in the ordering. If the graph is not a DAG, Topological Sorting for a graph is not possible.Functions and pseudocodesBegin    function topologicalSort():    a) Mark the current node as visited.    b) Recur for all the vertices adjacent to this vertex.    c) Push current vertex to stack which stores result. End Begin    function topoSort() which uses recursive topological sort() function:    a) Mark all the vertices which are not visited.   ...

Read More

C++ Program to Check Whether a Hamiltonian Cycle or Path Exists in a Given Graph

Paul Richard
Paul Richard
Updated on 30-Jul-2019 1K+ Views

A Hamiltonian cycle is a Hamiltonian Path such that there is an edge (in graph) from the last vertex to the first vertex of the Hamiltonian Path. It is in an undirected graph is a path that visits each vertex of the graph exactly once.Functions and purposes:Begin    1.function isSafe() is used to check for whether it is adjacent to the previously added vertex and already not added.    2. function hamiltonianCycle() solves the hamiltonian problem.    3. function hamCycle() uses hamiltonianCycle() to solve the hamiltonian problem.       It returns false if there is no Hamiltonian Cycle possible, ...

Read More

Convert C/C++ program to Preprocessor code\\n

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

Here we will see how to generate the preprocessed or preprocessor code from the source code of a C or C++ program.To see the preprocessed code using g++ compiler, we have to use the ‘-E’ option with the g++.Preprocessor includes all of the # directives in the code, and also expands the MACRO function.Syntaxg++ -E program.cppExample#define PI 3.1415 int main() {    float a = PI, r = 5;    float c = a * r * r;    return 0; }Output$ g++ -E test_prog.cpp int main() {    float a = 3.1415, r = 5;    float c = a * r * r;    return 0; }

Read More

C++ Program to Check Whether it is Weakly Connected or Strongly Connected for a Directed Graph

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

Weakly or Strongly Connected for a given a directed graph can be find out using DFS. This is a C++ program of this problem.Functions usedBegin    Function fillorder() = fill stack with all the vertices.    a) Mark the current node as visited and print it    b) Recur for all the vertices adjacent to this vertex    c) All vertices reachable from v are processed by now, push v to Stack End Begin    Function DFS() :    a) Mark the current node as visited and print it    b) Recur for all the vertices adjacent to this vertex ...

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