Found 7347 Articles for C++

Process Synchronization in C/C++

Sunidhi Bansal
Updated on 23-Dec-2019 11:33:09

6K+ Views

Process synchronization is the technique to overcome the problem of concurrent access to shared data which can result in data inconsistency. A cooperating process is the one which can affect or be affected by other process which will lead to inconsistency in processes data therefore Process synchronization is required for consistency of data.The Critical-Section ProblemEvery process has a reserved segment of code which is known as Critical Section. In this section, process can change common variables, update tables, write files, etc. The key point to note about critical section is that when one process is executing in its critical section, ... Read More

Program for power of a complex number in O(log n) in C++

Sunidhi Bansal
Updated on 23-Dec-2019 11:18:15

1K+ Views

Given a complex number in the form of x+yi and an integer n; the task is calculate and print the value of the complex number if we power the complex number by n.What is a complex number?A complex number is number which can be written in the form of a + bi, where a and b are the real numbers and i is the solution of the equation or we can say an imaginary number. So, simply putting it we can say that complex number is a combination of Real number and imaginary number.Raising power of a complex numberTo raise ... Read More

Program for K Most Recently Used (MRU) Apps in C++

Sunidhi Bansal
Updated on 23-Dec-2019 11:12:31

1K+ Views

Given a number k and an array arr[n], containing n number of integer elements which are storing the id’s of the opened apps in a system; the task is to show k number of most recently used apps, like when we press alt+tab shows all the recent apps and most recent before the least recent. The position of every id represents different apps in a system −They are as follows −Id in arr[0] is the id of an app which is currently in use.Id in arr[1] is the id of an app which was most recently used.Id in arr[n-1] is ... Read More

Printing Items in 0/1 Knapsack in C++

Sunidhi Bansal
Updated on 23-Dec-2019 11:12:36

1K+ Views

Given weights and values of n items; the task is to print the items according to 0/1 knapsack for the following weights and values in a knapsack of capacity W, to get the maximum total value in the knapsack.What is 0/1 Knapsack?Knapsack is like a bag with only a fixed size or a bag which can handle a certain amount of weight. Each item which is included in a knapsack have some value(profit) and some weight to it. We have to add those weights which will get us the maximum profit according to the total weight a knapsack could hold.So ... Read More

Product of every K’th prime number in an array in C++

Sunidhi Bansal
Updated on 23-Dec-2019 11:04:17

92 Views

Given an array arr[n] containing n prime numbers and k; the task is to find the product of every k’th prime number in an array.Like, we have an array arr[] = {3, 5, 7, 11} and k = 2 so the prime number after every k i.e 5 and 11 we have to find their product which will be 5x11 = 55 and print the result as output.What are prime numbers?A prime number is a natural number which can’t be divided by any other number except 1 or the number itself. Some of the prime numbers are 2, 3, 5, ... Read More

Program for Point of Intersection of Two Lines in C++

Sunidhi Bansal
Updated on 23-Dec-2019 11:01:40

4K+ Views

Given points A and B corresponding to line AB and points P and Q corresponding to line PQ; the task is to find the point of intersection between these two lines.Note − The points are given in 2D plane on X and Y coordinates.Here A(a1, a2), B(b1, b2) and C(c1, c2), D(d1, d2) are the coordinates which are forming two distinct lines and P(p1, p2) is the point of intersection. (just for diagrammatic explanation of point of intersection)How to find the point of intersection −Let’s take above figure as −ExampleSo using the (a1, a2), (b1, b2), (c1, c2), (d1, d2) ... Read More

Program to build DFA that starts and end with ‘a’ from input (a, b) in C++

Sunidhi Bansal
Updated on 23-Dec-2019 10:56:18

809 Views

Given a DFA string of characters ‘a’ and ‘b’, which should start and end with the character ‘a’ the task is to find whether the string starts and ends with ‘a’ through a DFA.What is DFA(Deterministic Finite Automata)?In theory of computation, a branch of theoretical computer science, Deterministic Finite Automata is a finite state machine which accepts or reject strings of symbols. Deterministic refers to the uniqueness of the computation to run.For finding the string by a DFA and the string should start and end with ‘a’ from the input (a, b). Since there is no concept of memory and ... Read More

Probability of getting at least K heads in N tosses of Coins in C++

Sunidhi Bansal
Updated on 23-Dec-2019 10:52:10

447 Views

Probability is the chances of getting the desired output from the set of data available. The range of probability lie between 0 and 1 where an integer 0 shows the chances of impossibility and 1 indicates certainty.What is probability?Probability in mathematics gives us the tools that tell the uncertainty of the events and reasons. In other words we can say probability deals with calculating the likelihood of a given event’s occurrence, which can be expressed as a number between 1 and 0. For example: the probability of getting a head’s when an unbiased coin is tossed, or getting a 3 ... Read More

Priority Queue in C++ Standard Template Library (STL)

Sunidhi Bansal
Updated on 23-Dec-2019 09:29:44

337 Views

Priority queue is an abstract data type for storing a collection of prioritized elements that supports insertion and deletion of an element based upon their priorities, that is, the element with first priority can be removed at any time. The priority queue doesn’t stores elements in linear fashion with respect to their locations like in Stacks, Queues, List, etc. The priority queue ADT(abstract data type) stores elements based upon their priorities.Priority Queue supports the following functions −Size() − it is used to calculate the size of the priority queue as it returns the number of elements in it.Empty() − it return ... Read More

Product of all Subsequences of size K except the minimum and maximum Elements in C++

Sunidhi Bansal
Updated on 23-Dec-2019 07:45:59

138 Views

Given an array arr[n], containing n number of integers and an integer k for defining the size; the task is to print the product of all the subsequences of size k except the minimum and maximum elements.Let us assume we have a set of 4 elements {1, 2, 3, 4} and k as 2 so its subsets will be − {1, 2}, {2, 3}, {3, 4}, {1, 4}, {1, 3}, {2, 4}So excluding the maximum element 4, and minimum element 1, the remaining elements will be −2, 3, 3, 3, 2, product of which will be −2 * 3 * ... Read More

Advertisements