Found 7346 Articles for C++

What is a segmentation fault in C/C++ program?

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

943 Views

A segmentation fault occurs when your program attempts to access an area of memory that it is not allowed to access. In other words, when your program tries to access memory that is beyond the limits that the operating system allocated for your program.Segmentation faults are mostly caused by pointers that are:Used to being properly initialized.Used after the memory they point to has been reallocated or freed.Used in an indexed array where the index is outside of the array bounds.

How to pass objects to functions in C++ Program?

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

8K+ Views

There are four ways of passing objects to functions. Let's assume you have a class X and want to pass it to a function fun, thenPass by ValueThis creates a shallow local copy of the object in the function scope. Things you modify here won't be reflected in the object passed to it. For example, Declarationvoid fun(X x);CallingX x; fun(x);Pass by ReferenceThis passes a reference to the object to the function. Things you modify here will be reflected in the object passed to it. No copy of the object is created. For example, Declarationvoid fun(X &x);CallingX x; fun(x);Pass by const ... Read More

How to detect integer overflow in C/C++?

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

290 Views

The only safe way is to check for overflow before it occurs. There are some hacky ways of checking for integer overflow though. So if you're aiming for detecting overflow in unsigned int addition, you can check if the result is actually lesser than either values added. So for example, Example Codeunsigned int x, y; unsigned int value = x + y; bool overflow = value < x; // Alternatively "value < y" should also workThis is because if x and y are both unsigned ints, if added and they overflow, their values can't be greater than either of them ... Read More

C++ Program to Find the Longest Increasing Subsequence of a Given Sequence

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

639 Views

Longest Increasing Subsequence is a subsequence where one item is greater than its previous item.Here we will try to find Longest Increasing Subsequence length, from a set of integers.Input: A set of integers. {0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15} Output: The length of longest increasing subsequence. Here it is 6. The subsequence is 0, 2, 6, 9, 13, 15.AlgorithmlongestSubSeq(subarray, n)Input: The sub array and the size of sub array.Output: Longest increasing sub sequence length.Begin    define array length of size n    initially set 0 to all entries of length ... Read More

C++ Program to Implement Jarvis March to Find the Convex Hull

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

925 Views

Jarvis March algorithm is used to detect the corner points of a convex hull from a given set of data points.Starting from left most point of the data set, we keep the points in the convex hull by anti-clockwise rotation. From a current point, we can choose the next point by checking the orientations of those points from current point. When the angle is largest, the point is chosen. After completing all points, when the next point is the start point, stop the algorithm.Input: Set of points: {(-7, 8), (-4, 6), (2, 6), (6, 4), (8, 6), (7, -2), (4, ... Read More

C++ Program to Implement Graham Scan Algorithm to Find the Convex Hull

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

3K+ Views

Convex hull is the minimum closed area which can cover all given data points.Graham's Scan algorithm will find the corner points of the convex hull. In this algorithm, at first the lowest point is chosen. That point is the starting point of the convex hull. Remaining n-1 vertices are sorted based on the anti-clock wise direction from the start point. If two or more points are forming same angle, then remove all points of same angle except the farthest point from start.From the remaining points, push them into the stack. And remove items from stack one by one, when orientation ... Read More

C++ Program to Implement Adjacency Matrix

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

13K+ Views

The adjacency matrix of a graph is a square matrix of size V x V. The V is the number of vertices of the graph G. In this matrix in each side V vertices are marked. If the graph has some edges from i to j vertices, then in the adjacency matrix at ith row and jth column it will be 1 (or some non-zero value for weighted graph), otherwise that place will hold 0.The complexity of Adjacency Matrix representation:The adjacency matrix representation takes O(V2) amount of space while it is computed. When graph has maximum number of edges and ... Read More

C++ Program to Implement Adjacency List

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

11K+ Views

The adjacency list representation of a graph is linked list representation. In this representation we have an array of lists The array size is V. Here V is the number of vertices. In other words, we can say that we have an array to store V number of different lists. If a list header is vertex u, then it signifies that it will hold all of the adjacent vertices of u.The complexity of Adjacency List representationThis representation takes O(V+2E) for undirected graph, and O(V+E) for directed graph. If the number of edges are increased, then the required space will also ... Read More

C++ Program to Implement Stack

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

9K+ Views

In this program we will see how to implement stack using C++. A stack is an abstract data structure that contains a collection of elements. Stack implements the LIFO mechanism i.e. the element that is pushed at the end is popped out first. Some of the principle operations in the stack are −Push - This adds a data value to the top of the stack.Pop - This removes the data value on top of the stackPeek - This returns the top data value of the stackA program that implements a stack using array is given as follows.Input: Push elements 11, ... Read More

C++ Program to Evaluate an Expression using Stacks

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

3K+ Views

For solving mathematical expression, we need prefix or postfix form. After converting infix to postfix, we need postfix evaluation algorithm to find the correct answer.Here also we have to use the stack data structure to solve the postfix expressions.From the postfix expression, when some operands are found, pushed them in the stack. When some operator is found, two items are popped from stack and then operation is performed in correct sequence. After that the result is also pushed in the stack for future use. After completing the whole expression, the final result is also stored in the stack top.Input: Postfix ... Read More

Advertisements