Server Side Programming Articles - Page 2132 of 2651

Find Harmonic mean using Arithmetic mean and Geometric mean using C++.

Arnab Chakraborty
Updated on 30-Oct-2019 05:44:56

351 Views

Here we will see how to get the Harmonic mean using the arithmetic mean and the geometric mean. The formula for these three means are like below −Arithmetic Mean − (a + b)/2Geometric Mean − $$\sqrt{\lgroup a*b\rgroup}$$Harmonic Mean − 2ab/(a+b)The Harmonic Mean can be expressed using arithmetic mean and geometric mean using this formula −$$HM=\frac{GM^{2}}{AM}$$Example Live Demo#include #include using namespace std; double getHarmonicMean(int a, int b) {    double AM, GM, HM;    AM = (a + b) / 2;    GM = sqrt(a * b);    HM = (GM * GM) / AM;    return HM; } int ... Read More

Find floor and ceil in an unsorted array using C++.

Arnab Chakraborty
Updated on 29-Oct-2019 12:03:00

680 Views

Here we will see how to find the floor and ceiling in an unsorted array. The floor value is larger element which is smaller than or equal to x, and the ceiling value is smallest value which is larger than x. If the array A = [5, 6, 8, 9, 6, 5, 5, 6], and x is 7, then the floor value is 6, and the ceiling value is 8.To solve this problem, we will follow the linear search approach. We will traverse the array and track two distances with respect to x.Min distance of element greater than or equal ... Read More

Find difference between sums of two diagonals in C++.

Arnab Chakraborty
Updated on 29-Oct-2019 11:54:13

542 Views

Here we will see how to get the difference between the sums of two diagonals of a given matrix. Suppose we have a matrix of order N x N, we have to get the sum of primary and secondary diagonals, then get the difference of them. To get the major diagonal, we know that the row index and column index increases simultaneously. For the second diagonal, row index and column index values are increased by this formula row_index = n – 1 – col_index. After getting the sum, take the difference and return a result.Example Live Demo#include #include #define MAX 100 ... Read More

Find column with maximum sum in a Matrix using C++.

Arnab Chakraborty
Updated on 29-Oct-2019 11:44:18

484 Views

Suppose we have a matrix of size M x N. We have to find the column, that has a maximum sum. In this program we will not follow some tricky approach, we will traverse the array column-wise, then get the sum of each column, if the sum is the max, then print the sum and the column index.Example#include #define M 5 #define N 5 using namespace std; int colSum(int colIndex, int mat[M][N]){    int sum = 0;    for(int i = 0; i maxSum) {          maxSum = sum;          index = i;       }    }    cout

Find ceil of a/b without using ceil() function in C++.

Arnab Chakraborty
Updated on 29-Oct-2019 11:41:09

888 Views

Here we will see how to get the ceiling value of a/b without using the ceil() function. If a = 5, b = 4, then (a/b) = 5/4. ceiling(5/4) = 2. To solve this, we can follow this simple formula −$$ceil\lgroup a,b\rgroup=\frac{a+b-1}{b}$$Example Live Demo#include using namespace std; int ceiling(int a, int b) {    return (a+b-1)/b; } int main() {    cout

Find area of triangle if two vectors of two adjacent sides are given using C++

Arnab Chakraborty
Updated on 29-Oct-2019 11:39:27

366 Views

Suppose we have two vectors for two adjacent sides of a triangle in the form $x\hat{i}+y\hat{j}+z\hat{k}$ Our task is to find the area of triangle. The area of triangle is magnitude of the cross product of two vectors. (|A x B|)$$\frac{1}{2}\rvert \vec{A}\times\vec{B}\rvert=\frac{1}{2}\sqrt{\lgroup y_{1}*z_{2}-y_{2}*z_{1}\rgroup^{2}+\lgroup x_{1}*z_{2}-x_{2}*z_{1}\rgroup^{2}+\lgroup x_{1}*y_{2}-x_{2}*y_{1}\rgroup^{2}}$$Example Live Demo#include #include using namespace std; float area(float A[], float B[]) {    float area = sqrt(pow((A[1] * B[2] - B[1] * A[2]), 2) + pow((A[0] * B[2] - B[0] * A[2]), 2) + pow((A[0] * B[1] - B[0] * A[1]), 2));    return area*0.5; } int main() {    float A[] = {3, 1, -2}; ... Read More

Find area of parallelogram if vectors of two adjacent sides are given using C++.

Arnab Chakraborty
Updated on 29-Oct-2019 11:37:09

156 Views

Suppose we have two vectors for two adjacent sides of a parallelogram in the form $x\hat{i}+y\hat{j}+z\hat{k}$ Our task is to find the area of parallelogram. The area of parallelogram is magnitude of the cross product of two vectors. (|A × B|)$$\rvert \vec{A}\times\vec{B}\rvert=\sqrt{\lgroup y_{1}*z_{2}-y_{2}*z_{1}\rgroup^{2}+\lgroup x_{1}*z_{2}-x_{2}*z_{1}\rgroup^{2}+\lgroup x_{1}*y_{2}-x_{2}*y_{1}\rgroup^{2}}$$Example Live Demo#include #include using namespace std; float area(float A[], float B[]) {    float area = sqrt(pow((A[1] * B[2] - B[1] * A[2]), 2) + pow((A[0] * B[2] - B[0] * A[2]), 2) + pow((A[0] * B[1] - B[0] * A[1]), 2));    return area; } int main() {    float A[] = {3, 1, -2}; ... Read More

Find and print duplicate words in std::vector using STL functions using C++.

Arnab Chakraborty
Updated on 29-Oct-2019 11:35:06

590 Views

Consider we have a list of strings. The list has some duplicate strings. We have to check which strings are occurred more than once. Suppose the string list is like [“Hello”, “Kite”, “Hello”, “C++”, “Tom”, “C++”]Here we will use the hashing technique, so create an empty hash table, then traverse each string, and for each string, s is already present in the hash, then display the string, otherwise insert into the hash.Example Live Demo#include #include #include using namespace std; void displayDupliateStrings(vector strings) {    unordered_set s;    bool hasDuplicate = false;    for (int i = 0; i

Find an equal point in a string of brackets using C++.

Arnab Chakraborty
Updated on 29-Oct-2019 11:32:58

421 Views

Here we will see how to get the equal points in a string of brackets. The equal point is the index I, such that the number of opening brackets before it is equal to the number of the closing bracket after it. Suppose a bracket string is like “(()))(()()())))”, if we see closer, we can getSo the number of opening brackets from 0 to 9 is 5, and the number of the closing brackets from 9 to 14 is also 5, so this is the equal point.To solve this problem, we have to follow these few steps −Store the number ... Read More

Find an element in array such that sum of left array is equal to sum of right array using c++

Arnab Chakraborty
Updated on 29-Oct-2019 11:27:06

742 Views

Suppose we have an array A, it has n elements. Our task is to divide the array A into two subarrays, such that the sum of each subarray will be the same. Suppose the array A = [2, 3, 4, 1, 4, 5], The output is 1, so subarrays before 1 and after 1 are taken. [2, 3, 4], and [4, 5].To solve this problem, we will calculate the whole array except for the first element in right_sum. Consider that is the partitioning element. We will traverse from left to right. Subtracting an element from right_sum and adding an element ... Read More

Advertisements