Found 33676 Articles for Programming

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

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

122 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

129 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

Disarium Number with examples in C++?

AmitDiwan
Updated on 16-Jan-2021 08:36:34

1K+ Views

A number whose sum of its digits powered with its respective position equals to the number itself is called a disarium number.The noOfDigits(int num) function takes the number and return the number of digits by constantly dividing the number by 10 while there is only ones place left. On each iteration the digits variable is incremented to keep the digits track and is returned once the while loop ends.int noOfDigits(int num){    int digits = 0;    int temp = num;    while (temp){       temp= temp/10;       digits++;    }    return digits; }Next, isDisarium(int ... Read More

Depth of the deepest odd level node in Binary Tree in C++?

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

118 Views

Let us first define the struct that would represent a tree node that contains the int key 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 key) function that takes an int key value and assign it to the key member of the node. The function returns the pointer to the created struct Node. Also, the left and right child of the newly created node are set to null.Node* ... Read More

Depth of an N-Ary tree in C++?

AmitDiwan
Updated on 16-Jan-2021 08:07:59

282 Views

Let us first define the struct that would represent a tree node that contains a character key and a vector of Node *.struct Node{    char key;    vector children; };Next we create our createNode(int key) function that takes an int key value and assign it to the key member of the node. The function returns the pointer to the created struct node.Node *createNode(int key){    Node *node = new Node;    node->key = key;    return node; }Our depthOfTree(struct Node* root) function takes the root node as parameter. If the root is NULL then the depth is returned as ... Read More

Density of Binary Tree in One Traversal in C++?

AmitDiwan
Updated on 16-Jan-2021 08:03:50

182 Views

The density of a binary tree is calculated by dividing its size by its height.Binary tree density = size/height.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 member of the node. The function returns the pointer to the created struct ... Read More

Deletion in a Binary Tree in C++?

AmitDiwan
Updated on 16-Jan-2021 07:58:40

310 Views

The deletion is to be performed by replacing the deleted mode by bottom and rightmost node.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 newNode(int data) function that takes an int value and assign it to the data member of the node. The function returns the pointer to the created struct Node. Also, the ... Read More

Diagonal of a Regular Heptagon in C++?

AmitDiwan
Updated on 16-Jan-2021 07:46:50

157 Views

To find the length of the diagonal we put the value of side in 2*side*sin (900/14). The value of sin (900/14) = 0.9.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*0.9;    cout

Demlo number (Square of 11...1)” in C++?

AmitDiwan
Updated on 16-Jan-2021 07:44:20

224 Views

Demlo numbers are palindromic numbers that are generated by the square of number of form 11..1 given that the number is less than 10 digits.Let us first declare the string variables −string demNum = "1111"; string square = "";Now, we loop till the length of the demNum string. Inside the loop we convert the index value i to string and append it to square variable.for(int i=1 ;i= 1; i--)    square += char(i + '0');ExampleLet us see the following implementation to get a better understanding of demlo numbers − Live Demo#include using namespace std; int main(){    string demNum = ... Read More

Advertisements