Found 7347 Articles for C++

C++ Program for sorting variables of any data type

Sunidhi Bansal
Updated on 20-Dec-2019 14:01:57

1K+ Views

We are given with the values of different datatypes like it can be of type integer, float, string, bool, etc. and the task is to sort the variables of any data type using one common method or function and display the result.In C++, we can use std::sort to sort any type of array which is available in C++ standard template library(STL). By default sort function sorts the array element in ascending order. Sort() function takes three arguments −Start element in the array list i.e. from where you want to start your sortEnd element in the array list i.e. till where ... Read More

Product of non-repeating (distinct) elements in an Array in C++

Sunidhi Bansal
Updated on 20-Dec-2019 13:24:19

145 Views

We are given with an array of repeating or duplicate elements and the task is to find the product of all those elements which are non-repeating or distinct in the given array and display the results.ExampleInput-: arr[] = {2, 1, 1, 2, 3, 4, 5, 5 } Output-: 120 Explanation-: Since 1, 2 and 5 are repeating more than once so we will take them into consideration for their first occurrence. So result will be 1 * 2 * 3 * 4 * 5 = 120 Input-: arr[] = {1, 10, 9, 4, 2, 10, 10, 45, 4 } ... Read More

C++ Program for Bisection Method

Sunidhi Bansal
Updated on 20-Dec-2019 13:04:12

13K+ Views

Given with the function f(x) with the numbers a and b where, f(a) * f(b) > 0 and the function f(x) should lie between a and b i.e. f(x) = [a, b]. The task is to find the value of root that lies between interval a and b in function f(x) using bisection method.What is bisection method?Bisection method is used to find the value of a root in the function f(x) within the given limits defined by ‘a’ and ‘b’. The root of the function can be defined as the value a such that f(a) = 0.ExampleQuadratic equation F(x) = ... Read More

C++ Program for Priority Scheduling

Sunidhi Bansal
Updated on 20-Dec-2019 12:43:48

10K+ Views

We are given with the n number of processes i.e. P1, P2, P3, ......., Pn with their corresponding burst times and priorities associated with each process . The task is to find the average waiting time ,average turnaround time and the sequence of process execution using priority CPU scheduling algorithm.What is Waiting Time and Turnaround Time?Turnaround Time is the time interval between the submission of a process and its completion.Turnaround Time = completion of a process – submission of a processWaiting Time is the difference between turnaround time and burst timeWaiting Time = turnaround time – burst timeWhat is Priority ... Read More

C++ Program for Best Fit algorithm in Memory Management

Sunidhi Bansal
Updated on 20-Dec-2019 12:30:51

4K+ Views

Given two arrays containing block size and process size; the task is to print the results according to Best Fit algorithm in memory management.What is Best Fit Algorithm?Best Fit is a memory management algorithm; it deals with allocating smallest free partition which meets the requirement of the requesting process. In this algorithm we look for the whole memory block and check the smallest and most appropriate block for the process and then look for the immediate near block which can be used to fulfill the adequate process.So we will take the block size and process size and return the output ... Read More

C++ Program for Optimal Page Replacement Algorithm

Sunidhi Bansal
Updated on 20-Dec-2019 12:20:43

4K+ Views

Given page number and page size; the task is to find number of hits and misses as when we allocate the memory block to a page using Optimal Page Replacement Algorithm.What is Optimal Page Replacement Algorithm?Optimal page replacement algorithm is a page replacement algorithm. A page replacement algorithm is an algorithm which decides which memory page is to be replaced. In Optimal page replacement we replace the page which is not referred to the near future, although it can’t be practically implemented, but this is most optimal and have minimal miss, and is most optimal.Let’s understand by using an example ... Read More

C++ Program for Derivative of a Polynomial

Sunidhi Bansal
Updated on 20-Dec-2019 11:32:06

4K+ Views

Given a string containing the polynomial term, the task is to evaluate the derivative of that polynomial.What is a Polynomial?Polynomial comes from two words: - “Poly” which means “many” and “nomial” means “terms”, which comprises many terms. Polynomial expression is an expression containing variables, coefficients and exponents, which only involves operations such as, addition, multiplication and subtraction of variable(s).Example of polynomialx2+x+1Derivative of the polynomial p(x) = mx^n  will be −m * n * x^(n-1)ExampleInput: str = "2x^3 +1x^1 + 3x^2"    val = 2 Output: 37 Explanation: 6x^2 + 1x^0 + 6x^1    Putting x = 2    6*4 + ... Read More

Program for Mobius Function in C++

Sunidhi Bansal
Updated on 20-Dec-2019 11:17:22

320 Views

Given a number n; the task is to find the Mobius function of the number n.What is Mobius Function?A Mobius function is number theory function which is defined by$$\mu(n)\equiv\begin{cases}0\1\(-1)^{k}\end{cases}$$n=  0 If n has one or more than one repeated factorsn= 1 If n=1n= (-1)k  If n is product of k distinct prime numbersExampleInput: N = 17 Output: -1 Explanation: Prime factors: 17, k = 1, (-1)^k 🠠(-1)^1 = -1 Input: N = 6 Output: 1 Explanation: prime factors: 2 and 3, k = 2 (-1)^k 🠠(-1)^2 = 1 Input: N = 25 Output: 0 Explanation: Prime factor is ... Read More

Max sum of M non-overlapping subarrays of size K in C++

Narendra Kumar
Updated on 20-Dec-2019 10:39:21

182 Views

Problem statementGiven an array and two numbers M and K. We need to find sum of max M subarrays of size K (non-overlapping) in the array. (Order of array remains unchanged). K is the size of subarrays and M is the count of subarray. It may be assumed that size of array is more than m*k. If total array size is not multiple of k, then we can take partial last array.ExampleIf Given array is = {2, 10, 7, 18, 5, 33, 0}. N = 7, M = 3 and K = 1 then output will be 61 as subset ... Read More

Missing Permutations in a list in C++

Narendra Kumar
Updated on 20-Dec-2019 10:34:29

68 Views

Problem statementGiven a list of permutations of any word. Find the missing permutation from the list of permutations.ExampleIf permutation is = { “ABC”, “ACB”, “BAC”, “BCA”} then missing permutations are {“CBA” and “CAB”}AlgorithmCreate a set of all given stringsAnd one more set of all permutationsReturn difference between two setsExample Live Demo#include using namespace std; void findMissingPermutation(string givenPermutation[], size_t permutationSize) {    vector permutations;    string input = givenPermutation[0];    permutations.push_back(input);    while (true) {       string p = permutations.back();       next_permutation(p.begin(), p.end());       if (p == permutations.front())          break;     ... Read More

Advertisements