Found 7197 Articles for C++

C++ Program for First Fit algorithm in Memory Management

Sunidhi Bansal
Updated on 23-Dec-2019 06:26:47

7K+ Views

Given with the n number of processes and m size of memory blocks and the task is to find the best fit memory block for the corresponding process using the first fit memory management algorithm.What is First Fit Memory Management Algorithm?There is multiple memory partitioning algorithms available which are used by the operating system to allocate the memory blocks to the processes like −First Fit AlgorithmNext Fit AlgorithmBest Fit AlgorithmWorst Fit AlgorithmQuick Fit AlgorithmFirst Fit Algorithm is the simplest technique of allocating the memory block to the processes amongst all. In this algorithm, the pointer keeps track of all the ... Read More

Minimum number of swaps required to sort an array in C++

Narendra Kumar
Updated on 23-Dec-2019 06:19:04

612 Views

Problem statementGiven an array of N distinct elements, find the minimum number of swaps required to sort the arrayExampleIf array is {4, 2, 1, 3} then 2 swaps are requiredSwap arr[0] with arr[2]Swap arr[2] with arr[3}Algorithm1. Create a vector of pair in C++ with first element as array alues and second element as array indices. 2. Sort the vector of pair according to the first element of the pairs. 3. Traverse the vector and check if the index mapped with the value is correct or not, if not then keep swapping until the element is placed correctly and keep counting ... Read More

Reverse a string using the pointer in C++

Ajay yadav
Updated on 23-Dec-2019 06:14:43

3K+ Views

This article reverse a string using the pointer in C++ coding, First, it calculates the length of a pointer to string then run a for loop in decrement order to display the reverse string as follows;Example Live Demo#include #include using namespace std; int main(){    char *str="ajaykumar";    cout

Minimum number using set bits of a given number in C++

Narendra Kumar
Updated on 23-Dec-2019 06:12:59

218 Views

Problem statementGiven an unsigned number, find the minimum number that could be formed by using the bits of the given unsigned number.ExampleIf input = 10 then answer would be 3Binary representation of 10 is 1010 and minimum number with 2 set bits is 0011 i.e. 3Algorithm1. Count the number of set bits. 2. (Number of set bits) ^ 2 – 1 represents the minimized number)Example Live Demo#include using namespace std; int getSetBits(int n) {    int cnt = 0;    while (n) {       ++cnt;       n = n & (n - 1);    }    return cnt; } int getMinNumber(int n){    int bits = getSetBits(n);    return pow(2, bits) - 1; } int main() {    int n = 10;    cout

C++ Program for dot product and cross product of two vectors

Sunidhi Bansal
Updated on 12-Aug-2021 14:28:43

7K+ Views

We are given two vectors let’s say vector A and vector B containing x, y, and directions, and the task is to find the cross product and dot product of the two given vector arrays.What is a vector?In mathematics, a quantity that has a magnitude and a direction is known as a vector whereas a quantity that has only one value as magnitude is known as a scalar. The point from where the vector starts is known as the initial point and the point where the vector ends is known as the terminal point. The distance between the initial point ... Read More

Sort an Array of string using Selection sort in C++

Ajay yadav
Updated on 23-Dec-2019 06:09:48

1K+ Views

The Selection Sort algorithm sorts an exhibit by more than once finding the base component from the unsorted part and putting it toward the start. In each emphasis of determination sort, the base component from the unsorted subarray is picked and moved to the arranged subarray.Example Live Demo#include #include using namespace std; #define MAX_LEN 50 void selectionSort(char arr[][50], int n){    int i, j, mIndex;    // Move boundary of unsorted subarray one by one    char minStr[50];    for (i = 0; i < n-1; i++){       // Determine minimum element in unsorted array     ... Read More

Minimum number with digits as and 7 only and given sum in C++

Narendra Kumar
Updated on 23-Dec-2019 06:08:25

139 Views

Problem statementLucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. The task is to find minimum lucky number has the sum of digits equal to n.ExampleIf sum = 22 then lucky number is 4477 as 4 + 4 + 7 + 7 = 22Algorithm1. If sum is multiple of 4, then result has all 4s. 2. If sum is multiple of 7, then result has all 7s. 3. If sum is not multiple of 4 or 7, then we can subtract one of them till sum becomes multiple of other.Example Live ... Read More

stable_sort() in C++ STL

Ajay yadav
Updated on 23-Dec-2019 06:03:30

226 Views

The stable_sort method of STL first sorts the components with name as the key in ascending order and afterward the components are arranged with their segment as the key. Moreover, The stable_sort() calculation is viewed as steady in light of the fact that the overall request of comparable components is kept up. Here is the source code of the C++ program which exhibits the stable_sort() calculation demonstrated as follows;Example Live Demo#include using namespace std; int main(){    int arr[] = { 11, 15, 18, 19, 16, 17, 13, 20, 14, 12, 10 };    int n = sizeof(arr) / sizeof(arr[0]);    stable_sort(arr, arr + n);    cout

Minimum numbers which is smaller than or equal to N and with sum S in C++

Narendra Kumar
Updated on 23-Dec-2019 06:00:36

102 Views

Problem statementGiven N numbers from 1 to N and a number S. The task is to print the minimum number of numbers that sum up to give SExampleIf n = 7 and s = 10 then minimum 2 numbers are required(9, 1) (8, 2) (7, 3) (6, 4)AlgorithmAnswer can be calculated using below formula (S/N) + 1 if { S %N > 0}Example Live Demo#include using namespace std; int getMinNumbers(int n, int s) {    return s % n ? s / n + 1 : s / 2; } int main() {    int n = 7;    int s = 10;    cout

Minimum numbers needed to express every integer below N as a sum in C++

Narendra Kumar
Updated on 23-Dec-2019 05:59:39

231 Views

Problem statementWe have an integer N. We need to express N as a sum of K integers such that by adding some or all of these integers we can get all the numbers in the range 1 to N. The task is to find minimum value of KExampleIf N = 8 then final answer i.e. K would be 3If we take integers 1, 2, 3, and 4 then adding some or all of these groups we can get all number in the range 1 to Ne.g. 1 = 1 2 = 2 3 = 3 4 = 4 5 = ... Read More

Advertisements