Programming Articles - Page 2569 of 3366

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

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

839 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

Tapas Kumar Ghosh
Updated on 01-Jul-2025 15:30:33

3K+ Views

What is Sparse Array?Sparse array is used to determines the dimension of an array in which most of the elements are zero. It can be used for matrix calculation. Characteristics of Sparse Array Following are list of points that defines the characteristics of a sparse array: Most of the elements are zero or null values which means it is unused. Only non-zero elements and their indexes are stored. It is used to save the memory while comparing to normal array calculation. Example The Sparse array of ... Read More

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

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

338 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

What is the importance of "Java.lang.Class" in Java?

raja
Updated on 30-Jul-2019 22:30:26

451 Views

The java.lang.Class is one of the most important class in Java and it can provide several utility methods like getClass(), forName() which is used to find and load a class. It can also provide methods like Class.newInstance() which is the backbone of reflection and allow us to create an instance of a class without using new() operator.Importance of java.lang.ClassInstances of the class Class represent classes, interfaces,  enum and annotation in a running Java application.Whenever a java file is compiled, the compiler will insert a public, static, final field named Class of the type java.lang.Class into generated .class fileEach and every class exposes its code in the form of an ... Read More

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

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

4K+ 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

710 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

835 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

525 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

224 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 function chaining in JavaScript?

vineeth.mariserla
Updated on 29-Jun-2020 08:40:24

4K+ Views

Function chainingFunction chaining is nothing but grouping functions in one single line using dot notation. This type of chaining makes the code very concise and also improves the performance.Here we are going to learn function chaining using regular objects.a) Without function chaining In the following example an object 'obj' is created and in that object a public property called 'i' is created using keyword 'this' and initially assigned a value 0. later on user defined functions called add(), subtract() and print() are created inside the same object 'obj'. Now, the object "obj" will acts like a class(it's properties can be shared by ... Read More

Advertisements