Find x to Convert Fraction a/b to c/d in C++

Ayush Gupta
Updated on 03-Oct-2019 12:30:47

118 Views

In this article, we will be discussing a program to find ΔX which is added to the numerator and denominator both of given fraction (a/b) to convert it to another given irreducible fraction (c/d).For example, let us suppose we have been given with the following values, a = 4 | b = 2 | c = 4 | d = 3then ΔX would be 4 such that (a + ΔX)/(b + ΔX) = 8/6 = 2/3As we know, (a + ΔX)/(b + ΔX) = c/d. Solving this equation for ΔX, we getΔX = (bc - ad) / (d - c)Example Live ... Read More

Find Path Between Two Cells in Matrix Using C++

Ayush Gupta
Updated on 03-Oct-2019 12:28:49

191 Views

In this article, we will be discussing a program to find whether there exists a path between two cells in a given matrix.Let us suppose we have been given a square matrix with possible values 0, 1, 2 and 3. Here, 0 means Blank Wall1 means Source2 means Destination3 means Blank CellThere can only be one Source and Destination in the matrix. The program is to see if there’s a possible path from Source to Destination in the given matrix moving in all four possible directions but not diagonally.Example Live Demo#include using namespace std; //creating a possible graph from given array ... Read More

Check if Two Parallel Lines Contain All Coordinates in C++

Ayush Gupta
Updated on 03-Oct-2019 12:21:59

394 Views

In this article, we will be discussing a program to find whether only two parallel lines can hold all the given coordinates points.For this we will be given an array, such that the coordinates will be (i, arr[i]). Let us suppose we are given an array, arr = {2, 6, 8, 12, 14}Then we can have these points on two parallel lines, the first line containing (1, 2), (3, 8) and (5, 14). The second line having the rest coordinates i.e (2, 6) and (4, 12).This problem can be solved by comparing the slopes of the lines made by the ... Read More

Find Vertex, Focus and Directrix of a Parabola in C++

Ayush Gupta
Updated on 03-Oct-2019 12:18:03

180 Views

In this article, we will be discussing a program to find the vertex, focus and directrix of a parabola when the coefficients of its equation is given.Parabola is a curve whose all points on the curve are equidistant from a single point called focus.As we know the general equation for a parabola isy = ax2 + bx + cFor this equation, the following are defined as :Vertex -(-b/2a, 4ac - b2/4a) Focus - (-b/2a, 4ac - b2+1/4a) Directrix - y = c - (b2 +1)4aExample Live Demo#include using namespace std; void calc_para(float a, float b, float c) {    cout ... Read More

Find Probability of a State in a Markov Chain in C++

Ayush Gupta
Updated on 03-Oct-2019 12:15:35

485 Views

In this article, we will be discussing a program to find the probability of reaching from the initial state to the final state in a given time period in Markov chain.Markov chain is a random process that consists of various states and the associated probabilities of going from one state to another. It takes unit time to move from one state to another.Markov chain can be represented by a directed graph. To solve the problem, we can make a matrix out of the given Markov chain. In that matrix, element at position (a, b) will represent the probability of going ... Read More

Find the Parity of a Number Efficiently in C++

Ayush Gupta
Updated on 03-Oct-2019 12:10:44

2K+ Views

In this article, we will be discussing a program to find the parity of a given number N.Parity is defined as the number of set bits (number of ‘1’s) in the binary representation of a number.If the number of ‘1’s in the binary representation are even, the parity is called even parity and if the number of ‘1’s in the binary representation is odd, the parity is called odd parity.If the given number is N, we can perform the following operations.y = N ^ (N >> 1)y = y ^ (y >> 2)y = y ^ (y >> 4)y = ... Read More

Find Number of Triangles Among Horizontal and Vertical Line Segments in C++

Ayush Gupta
Updated on 03-Oct-2019 12:07:05

159 Views

In this article, we will be discussing a program to find the number of triangles that can be formed by joining the intersection points of the given horizontal and vertical line segments.For example, let us say we had been given the following line segments. In this we have 3 intersection points. So the number of triangles that can be formed using these points will be 3C2.   | ---|--------|--    |        |    |  --|---|    |        |We will be following the Sweep Line Algorithm. We will be storing all the values of the line ... Read More

Find First Digit in Product of an Array of Numbers in C++

Ayush Gupta
Updated on 03-Oct-2019 12:00:40

136 Views

In this article, we will be discussing a program to find the first digit in the product of the elements of the given array.For example, let us say we have been given an array.arr = {12, 5, 16}Then the product is of these elements would be 12*5*16 = 960. Therefore, the result i.e the first digit of the product in this case would be 9.Example Live Demo#include using namespace std; int calc_1digit(int arr[], int x) {    long long int prod = 1;    for(int i = 0;i < x; i++) {       prod = prod*arr[i];    } ... Read More

Find Best Fit Rectangle that Covers a Given Point in C++

Ayush Gupta
Updated on 03-Oct-2019 11:58:26

279 Views

In this article, we will be discussing a program to find the best fit rectangle that covers a given point.In this problem, we are given we the coordinates of a point (x, y) and a ratio of length/breadth = l/b (say). We have to find the coordinates of a rectangle which contains the given point and whose dimensions follow the given ratio. In case of multiple rectangle existing, we have to choose the one having the shortest distance between its euclid’s center and the given point.To solve this, first we would minimize the ratio l/b. After that, we find the ... Read More

Find Sum of Digits Until Single Digit in C++

Ayush Gupta
Updated on 03-Oct-2019 11:55:00

636 Views

In this article, we will be discussing a program to find the sum of digits of a number until the sum itself becomes a single digit and cannot be done summation of further.For example, take the case of a number 14520. Adding the digits of this number we get 1 + 4 + 5 + 2 + 0 = 12. Since this is not a single digit number, we would further add the digits of the number received. Adding them we get, 1 + 2 = 3.Now, 3 is the final answer because it is a single digit number itself ... Read More

Advertisements