C++ Articles

Page 551 of 597

Find all possible outcomes of a given expression in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 18-Dec-2019 308 Views

Suppose we have an arithmetic expression without parentheses. Our task is to find all possible outcomes of that expression. Suppose the expression is like 1+2*3-4, this can be interpreted like below −1+(2*(3-4)) = 1 + (2* -1) = -1(1+2)*(3-4) = 3 * -1 = -31+((2*3)-4) = 1 + (6 - 4) = 3((1+2)*3)-4 = (3 * 3) - 4 = 51+(2*3)-4 = 1 + 6 – 4 = 3To solve this problem, we have to follow these steps −Initially set res as emptyFor every operator x, do the following −Recursively evaluate all possible values on left of x, let the ...

Read More

Find the node whose absolute difference with X gives maximum value in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 17-Dec-2019 149 Views

Suppose we have a tree, and the weights of all the nodes and an integer x. We have to find the node i, such that |weight[i] - x| is minimum. If the graph is like below, and x = 15Output will be 3. Now for different nodes, it will be like belowNode 1, |5 – 15| = 10Node 2, |10 – 15| = 5Node 3, |11 – 15| = 4Node 4, |8 – 15| = 7Node 5, |6 – 15| = 9The idea is simple. We will perform the DFS on the tree, and keep track of the node, whose ...

Read More

C++ Program to implement t-test

Ayush Gupta
Ayush Gupta
Updated on 03-Dec-2019 1K+ Views

In this tutorial, we will be discussing a program to implement t-test.The t-test of the student’s T test is used to compare two means and tell if both of them are similar or different. Along with this, t-test also helps to determine how large the differences are to know the reason for the change.Example#include using namespace std; //calculating mean float calc_mean(float arr[], int n){    float sum = 0;    for (int i = 0; i < n; i++)       sum = sum + arr[i];    return sum / n; } //calculating standard deviation float calc_deviation(float arr[], ...

Read More

C++ Program to implement standard error of mean

Ayush Gupta
Ayush Gupta
Updated on 03-Dec-2019 324 Views

In this tutorial, we will be discussing a program to implement standard error of mean.Standard error of mean is the estimation of sample mean dispersion from population mean. Then it is used to estimate the approximate confidence intervals for the mean.Example#include using namespace std; //calculating sample mean float calc_mean(float arr[], int n){    float sum = 0;    for (int i = 0; i < n; i++)       sum = sum + arr[i];    return sum / n; } //calculating standard deviation float calc_deviation(float arr[], int n){    float sum = 0;    for (int i = ...

Read More

C++ program to implement Simpson's 3/8 rule

Ayush Gupta
Ayush Gupta
Updated on 03-Dec-2019 1K+ Views

In this tutorial, we will be discussing a program to implement SImpson’s ⅜ rule.Simpson’s ⅜ rule is used for doing numerical integrations. The most common use case of this method is in performing numerical approximations of definite integrals.In this, the parabolas on the graph are used for performing the approximations.Example#include using namespace std; //function that is to be integrated float func_inte( float x){    return (1 / ( 1 + x * x )); } //calculating the approximations float func_calculate(float lower_limit, float upper_limit, int interval_limit ){    float value;    float interval_size = (upper_limit - lower_limit) / interval_limit;    float ...

Read More

C++ Program to implement Linear Extrapolation

Ayush Gupta
Ayush Gupta
Updated on 03-Dec-2019 1K+ Views

In this tutorial, we will be discussing a program to implement Linear Extrapolation.Extrapolation is defined as a process in which the required value for a certain function is beyond the lower or the upper limits of the function definition.In the case of Linear Extrapolation, the value beyond the scope is found using the tangent made on the graph of the function to determine the required value. Linear Extrapolation gives quite accurate results when applied.Example#include using namespace std; //structuring the values of x and y struct Data {    double x, y; }; //calculating the linear extrapolation double calc_extrapolate(Data d[], ...

Read More

C++ program to implement Inverse Interpolation using Lagrange Formula

Ayush Gupta
Ayush Gupta
Updated on 03-Dec-2019 507 Views

In this tutorial, we will be discussing a program to implement Inverse Interpolation using Lagrange formula.Inverse Interpolation is defined as the method of finding the value of an independent variable from the given value of dependent value lying between two tabulated set of values for an unknown function.Example#include using namespace std; //structuring the values of x and y struct Data {    double x, y; }; //calculating inverse interpolation double calc_invinter(Data d[], int n, double y){    double x = 0;    int i, j;    for (i = 0; i < n; i++) {       double ...

Read More

C++ program to implement Collatz Conjecture

Ayush Gupta
Ayush Gupta
Updated on 03-Dec-2019 517 Views

In this tutorial, we will be discussing a program to implement Collatz Conjecture.For this, we will be given with a number n and we have to find out whether it can be converted to 1 using two operations −If n is even, n is converted to n/2.If n is odd, n is converted to 3*n + 1.Example#include using namespace std; //checking if n reaches to 1 or not bool check1(int n, unordered_set &s){    if (n == 1)       return true;    if (s.find(n) != s.end())       return false;    return (n % 2)? check1(3*n + ...

Read More

C++ program to get the Sum of series: 1 – x^2/2! + x^4/4! -.... upto nth term

Ayush Gupta
Ayush Gupta
Updated on 03-Dec-2019 1K+ Views

In this tutorial, we will be discussing a program to get the sum of series 1 – x^2/2! + x^4/4! … upto nth term.For this we will be given with the values of x and n. Our task will be to calculate the sum of the given series upto the given n terms. This can be easily done by computing the factorial and using the standard power function to calculate powers.Example#include #include //calculating the sum of series double calc_sum(double x, int n){    double sum = 1, term = 1, fct, j, y = 2, m;    int ...

Read More

C++ program to generate random alphabets

Ayush Gupta
Ayush Gupta
Updated on 03-Dec-2019 978 Views

In this tutorial, we will be discussing a program to generate random alphabets.For this, we will have a fixed size of array/string and use the rand() function to generate a random string of alphabets.Example#include using namespace std; const int MAX = 26; //generating a string of random alphabets string gen_random(int n){    char alphabet[MAX] = {       'a', 'b', 'c', 'd', 'e', 'f', 'g',       'h', 'i', 'j', 'k', 'l', 'm', 'n',       'o', 'p', 'q', 'r', 's', 't', 'u',       'v', 'w', 'x', 'y', 'z'    };    string res ...

Read More
Showing 5501–5510 of 5,962 articles
« Prev 1 549 550 551 552 553 597 Next »
Advertisements