Found 7346 Articles for C++

strcat() vs strncat() in C++

Arjun Thakur
Updated on 24-Jun-2020 10:49:31

551 Views

Both strcat() and strncat() are predefined string functions in C++. Details about these are given as follows.strcat()This function is used for concatenation. It appends a copy of the source string at the end of the destination string and returns a pointer to the destination string. The syntax of strcat() is given as follows.char *strcat(char *dest, const char *src)A program that demonstrates strcat() is given as follows.Example Live Demo#include #include using namespace std; int main() {    char str1[20] = "Mangoes are ";    char str2[20] = "yellow";    strcat(str1, str2);    cout

strchr() Function in C++

Chandu yadav
Updated on 24-Jun-2020 10:49:57

323 Views

In C++, strchr() is a predefined function. It is used for string handling and it returns the first occurance of a given character in the string provided.The syntax of strchr() is given as follows.char *strchr( const char *str, int c)In the above syntax, str is the string that contains the character c. The strchr() function finds the first occurrence of c in str.A program that demonstrates the strchr() function is given as follows.Example Live Demo#include #include using namespace std; int main() {    char str[] = "strings";    char * c = strchr(str,'s');    cout

C++ Program to Perform Preorder Non-Recursive Traversal of a Given Binary Tree

George John
Updated on 24-Jun-2020 10:52:03

966 Views

Tree traversal is a form of graph traversal. It involves checking or printing each node in the tree exactly once. The preorder traversal of a binary search tree involves visiting each of the nodes in the tree in the order (Root, Left, Right).An example of Preorder traversal of a binary tree is as follows.A binary tree is given as follows.Preorder Traversal is: 5 3 2 4 8 9A program to perform preorder non-recursive traversal is given as follows.Example Live Demo#include #include using namespace std; struct node {    int data;    struct node *left;    struct node *right; }; struct ... Read More

C++ Program to Find Factorial of a Number using Dynamic Programming

Ankith Reddy
Updated on 30-Jul-2019 22:30:23

5K+ Views

The factorial of a positive integer n is equal to 1*2*3*...n. Factorial of a negative number does not exist. Here a C++ program is given to find out the factorial of a given input using dynamic programming.AlgorithmBegin    fact(int n):       Read the number n       Initialize       i = 1, result[1000] = {0}       result[0] = 1       for i = 1 to n          result[i] = I * result[i-1]    Print result EndExample Code#include using namespace std; int result[1000] = {0}; int fact(int n) {    if (n >= 0) {       result[0] = 1;       for (int i = 1; i

C++ Program to Sort an Array of 10 Elements Using Heap Sort Algorithm

Arjun Thakur
Updated on 12-Feb-2020 07:17:15

2K+ Views

Heap Sort is based on the binary heap data structure. In the binary heap the child nodes of a parent node are smaller than or equal to it in the case of a max heap, and the child nodes of a parent node are greater than or equal to it in the case of a min heap.An example that explains all the steps in Heap Sort is as follows.The original array with 10 elements before sorting is −207154101590237725This array is built into a binary max heap using max-heapify. This max heap represented as an array is given as follows.907720542515123710The root ... Read More

C++ Program to Perform Postorder Recursive Traversal of a Given Binary Tree

Chandu yadav
Updated on 24-Jun-2020 09:47:24

4K+ Views

Tree traversal is a form of graph traversal. It involves checking or printing each node in the tree exactly once. The postorder traversal of a binary search tree involves visiting each of the nodes in the tree in the order (Left, Right, Root).An example of postorder traversal of a binary tree is as follows.A binary tree is given as follows.Postorder Traversal is: 1 5 4 8 6The program to perform post-order recursive traversal is given as follows.Example Live Demo#include using namespace std; struct node {    int data;    struct node *left;    struct node *right; }; struct node *createNode(int val) ... Read More

C++ Program to Perform Inorder Recursive Traversal of a Given Binary Tree

George John
Updated on 24-Jun-2020 09:48:29

13K+ Views

Tree traversal is a form of graph traversal. It involves checking or printing each node in the tree exactly once. The inorder traversal of a binary search tree involves visiting each of the nodes in the tree in the order (Left, Root, Right).An example of Inorder traversal of a binary tree is as follows.A binary tree is given as follows.Inorder Traversal is: 1 4 5 6 8The program to perform in-order recursive traversal is given as follows.Example Live Demo#include using namespace std; struct node {    int data;    struct node *left;    struct node *right; }; struct node *createNode(int val) ... Read More

C++ Program to Perform Preorder Recursive Traversal of a Given Binary Tree

Ankith Reddy
Updated on 24-Jun-2020 09:49:39

7K+ Views

Tree traversal is a form of graph traversal. It involves checking or printing each node in the tree exactly once. The preorder traversal of a binary search tree involves visiting each of the nodes in the tree in the order (Root, Left, Right).An example of Preorder traversal of a binary tree is as follows.A binary tree is given as follows.Preorder Traversal is: 6 4 1 5 8The program to perform pre-order recursive traversal is given as follows.Example Live Demo#include using namespace std; struct node {    int data;    struct node *left;    struct node *right; }; struct node *createNode(int val) ... Read More

C++ Program to Check Multiplicability of Two Matrices

Arjun Thakur
Updated on 24-Jun-2020 09:50:27

248 Views

Two matrices are said to be multiplicable if they can be multiplied. This is only possible if the number of columns of the first matrix is equal to the number of rows of the second matrix. For example.Number of rows in Matrix 1 = 3 Number of columns in Matrix 1 = 2 Number of rows in Matrix 2 = 2 Number of columns in Matrix 2 = 5 Matrix 1 and Matrix 2 are multiplicable as the number of columns of Matrix 1 is equal to the number of rows of Matrix 2.A program to check the ... Read More

C++ Program to Compute Determinant of a Matrix

Chandu yadav
Updated on 24-Jun-2020 09:51:44

9K+ Views

The determinant of a square matrix can be computed using its element values. The determinant of a matrix A can be denoted as det(A) and it can be called the scaling factor of the linear transformation described by the matrix in geometry.An example of the determinant of a matrix is as follows.The matrix is: 3 1 2 7 The determinant of the above matrix = 7*3 - 2*1 = 21 - 2 = 19 So, the determinant is 19.The program to compute the determinant of a matrix is as follows.Example Live Demo#include #include using namespace std; int determinant( int matrix[10][10], int ... Read More

Advertisements