Found 7347 Articles for C++

Diagonally Dominant Matrix in C++?

AmitDiwan
Updated on 16-Jan-2021 10:34:57

293 Views

A matrix is said to be diagonally dominant matrix if for every matrix row, the diagonal entry magnitude of the row is larger than or equal to the sum of the magnitudes of every other non-diagonal entry in that row.Let us first define a constant int variable N with value 3 which represents our matrix dimensions.const int N = 3;The isDDM(int mat[N][N], int n) is a Boolean function that takes a copy of our matrix and the size of our matrix. Inside we iterate the rows and columns of our matrix using nested for loop. We then find the sum ... Read More

Diagonal Traversal of Binary Tree in C++?

AmitDiwan
Updated on 16-Jan-2021 10:28:18

151 Views

To consider the nodes that are passing between lines of slope -1. The diagonal traversal of the binary tree will be to traverse and print all those nodes present between these lines.Let us first define the struct that would represent a tree node that contains the data and its left and right node child. If this is the first node to be created then it’s a root node otherwise a child node.struct Node {    int data;    struct Node *leftChild, *rightChild; };Next we create our createNode(int data) function that takes an int value and assign it to the data ... Read More

Diagonal Sum of a Binary Tree in C++?

AmitDiwan
Updated on 16-Jan-2021 10:24:39

82 Views

To consider the nodes that are passing between lines of slope -1. The diagonal sum of the binary tree will be calculated by the sum of all nodes data that are present between these lines of reference.Let us first define the struct that would represent a tree node that contains the data and its left and right node child. If this is the first node to be created then it’s a root node otherwise a child node.struct Node {    int data;    struct Node *leftChild, *rightChild; };Next we create our createNode(int data) function that takes an int value and ... Read More

Diagonal of a Regular Pentagon in C++?

AmitDiwan
Updated on 16-Jan-2021 10:19:00

82 Views

To find the length for the diagonal of a regular pentagon we put the value of side in (1+√5)side/2 = (1+2.24)side/2.ExampleLet us see the following implementation to get the regular Heptagon diagonal from its side − Live Demo#include using namespace std; int main(){    float side = 5;    if (side < 0)       return -1;    float diagonal = (1+2.24) *(side/2);    cout

Diagonal of a Regular Hexagon in C++?

AmitDiwan
Updated on 16-Jan-2021 10:16:44

97 Views

The regular hexagons are comprised of six equilateral triangles so the diagonal of a regular hexagon would be 2*side.ExampleLet us see the following implementation to get the regular Heptagon diagonal from its side − Live Demo#include using namespace std; int main(){    float side = 12;    if (side < 0)       return -1;    float diagonal = 2*side;    cout

DFA for Strings not ending with “THE” in C++?

AmitDiwan
Updated on 16-Jan-2021 10:14:31

233 Views

To use Deterministic Finite Automaton(DFA) to find strings that aren’t ending with the substring “THE”. We should keep that in mind that any variation of the substring “THE” like “tHe”, “The” ,”ThE” etc should not be at the end of the string.First, we define our dfa variable and initialise it to 0 which keeps our track of state. It is incremented on each character matched.int dfa = 0;The begin(char c) method takes a character and checks if its ‘t’ or ‘T’ and go to first state i.e 1.void begin(char c){    if (c == 't' || c == 'T')   ... Read More

DFA based division in C++?

AmitDiwan
Updated on 16-Jan-2021 10:08:29

137 Views

The Deterministic Finite Automaton(DFA) is used for checking if a number is divisible by another number k or not. The algorithm is useful because it can also find the remainder if the number isn’t divisible.In DFA based division we build a DFA table with k states. We consider binary representation of the number so there is only 0 and 1 in each state in DFA.The createTransTable(int k, int transTable[][2]) function is used for creating the transTable and storing the states in it. It takes the number k by which the number is to be divisible and transTable[][2] which is an ... Read More

Determine the position of the third person on regular N sided polygon in C++?

AmitDiwan
Updated on 16-Jan-2021 10:01:57

60 Views

In a N sided polygon if two children are standing on A and B vertex then we need to determine the vertex number where another person should stand so that there should be minimum number of jumps required by that person to reach A and B both.Two conditions to note here are that the polygon vertices are numbered in a clockwise manner and we will always choose the least numbered vertex in case there are multiple answers.The vertexPosition(int sides, int vertexA, int vertexB) takes the no. of sides of the polygon and the position of vertex A and B. The ... Read More

Determine the number of squares of unit area that a line will pass through in C++?

AmitDiwan
Updated on 16-Jan-2021 09:54:21

63 Views

The objective is to determine the number of squares a line will pass through given two endpoints (x1, y1) and (x2, y2).To find the number of squares through which our line pass we need to find : difference between the x points (dx) = x2-x1, difference between the y points (dy) = y2-y1, adding the dx and dy and subtracting by their gcd (result) = dx + dy – gcd(dx, dy).The unitSquares(int x1, int y1, int x2, int y2) function takes four values x1, y1 and x2, y2. The absolute difference between the x2 and x1 and the absolute difference ... Read More

Determinant of a Matrix in C++?

AmitDiwan
Updated on 16-Jan-2021 09:48:53

1K+ Views

The determinant of a matrix can be calculated only for a square matrix by multiplying the first row cofactor by the determinant of the corresponding cofactor and adding them with alternate signs to get the final result.$$A = \begin{bmatrix}a & b & c\d & e &f \g & h &i \ \end{bmatrix} |A| = a(ei-fh)-b(di-gf)+c(dh-eg)$$First we have the determinantOfMatrix(int mat[N][N], int dimension) function that takes the matrix and the dimension value of the matrix. If the matrix is of only 1 dimension then it returns the [0][0] matrix value. This condition is also used as a base condition since we ... Read More

Advertisements