Found 7347 Articles for C++

C++ Program to Find Closest Pair of Points in an Array

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

680 Views

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 More

C++ Program to Implement Sparse Array

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

2K+ Views

A sparse matrix is a matrix in which majority of the elements are 0. An example for this is given as follows.The matrix given below contains 5 zeroes. Since the number of zeroes is more than half the elements of the matrix, it is a sparse matrix.0 0 9 5 0 8 7 0 0AlgorithmBegin    Declare a 2D array a[10][10] to the integer datatype.       Initialize some values of array.    Declare i, j, count to the integer datatype.       Initialize count = 0.    Declare row, col to the integer datatype.       ... Read More

C++ Program to Find the maximum subarray sum using Binary Search approach

Smita Kapse
Updated on 30-Jul-2019 22:30:26

219 Views

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 More

C++ Program to Solve Travelling Salesman Problem for Unweighted Graph

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

3K+ Views

Travelling Salesman Problem use to calculate the shortest route to cover all the cities and return back to the origin city. This method is use to find the shortest path to cover all the nodes of a graph.This is the program to find shortest route of a unweighted graph.AlgorithmBegin    Define a variable vr = 4 universally.    Declare an integer function TSP to implement Travelling salesman Problem.    Declare a graph grph[][] as a 2D matrix and variable p to the integer datatype.    Pass them as a parameter.    Declare variable ver to the vector datatype.    for ... Read More

Proper stack and heap usage in C++?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

554 Views

The stack − All variables declared inside the function will take up memory from the stack. So, any local variable inside a function lives on the stack.The heap − This is unused memory of the program and can be used to allocate the memory dynamically when program runs. So If we want something to live longer than the function that declared it, we must allocate it on the heap.Exampleint main() {    int a; //get memory allocated on stack.    int *ptr=new int[7]; //memory for 7 integers allocated on heap. }The main issue in heap memory is fragmentation whereas memory ... Read More

Why do we pass a Pointer by Reference in C++?

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

687 Views

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 Live Demo#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

Why do we check for a NULL pointer before deleting in C/C++?

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

389 Views

It is basically pointless to check for a NULL pointer before deleting. Deleting a pointer will do nothing if the pointer set to NULL. It might be the reason to check for the NULL pointer that deleting a pointer which is already set to NULL may indicate bugs in the program.

Why are NULL pointers defined differently in C and C++?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

131 Views

In C++, a null pointer can be defined by as a null pointer constant is an integer constant expression with the value 0, like −int*p = 0;But in c, a null pointer can be defined by as a null pointer constant is an integer constant expression with the value 0 or such an expression cast to void*, like −Int *p = 0;;Orint*p = (void*) 0;In C++11 a keyword “nullptr” is used to represent nullpointer.int* ptr = nullptr;In CExample Live Demo#include int main() {    int *p= NULL; //initialize the pointer as null.    printf("The value of pointer is %u", p); ... Read More

What is difference between a pointer and reference parameter in C++?

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

6K+ Views

PointersPointer variables are used to store the address of variable.SyntaxType *pointer;InitializationType *pointer; Pointer=variable name;ReferencesWhen a parameter is declared as reference, it becomes an alternative name for an existing parameter.SyntaxType &newname=existing name;InitializationType &pointer; Pointer=variable name;The main differences between pointers and reference parameters are −References are used to refer an existing variable in another name whereas pointers are used to store address of variable.References cannot have a null value assigned but pointer can.A reference variable can be referenced by pass by value whereas a pointer can be referenced by pass by reference.A reference must be initialized on declaration while it is not ... Read More

Scope Resolution Operator vs this pointer in C++

Smita Kapse
Updated on 30-Jul-2019 22:30:26

357 Views

Scope Resolution Operator is used to access static or class members whereas this pointer is used to access object members when there is a local variable with same name.Scope Resolution operatorExample Live Demo#include using namespace std; class AB {    static int x;    public:       // Local parameter 'x' hides class member       // 'x', but we can access it using ::.    void print(int x) {       cout

Advertisements