Found 7347 Articles for C++

Else and Switch Statements with initializers in C++17

Arnab Chakraborty
Updated on 29-Jan-2020 07:22:23

162 Views

In many cases, we need to verify the value of something returned by a function and perform conditional operations based on this value. So our code is given below −// Some method or function return_type foo(Params) // Call function with Params and // store return in var1 auto var1 = foo(Params); if (var1 == /* some value */) {    //Perform Something } else {    //Perform Something else }Just follow the general format in all conditional if-else blocks. Firstly there is exist an optional initial statement which sets up the variable, followed by the if-else block. So the general ... Read More

AA Trees in C/C++?

Arnab Chakraborty
Updated on 29-Jan-2020 07:18:07

947 Views

An AA tree in computer science is defined as a form of balanced tree implemented for storing and retrieving ordered data efficiently. AA trees are treated as a variation of the red-black tree, a form of binary search tree which supports efficient addition and deletion of entries. As opposed to red-black trees, red nodes on an AA tree can only be added as a right subchild, no left subchild. The result of this operation is the simulation of a 2-3 tree instead of a 2-3-4 tree, which causes simplification the maintenance operations. The maintenance algorithms for a red-black tree require ... Read More

A-Buffer Method in C/C++?

Arnab Chakraborty
Updated on 29-Jan-2020 07:07:58

1K+ Views

A-Buffer technique in computer graphics is a simple hidden face detection mechanism used to medium scale virtual memory computers. This technique is also known as anti-aliased or area-averaged or accumulation buffer. This technique extends the algorithm of depth-buffer (or Z Buffer) technique. As the depth buffer technique can only be implemented for opaque object but not for transparent object, the A-buffer technique provides advantage in this scenario. Though the A buffer technique requires more memory, but different surface colors can be correctly composed implementing it. Being a descendent of the Z-buffer algorithm, each position in the buffer can locate or ... Read More

A Number Link Game in C/C++?

Arnab Chakraborty
Updated on 29-Jan-2020 07:04:03

133 Views

The Game − Suppose an n × n array of squares. Out of these, some of the squares are empty, some are solid, and some non-solid squares are set by integers 1, 2, 3, … .Each integer maintains or occupies exactly two different squares on the board. The task of the player is to connect the two occurrences of each integer on the board with the help of a simple path implementing horizontal and vertical movements alone. No two different paths are permitted to intersect one another. No path may include any solid square (solid squares are not permitted to ... Read More

3-way Merge Sort in C++

Arnab Chakraborty
Updated on 29-Jan-2020 07:01:31

1K+ Views

Merge sort involves recursively dividing the array into 2 parts, sorting and finally merging them. A variant of merge sort is treated as 3-way merge sort where instead of dividing the array into 2 parts we divide it into 3 parts.Merge sort breaks down the arrays to subarrays of size half in recursive manner. In the same way, 3-way Merge sort breaks down the arrays to subarrays of size one third.ExamplesInput : 46, -1, -44, 79, 31, -41, 11, 20 , 74, 94 Output : -44 -41 -1 11 20 31 46 74 79 94 Input : 24, -18 ... Read More

2-Satisfiability(2-SAT) Problem in C/C++?

Arnab Chakraborty
Updated on 29-Jan-2020 06:52:04

236 Views

Let f = (x1 ∨ y1) ∧ (x2 ∨ y2) ∧ ... ∧ (xn ∨ yn).Problem: Is f satisfiable?xi ∨ yi and and   are all equivalent. So we are converting each of (xi ∨ yi) s into those two statements.Now assume a graph with 2n vertices. In case of each of (xi∨yi) s two directed edges are addedFrom ¬xi to yiFrom ¬yi to xif is not treated as satisfiable if both ¬xi and xi are in the same SCC (Strongly Connected Component)Assume that f is treated as satisfiable. Now we want to provide values to each variable in order to satisfy ... Read More

2-3 Trees (Search and Insert) in C/C++?

Arnab Chakraborty
Updated on 29-Jan-2020 06:18:07

588 Views

A 2-3 tree is defined as a tree data structure, where every node with children (internal node) has either two children (2-node) as well as one data element or three children (3-nodes) as well as two data elements.DefinitionsWe call that an internal node is a 2-node if it has one data element and two children.We call that an internal node is a 3-node if it has two data elements and three children.We call that T is a 2-3 tree if and only if one of the following statements satisf −T is vacant or empty. In other words, T contains no ... Read More

0/1 Knapsack using Branch and Bound in C/C++?

Arnab Chakraborty
Updated on 29-Jan-2020 06:09:36

1K+ Views

The idea is to implement the fact that the Greedy approach provides the best solution for Fractional Knapsack problem.To check whether a particular node can give us a better solution or not, we calculate the optimal solution (through the node) implementing Greedy approach. If the solution calculated by Greedy approach itself is more than the best so far, then we can’t obtain a better solution through the node.Complete Algorithm is given below −Sort all items according to decreasing order of ratio of value per unit weight so that an upper bound can becalculated implementing Greedy Approach.Initialize maximum profit, such as ... Read More

Convert Binary Number in a Linked List to Integer in C++

Arnab Chakraborty
Updated on 27-Apr-2020 09:16:41

631 Views

Suppose we have one ‘head’ which is a reference node to a singly-linked list. The value of each node present in the linked list is either 0 or 1. This linked list stores the binary representation of a number. We have to return the decimal value of the number present in the linked list. So if the list is like [1, 0, 1, 1, 0, 1]To solve this, we will follow these steps −x := convert the list elements into an arraythen reverse the list xans := 0, and temp := 1for i in range i := 0, to size ... Read More

Shift 2D Grid in C++

Arnab Chakraborty
Updated on 27-Apr-2020 09:14:50

451 Views

Suppose we have one 2D grid of size m x n. We have another variable k. We have to shift the grid k times. The shift operation will be as followsElement at grid G[i, j] moves to G[i, j + 1]Element at grid G[i, n – 1] moves to G[i + 1, 0]Element at grid G[m - 1, n – 1] moves to G[0, 0]So if the grid is like −123456789The output will be −912345678To solve this, we will follow these steps −The shift operation will take the matrix as inputn = number of rows, m := number of columns, ... Read More

Advertisements