Programming Articles - Page 2567 of 3363

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

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

359 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

471 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

730 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

854 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

550 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

240 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

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

Tapas Kumar Ghosh
Updated on 16-Jun-2025 17:00:00

534 Views

Both scope resolution operator and 'this' pointer are used for different purposes in the C++ programs. The 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 the same name. Scope Resolution Operator in C++ The concept of scope resolution operator (::) is used in Object-Oriented Programming (OOPs) and namespace standard that can used to access something belongs to a specific scope, such as a class or namespace. Syntax It's syntax is as follows: :: Example In this program, we use ... Read More

Advertisements