Found 7347 Articles for C++

Array get() function in C++ STL?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

233 Views

In this section we will see the get() function of array in C++ STL. This function is used to get the ith element of the array container. The syntax is like below −Syntaxget array_nameThis function takes two mandatory parameters. The is the index parameter. It is used to point to the ith position of the array. The second argument is array_name. This is the actual array from this ith element will be taken. This function returns the ith element.Let us see one example to get the idea.Example Live Demo#include #include using namespace std; main() {    array arr = {00, 11, ... Read More

Argument Coercion in C/C++?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

1K+ Views

Here we will see about the argument coercion in C or C++. The Argument Coercion is one technique by which the compiler can implicitly convert the arguments from one type to another type. It follows argument promotion rule. If one argument is lower datatype, that can be converted into higher datatypes, but the reverse is not true. The reason is if one higher datatype is converted into a lower datatype, it may loss some data.Let us see one pyramid that can express how the implicit conversion takes place.Example Live Demo#include using namespace std; double myAdd(double a, double b){    return a+b; ... Read More

All reverse permutations of an array using STL in C++?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

167 Views

In this section we will see how to generate all reverse permutations using the STL in C++. The forward and reverse permutation of some numbers like (1, 2, 3) will be like below −Forward permutation1, 2, 3 1, 3, 2 2, 1, 3 2, 3, 1 3, 1, 2 3, 2, 1Reversed permutation3, 2, 1 3, 1, 2 2, 3, 1 2, 1, 3 1, 3, 2 1, 2, 3We will use the previous_permutation() function to get the resultAlgorithmgetPermutation(arr, n)Begin    sort arr    reverse the arr    repeat       print array elements    until the previous permutation calculation is not completed EndExample Live Demo#include #include using namespace std; void disp(int arr[], int n){    for(int i = 0; i

C/C++ Program to Find reminder of array multiplication divided by n ?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

183 Views

Here we will see how to calculate the remainder of array multiplication after dividing the result by n. The array and the value of n are supplied by the user. Suppose the array is like {12, 35, 69, 74, 165, 54} so the multiplication will be (12 * 35 * 69 * 74 * 165 * 54) = 19107673200. Now if we want to get the remainder after diving this by 47 it will be 14.As we can see this problem is very simple. we can easily multiply the elements then by using modulus operator, it can get the result. ... Read More

C/C++ Program to find Product of unique prime factors of a number?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

472 Views

In this section we will see how we can get the product of unique prime factor of a number in an efficient way. There is a number say n = 1092, we have to get product of unique prime factors of this. The prime factors of 1092 are 2, 2, 3, 7, 13. So the unique prime factors are {2, 3, 7, 13}, the product is 546. To solve this problem, we have to follow this rule −When the number is divisible by 2, then multiply 2 with product, and divide the number by 2 repeatedly, then next 2s will ... Read More

C/C++ Program for Odd-Even Sort (Brick Sort)?

Arnab Chakraborty
Updated on 01-Jul-2020 14:31:25

376 Views

Here we will see how the brick sort works. The Brick sort is one modification of bubble sort. This algorithm is divided into two parts. These parts are odd part and even parts. In the odd part we will use the bubble sort on odd indexed items, and in the even part we will use the bubble sort on even indexed elements. Let us see the algorithm to get the idea.AlgorithmbrickSort(arr, n)begin    flag := false    while the flag is not true, do       flag := true       for i := 1 to n-2, increase ... Read More

C/C++ Program for Finding the vertex, focus and directrix of a parabola?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

108 Views

Here we will see how to find the vertex, focus directrix of a parabola using C or C++ program. To get these parameters we need the general equation of a parabola. The general formula is −𝑦 = 𝑎𝑥2 + 𝑏𝑥 + 𝑐The values of a, b and c are given.The formula for the vertex −The formula for the focus −The formula for the Directrix - y −Example Live Demo#include using namespace std; void getParabolaDetails(float a, float b, float c) {    cout

C++ Program to Split the array and add the first part to the end?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

381 Views

Here we will see how to split an array, and add the first part after splitting at the end position. Suppose the array contents are {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}. We want to cut this intro two parts. The first part is from index 0 to 3 (split size 4), and second part is rest. After adding the first part at the end, the array elements will be like this {4, 5, 6, 7, 8, 9, 0, 1, 2, 3}. To solve this problem, we will follow this algorithm.AlgorithmsplitArray(arr, n, k)begin    for i := ... Read More

C++ Program to find sum of even factors of a number?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

248 Views

In this section we will see how we can get the sum of all even prime factors of a number in an efficient way. There is a number say n = 480, we have to get all factor of this. The prime factors of 480 are 2, 2, 2, 2, 2, 3, 5. The sum of all even factors is 2+2+2+2+2 = 10. To solve this problem, we have to follow this rule −When the number is divisible by 2, add them into the sum, and divide the number by 2 repeatedly.Now the number must be odd. So we will ... Read More

C++ Program for Recursive Bubble Sort?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

846 Views

In this section we will see another approach of famous bubble sort technique. We have used bubble sort in iterative manner. But here we will see recursive approach of the bubble sort. The recursive bubble sort algorithm is look like this.AlgorithmbubbleRec(arr, n)begin    if n = 1, return    for i in range 1 to n-2, do       if arr[i] > arr[i+1], then          exchange arr[i] and arr[i+1]       end if    done    bubbleRec(arr, n-1) endExample Live Demo#include using namespace std; void recBubble(int arr[], int n){    if (n == 1)     ... Read More

Advertisements