Found 7347 Articles for C++

C/C++ Program to Find the sum of Series with the n-th term as n^2 – (n-1)^2

sudhir sharma
Updated on 20-Aug-2019 08:24:03

220 Views

There are many types of series in mathematics which can be solved easily in C programming. This program is to find the sum of following of series in C program.Tn = n2 - (n-1)2Find the sum of all of the terms of series as Sn mod (109 + 7) and, Sn = T1 + T2 + T3 + T4 + ...... + TnInput: 229137999 Output: 218194447ExplanationTn can be expressed as 2n-1 to get itAs we know ,=> Tn = n2 - (n-1)2 =>Tn = n2 - (1 + n2 - 2n) =>Tn = n2 - 1 - n2 + 2n ... Read More

"static const" vs "#define" vs "enum" ?

sudhir sharma
Updated on 20-Aug-2019 08:22:23

5K+ Views

"static const"“static const” is basically a combination of static(a storage specifier) and const(a type qualifier).The static determines the lifetime and visibility/accessibility of the variable. This means if a variable is declared as a static variable, it will remain in the memory the whole time when the program is running, while the normal or auto variables are destroyed when the function (where the variable was defined) is over.The const is a type qualifier. A type qualifier is used to express additional info about a value through type system. When a variable is initialized using the const type qualifier, it will not ... Read More

"delete this" in C++?

sudhir sharma
Updated on 20-Aug-2019 08:19:16

2K+ Views

Delete is an operator that is used to Deallocate storage space of Variable.This pointer is a kind of pointer that can be accessed but only inside nonstatic member function and it points to the address of the object which has called the member function.This pointer holds the address of the current object, in simple words, you can say that this pointer points to the current object of the classWhenever we call a member function through its object, compiler secretly passes the address of calling that object as the first parameter in member function as this pointer.Generally, delete operator should not ... Read More

C/C++ Tokens?

sudhir sharma
Updated on 20-Aug-2019 08:13:18

8K+ Views

C++ Tokens are the smallest individual units of a program.C++ is the superset of C and so most constructs of C are legal in C++ with their meaning and usage unchanged. So tokens, expressions, and data types are similar to that of C.Following are the C++ tokens : (most of c++ tokens are basically similar to the C tokens)KeywordsIdentifiersConstantsVariablesOperatorsKeywordsKeywords are reserved words which have fixed meaning, and its meaning cannot be changed. The meaning and working of these keywords are already known to the compiler. C++ has more numbers of keyword than C, and those extra ones have special working ... Read More

Add n binary strings?

sudhir sharma
Updated on 20-Aug-2019 08:08:41

232 Views

In this program we have to add binary numbers given. there are n binary numbers and we have to add them all to give one binary number as an output.For this we will use binary addition logic and add all the terms from 1 to N one by one to gain the result.Input: "1011", "10", "1001" Output: 10110ExplanationThe easier method is by converting binary string to its decimal equivalent, then add them and convert into binary again. Here we will do the addition manually. We will use one helper function to add two binary strings. That function will be used ... Read More

Binary Search Tree insert with Parent Pointer in C++

Arnab Chakraborty
Updated on 20-Aug-2019 07:53:29

639 Views

We can insert new node into the BST in recursive manner. In that case we return the address of the root of each subtree. Here we will see another approach, where parent pointer will need to be maintained. Parent pointer will be helpful to find ancestor of a node etc.The idea is to store the address of the left and right subtrees, we set parent pointers of the returned pointers after recursive call. This confirms that all parent pointers are set during insertion. The parent of root is set to null.Algorithminsert(node, key) −begin    if node is null, then create ... Read More

Assign weights to edges such that longest path in terms of weights is minimized in C++

Arnab Chakraborty
Updated on 20-Aug-2019 07:47:16

91 Views

Here we will see one problem, in this problem one edge of a tree and a sum S is given. The task is to assign weights to all other weights, such that longest path in terms of weight is minimized. The sum of weights assigned to it is same as ‘S’.The approach is simple. The property of a tree that a path can have a maximum of two leaf nodes in it. That will be used to get the solution. So if we assign weights only to the edges connecting the leaf nodes, and assign other edges to 0. Then ... Read More

Array Manipulation and Sum using C/C++

Arnab Chakraborty
Updated on 20-Aug-2019 07:45:13

2K+ Views

Here we will see one problem, suppose one array is given. There are n elements. Another value S is also given. We have to find an element K in the array, such that, if all elements which are greater than K, are made equal to K, then the sum of all elements of the final array becomes equal to S. If that is not possible, then return -1.Suppose the elements are {12, 6, 3, 7, 8}, and sum value is 15, the output is 3. The final array is {3, 3, 3, 3, 3}, Sum of array elements is S ... Read More

Array Index with same count of even or odd numbers on both sides in C++

Arnab Chakraborty
Updated on 20-Aug-2019 07:43:06

86 Views

Here we will see one problem, suppose one array is given. There are n elements. We have to find one index, where frequency of even numbers of its left and the frequency of even numbers of its right side are same, or the frequency of odd numbers of its left is same as the frequency of odd numbers of its right. If no such result is there, return -1.Suppose the array is like {4, 3, 2, 1, 2, 4}. The output is 2. The element at index 2 is 2, there is only one odd number at left of it, ... Read More

Array algorithms in C++ STL

Arnab Chakraborty
Updated on 20-Aug-2019 07:39:58

471 Views

Since C++11 there are different functions added into the STL. These functions are present at algorithm header file. Here we will see some functions of this.The all_of() function is used to check one condition, that is true for all elements of a container. Let us see the code to get the ideaExample#include #include using namespace std; main() {    int arr[] = {2, 4, 6, 8, 10};    int n = sizeof(arr)/sizeof(arr[0]);    if(all_of(arr, arr + n, [](int x){return x%2 == 0;})) {       cout

Advertisements