Found 7401 Articles for C++

C++ Program to Implement Traveling Salesman Problem using Nearest Neighbour Algorithm

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

1K+ Views

Here is a C++ Program to Implement Traveling Salesman Problem using Nearest Neighbour Algorithm.Required functions and pseudocodesAlgorithmBegin    Initialize c = 0, cost = 1000;    Initialize g[][].    function swap() is used to swap two values x and y.    function cal_sum() to calculate the cost which take array a[] and size of array as input.    Initialize sum = 0.    for i = 0 to n       compute s+= g[a[i %3]][a[(i+ 1) %3]];    if (cost >s)       cost = s    function permute() is used to perform permutation:       if ... Read More

C++ Program to Implement Nearest Neighbour Algorithm

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

2K+ Views

This is a C++ program to implement Nearest Neighbour Algorithm which is used to implement traveling salesman problem to compute the minimum cost required to visit all the nodes by traversing across the edges only once.Required functions and pseudocodesAlgorithmBegin    Initialize c = 0, cost = 1000;    Initialize g[][].    function swap() is used to swap two values x and y.    function cal_sum() to calculate the cost which take array a[] and size of array as input.    Initialize sum =0.    for i = 0 to n       compute s+= g[a[i %3]][a[(i+ 1) %3]];   ... Read More

Program to find the Area of an Ellipse using C++

Nitya Raut
Updated on 30-Jul-2019 22:30:25

395 Views

Here we will see how to get the area of an ellipse using C++. The Ellipse has different parts. These are like below.Key-pointsDescriptionCenterThe center of the ellipse. It is also center of the line segments which links two foci.Major AxisThe longest diameter of an ellipsenmembThis is the number of elements, each one with a size of size bytes.Minor AxisThe smallest diameter of an ellipseChordThe line segment which points tFocusTwo points that are pointed in the diagramLotus RectumThe lotus rectum is a line passes through the focus and perpendicular to the major axis of an ellipseThe area of an ellipse is ... Read More

C++ Program to Construct Transitive Closure Using Warshall’s Algorithm

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

685 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 reach-ability 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 present between ... Read More

C++ Program to Implement Queue

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

3K+ Views

QueueThe queue which is implemented as FIFO where insertions are done at one end (rear) and deletions are done from another end (front). The first element that entered is deleted first.Queue operations are −EnQueue (int data) − Insertion at rear endint DeQueue()− Deletion from front endThis is a C++ program to implement queue using array.AlgorithmBegin    function Enqueue() to insert elements in queue:       If queue is completely filled up then print “Overflow”.       Otherwise insert element at rear.       Update the value of rear End Begin    function Dequeue() to delete elements from ... Read More

fread() function in C++

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

345 Views

The C/C++ library function size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) reads data from the given stream into the array pointed to, by ptr. Following is the declaration for fread() function.size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)The Following table contains the fread() parameters and description:ParametersDescriptionptrThis is the pointer to a block of memory with a minimum size of size*nmemb bytes.sizeThis is the size in bytes of each element to be read.nmembThis is the number of elements, each one with a size of size bytes.streamThis is the pointer to a FILE object that specifies an input stream.The ... Read More

C++ Program to Implement Dequeue

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

9K+ Views

Dequeue or Double Ended Queue is a generalized version of Queue data structure that allows insert and delete at both ends.Some basic operations of dequeue are −insert_at_beg(): inserts an item at the front of Dequeue.insert_at_end(): inserts an item at the rear of Dequeue.delete_fr_beg(): Deletes an item from front of Dequeue.delete_fr_rear(): Deletes an item from rear of Dequeue.Following is a C++ program to implement DequeueAlgorithmBegin    Declare a class dequeue to declare front f and rear r and following functions:    function insert_at_beg(int) to insert item at front:       If queue is not completely filled up, insert element at ... Read More

C++ Program to Implement Stack Using Two Queues

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

887 Views

StackThe stack which is implemented as LIFO, where insertion and deletion are done from the same end, top. The last element that entered is deleted first.Stack operations are −push (int data) − Insertion at topint pop() − Deletion from topQueueThe queue which is implemented as FIFO where insertions are done at one end (rear) and deletions are done from another end (front). The first element that entered is deleted first.Queue operations are −EnQueue (int data) − Insertion at the rear endint DeQueue() − Deletion from front endThis is a C++ Program to Implement Stack Using Two QueuesAlgorithmsBegin    function enqueue1 to ... Read More

cout << endl vs cout << “” in C++

Nitya Raut
Updated on 30-Jul-2019 22:30:25

889 Views

In this section we will see what are the differences between cout

C++ Program to Implement Queue Using Two Stacks

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

1K+ Views

StackThe stack which is implemented as LIFO, where insertion and deletion are done from the same end, top. The last element that entered is deleted first.Stack operations are −push (int data) − Insertion at topint pop() − Deletion from topQueueThe queue which is implemented as FIFO where insertions are done at one end (rear) and deletions are done from another end (front). The first element that entered is deleted first.Queue operations are −EnQueue (int data) − Insertion at rear endint DeQueue() − Deletion from front endThis is a C++ Program to Implement Queue Using Two Stacks.Description of functionsfunction enQueue() to ... Read More

Advertisements