Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
C++ Articles
Page 281 of 597
Why do we pass a Pointer by Reference in C++?
If we need to modify a pointer rather than the object that the pointer is pointing to, we pass a pointer by reference.Here is an example of how to pass a pointer by reference −Example#include using namespace std; void Decrement( int*& d ) { --d; } int main( void ) { int a = 26; int* ptr = &a; // pointer to pass // print before decrement cout
Read MoreC++ Program to Find the maximum subarray sum using Binary Search approach
Binary search is a fast search algorithm with run-time complexity of Ο(log n). This search algorithm works on the principle of divide and conquer. For this algorithm to work properly, the data collection should be in the sorted form.Binary search looks for a particular item by comparing the middle most item of the collection. If a match occurs, then the index of item is returned. If the middle item is greater than the item, then the item is searched in the sub-array to the left of the middle item. Otherwise, the item is searched for in the sub-array to the ...
Read MoreC++ Program to Find Closest Pair of Points in an Array
This is the program to find closest pair of points in an array.AlgorithmsFor Distance between Closest pointBegin Declare function Closest_dist_Spoint(poi stp[], int s, double dist, poi &pnt1, poi &pnt2) to the double datatype. Declare Minimum to the double datatype. Initialize Minimum = dist. for (int i = 0; i < s; ++i) for (int j = i+1; j < s && (stp[j].poi2 - stp[i].poi2) < Minimum; ++j) if (Distance(stp[i], stp[j]) < Minimum) then Minimum = Distance(stp[i], stp[j]). ...
Read MorePrint prime numbers in a given range using C++ STL
It is the program to print prime numbers in a given range.AlgorithmsBegin Declare a user define datatype stl. Declare a vector number to the stl datatype. Declare variable a to the stl datatype. Declare vector Prime_Number to the Boolean datatype. Prime_Number[0] = false. Prime_Number[1] = false. Declare b to the integer datatype. Initialize b = sqrt(a). for (stl pr=2; pr
Read MoreC++ Program to Create the Prufer Code for a Tree
Prufer code uniquely identifies a tree which is given by user as a graph representation with labels from 1 to p. This tree consist p(value is given by user) labels of node. It has sequence of p – 2 values.AlgorithmBegin Declare i, j, ver, edg, minimum, p to the integer datatype. Print “Enter the number of vertexes: ”. Enter the value of ver. Initialize edg = ver-1. Declare EDG[edg][2], DG[ver+1] to the integer datatype. Initialize DG[ver+1] = {0}. Print “This tree has (value of edg) edges for (value of ver) vertexes”. ...
Read Morememset in C++
In this section we will see what is the purpose of memset() function in C++. This function converts the value of a character to unsigned character and copies it into each of first n character of the object pointed by the given str[]. If the n is larger than string size, it will be undefined.The syntax of the memset() functionvoid* memset( void* str, int c, size_t n);In this example will use one string, then convert each character to some other character up to length n.Example#include using namespace std; int main() { char str[] = "Hello World"; memset(str, 'o', ...
Read MoreGetting a subvector from a vector in C++
This is a C++ program for getting a subvector from a vector in C++AlgorithmBegin Declare s as vector s(vector const &v, int m, int n) to initialize start and end position of vector to constructor. auto first = v.begin() + m. auto last = v.begin() + n + 1. Declare a variable vector of vector type. Pass the value of first and last position of vector. Return vector. Declare a template T. Declare a function show(). ...
Read MoreHow to shuffle a std::vector in C++
A vector shuffle can be done in the Fisher-Yates shuffle algorithm.In this algorithm, a linear scan of a vector is done and then swap each element with a random element among all the remaining element, including the element itself.AlgorithmBegin Declare a function show(). Pass a constructor of a vector as a parameter within show() function. for (auto const& i: input) Print the value of variable i. Declare v of vector type. Initialize some values into v vector in array pattern. ...
Read Morematch max_size() function in C++ STL
The match max_size() function in C++ STL returns the maximum number of elements in the match_results object that can be held by the match container.This function does not accept a parameter.Example Code#include #include using namespace std; int main() { match_results m; cout
Read MoreC++ Program to Check if a Binary Tree is a BST
Binary Search Tree is a binary tree data structure in which we have 3 properties −The left subtree of a binary search tree of a node contains only nodes with keys lesser than the node’s key.The right subtree of a binary search tree node contains only nodes with keys greater than the node’s key.The left and right of a subtree each must also be a binary search tree.AlgorithmBegin function BSTUtill() If node is equals to NULL then Return 1. If data of node is less than minimum or greater ...
Read More