codeparrot/xlcost-text-to-code huggingface dataset:
Binary Search Tree insert with Parent Pointer | C ++ program to demonstrate insert operation in binary search tree with parent pointer ; Node structure ; A utility function to create a new BST Node ; A utility function to do inorder traversal of BST ; A utility function to insert a new Node with given key in BST ; If the tree is empty , return a new Node ; Otherwise , recur down the tree ; Set parent of root of left subtree ; Set parent of root of right subtree ; return the ( unchanged ) Node pointer ; Driver Program to test above functions ; Let us create following BST 50 / \ 30 70 / \ / \ 20 40 60 80 ; print iNoder traversal of the BST
#include <bits/stdc++.h> NEW_LINE struct Node { int key ; struct Node * left , * right , * parent ; } ; struct Node * newNode ( int item ) { struct Node * temp = new Node ; temp -> key = item ; temp -> left = temp -> right = NULL ; temp -> parent = NULL ; return temp ; } void inorder ( struct Node * root ) { if ( root != NULL ) { inorder ( root -> left ) ; printf ( " Node ▁ : ▁ % d , ▁ " , root -> key ) ; if ( root -> parent == NULL ) printf ( " Parent ▁ : ▁ NULL ▁ STRNEWLINE " ) ; else printf ( " Parent ▁ : ▁ % d ▁ STRNEWLINE " , root -> parent -> key ) ; inorder ( root -> right ) ; } } struct Node * insert ( struct Node * node , int key ) { if ( node == NULL ) return newNode ( key ) ; if ( key < node -> key ) { Node * lchild = insert ( node -> left , key ) ; node -> left = lchild ; lchild -> parent = node ; } else if ( key > node -> key ) { Node * rchild = insert ( node -> right , key ) ; node -> right = rchild ; rchild -> parent = node ; } return node ; } int main ( ) { struct Node * root = NULL ; root = insert ( root , 50 ) ; insert ( root , 30 ) ; insert ( root , 20 ) ; insert ( root , 40 ) ; insert ( root , 70 ) ; insert ( root , 60 ) ; insert ( root , 80 ) ; inorder ( root ) ; return 0 ; }
Construct a special tree from given preorder traversal | A program to construct Binary Tree from preorder traversal ; A binary tree node structure ; Utility function to create a new Binary Tree node ; A recursive function to create a Binary Tree from given pre [ ] preLN [ ] arrays . The function returns root of tree . index_ptr is used to update index values in recursive calls . index must be initially passed as 0 ; store the current value of index in pre [ ] ; Base Case : All nodes are constructed ; Allocate memory for this node and increment index for subsequent recursive calls ; If this is an internal node , construct left and right subtrees and link the subtrees ; A wrapper over constructTreeUtil ( ) ; Initialize index as 0. Value of index is used in recursion to maintain the current index in pre [ ] and preLN [ ] arrays . ; This function is used only for testing ; first recur on left child ; then print the data of node ; now recur on right child ; Driver function to test above functions
#include <bits/stdc++.h> NEW_LINE struct node { int data ; struct node * left ; struct node * right ; } ; struct node * newNode ( int data ) { struct node * temp = new struct node ; temp -> data = data ; temp -> left = NULL ; temp -> right = NULL ; return temp ; } struct node * constructTreeUtil ( int pre [ ] , char preLN [ ] , int * index_ptr , int n ) { int index = * index_ptr ; if ( index == n ) return NULL ; struct node * temp = newNode ( pre [ index ] ) ; ( * index_ptr ) ++ ; if ( preLN [ index ] == ' N ' ) { temp -> left = constructTreeUtil ( pre , preLN , index_ptr , n ) ; temp -> right = constructTreeUtil ( pre , preLN , index_ptr , n ) ; } return temp ; } struct node * constructTree ( int pre [ ] , char preLN [ ] , int n ) { int index = 0 ; return constructTreeUtil ( pre , preLN , & index , n ) ; } void printInorder ( struct node * node ) { if ( node == NULL ) return ; printInorder ( node -> left ) ; printf ( " % d ▁ " , node -> data ) ; printInorder ( node -> right ) ; } int main ( ) { struct node * root = NULL ; int pre [ ] = { 10 , 30 , 20 , 5 , 15 } ; char preLN [ ] = { ' N ' , ' N ' , ' L ' , ' L ' , ' L ' } ; int n = sizeof ( pre ) / sizeof ( pre [ 0 ] ) ; root = constructTree ( pre , preLN , n ) ; printf ( " Following ▁ is ▁ Inorder ▁ Traversal ▁ of ▁ the ▁ Constructed ▁ Binary ▁ Tree : ▁ STRNEWLINE " ) ; printInorder ( root ) ; return 0 ; }
Minimum Possible value of | ai + aj | CPP program to find number of pairs and minimal possible value ; function for finding pairs and min value ; initialize smallest and count ; iterate over all pairs ; is abs value is smaller than smallest update smallest and reset count to 1 ; if abs value is equal to smallest increment count value ; print result ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void pairs ( int arr [ ] , int n , int k ) { int smallest = INT_MAX ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) { if ( abs ( arr [ i ] + arr [ j ] - k ) < smallest ) { smallest = abs ( arr [ i ] + arr [ j ] - k ) ; count = 1 ; } else if ( abs ( arr [ i ] + arr [ j ] - k ) == smallest ) count ++ ; } cout << " Minimal ▁ Value ▁ = ▁ " << smallest << " STRNEWLINE " ; cout << " Total ▁ Pairs ▁ = ▁ " << count << " STRNEWLINE " ; } int main ( ) { int arr [ ] = { 3 , 5 , 7 , 5 , 1 , 9 , 9 } ; int k = 12 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; pairs ( arr , n , k ) ; return 0 ; }
Rank of an element in a stream | CPP program to find rank of an element in a stream . ; Inserting a new Node . ; Updating size of left subtree . ; Function to get Rank of a Node x . ; Step 1. ; Step 2. ; Step 3. ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * left , * right ; int leftSize ; } ; Node * newNode ( int data ) { Node * temp = new Node ; temp -> data = data ; temp -> left = temp -> right = NULL ; temp -> leftSize = 0 ; return temp ; } Node * insert ( Node * & root , int data ) { if ( ! root ) return newNode ( data ) ; if ( data <= root -> data ) { root -> left = insert ( root -> left , data ) ; root -> leftSize ++ ; } else root -> right = insert ( root -> right , data ) ; return root ; } int getRank ( Node * root , int x ) { if ( root -> data == x ) return root -> leftSize ; if ( x < root -> data ) { if ( ! root -> left ) return -1 ; else return getRank ( root -> left , x ) ; } else { if ( ! root -> right ) return -1 ; else { int rightSize = getRank ( root -> right , x ) ; if ( rightSize == -1 ) return -1 ; return root -> leftSize + 1 + rightSize ; } } } int main ( ) { int arr [ ] = { 5 , 1 , 4 , 4 , 5 , 9 , 7 , 13 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int x = 4 ; Node * root = NULL ; for ( int i = 0 ; i < n ; i ++ ) root = insert ( root , arr [ i ] ) ; cout << " Rank ▁ of ▁ " << x << " ▁ in ▁ stream ▁ is : ▁ " << getRank ( root , x ) << endl ; x = 13 ; cout << " Rank ▁ of ▁ " << x << " ▁ in ▁ stream ▁ is : ▁ " << getRank ( root , x ) << endl ; x = 8 ; cout << " Rank ▁ of ▁ " << x << " ▁ in ▁ stream ▁ is : ▁ " << getRank ( root , x ) << endl ; return 0 ; }
Rank of an element in a stream | C ++ program to find rank of an element in a stream . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int main ( ) { int a [ ] = { 5 , 1 , 14 , 4 , 15 , 9 , 7 , 20 , 11 } ; int key = 20 ; int arraySize = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int count = 0 ; for ( int i = 0 ; i < arraySize ; i ++ ) { if ( a [ i ] <= key ) { count += 1 ; } } cout << " Rank ▁ of ▁ " << key << " ▁ in ▁ stream ▁ is : ▁ " << count - 1 << endl ; return 0 ; }
Construct Special Binary Tree from given Inorder traversal | C ++ program to construct tree from inorder traversal ; A binary tree node has data , pointer to left child and a pointer to right child ; Prototypes of a utility function to get the maximum value in inorder [ start . . end ] ; Recursive function to construct binary of size len from Inorder traversal inorder [ ] . Initial values of start and end should be 0 and len - 1. ; Find index of the maximum element from Binary Tree ; Pick the maximum value and make it root ; If this is the only element in inorder [ start . . end ] , then return it ; Using index in Inorder traversal , construct left and right subtress ; Function to find index of the maximum value in arr [ start ... end ] ; Helper function that allocates a new node with the given data and NULL left and right pointers . ; This funtcion is here just to test buildTree ( ) ; first recur on left child ; then print the data of node ; now recur on right child ; Driver code ; Assume that inorder traversal of following tree is given 40 / \ 10 30 / \ 5 28 ; Let us test the built tree by printing Insorder traversal
#include <bits/stdc++.h> NEW_LINE using namespace std ; class node { public : int data ; node * left ; node * right ; } ; int max ( int inorder [ ] , int strt , int end ) ; node * newNode ( int data ) ; node * buildTree ( int inorder [ ] , int start , int end ) { if ( start > end ) return NULL ; int i = max ( inorder , start , end ) ; node * root = newNode ( inorder [ i ] ) ; if ( start == end ) return root ; root -> left = buildTree ( inorder , start , i - 1 ) ; root -> right = buildTree ( inorder , i + 1 , end ) ; return root ; } int max ( int arr [ ] , int strt , int end ) { int i , max = arr [ strt ] , maxind = strt ; for ( i = strt + 1 ; i <= end ; i ++ ) { if ( arr [ i ] > max ) { max = arr [ i ] ; maxind = i ; } } return maxind ; } node * newNode ( int data ) { node * Node = new node ( ) ; Node -> data = data ; Node -> left = NULL ; Node -> right = NULL ; return Node ; } void printInorder ( node * node ) { if ( node == NULL ) return ; printInorder ( node -> left ) ; cout << node -> data << " ▁ " ; printInorder ( node -> right ) ; } int main ( ) { int inorder [ ] = { 5 , 10 , 40 , 30 , 28 } ; int len = sizeof ( inorder ) / sizeof ( inorder [ 0 ] ) ; node * root = buildTree ( inorder , 0 , len - 1 ) ; cout << " Inorder ▁ traversal ▁ of ▁ the ▁ constructed ▁ tree ▁ is ▁ STRNEWLINE " ; printInorder ( root ) ; return 0 ; }
Continuous Tree | C ++ program to check if a tree is continuous or not ; A binary tree node has data , pointer to left child and a pointer to right child ; Helper function that allocates a new node with the given data and NULL left and right pointers . ; Function to check tree is continuous or not ; if next node is empty then return true ; if current node is leaf node then return true because it is end of root to leaf path ; If left subtree is empty , then only check right ; If right subtree is empty , then only check left ; If both left and right subtrees are not empty , check everything ; Driver program to test mirror ( )
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left , * right ; } ; struct Node * newNode ( int data ) { struct Node * node = new Node ; node -> data = data ; node -> left = node -> right = NULL ; return ( node ) ; } bool treeContinuous ( struct Node * ptr ) { if ( ptr == NULL ) return true ; if ( ptr -> left == NULL && ptr -> right == NULL ) return true ; if ( ptr -> left == NULL ) return ( abs ( ptr -> data - ptr -> right -> data ) == 1 ) && treeContinuous ( ptr -> right ) ; if ( ptr -> right == NULL ) return ( abs ( ptr -> data - ptr -> left -> data ) == 1 ) && treeContinuous ( ptr -> left ) ; return abs ( ptr -> data - ptr -> left -> data ) == 1 && abs ( ptr -> data - ptr -> right -> data ) == 1 && treeContinuous ( ptr -> left ) && treeContinuous ( ptr -> right ) ; } int main ( ) { struct Node * root = newNode ( 3 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 4 ) ; root -> left -> left = newNode ( 1 ) ; root -> left -> right = newNode ( 3 ) ; root -> right -> right = newNode ( 5 ) ; treeContinuous ( root ) ? cout << " Yes " : cout << " No " ; return 0 ; }
Sum of middle row and column in Matrix | C ++ program to find sum of middle row and column in matrix ; loop for sum of row ; loop for sum of column ; Driver function
#include <iostream> NEW_LINE using namespace std ; const int MAX = 100 ; void middlesum ( int mat [ ] [ MAX ] , int n ) { int row_sum = 0 , col_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) row_sum += mat [ n / 2 ] [ i ] ; cout << " Sum ▁ of ▁ middle ▁ row ▁ = ▁ " << row_sum << endl ; for ( int i = 0 ; i < n ; i ++ ) col_sum += mat [ i ] [ n / 2 ] ; cout << " Sum ▁ of ▁ middle ▁ column ▁ = ▁ " << col_sum ; } int main ( ) { int mat [ ] [ MAX ] = { { 2 , 5 , 7 } , { 3 , 7 , 2 } , { 5 , 6 , 9 } } ; middlesum ( mat , 3 ) ; return 0 ; }
Row | C program showing time difference in row major and column major access ; taking MAX 10000 so that time difference can be shown ; accessing element row wise ; accessing element column wise ; driver code ; Time taken by row major order ; Time taken by column major order
#include <stdio.h> NEW_LINE #include <time.h> NEW_LINE #define MAX  10000 NEW_LINE int arr [ MAX ] [ MAX ] = { 0 } ; void rowMajor ( ) { int i , j ; for ( i = 0 ; i < MAX ; i ++ ) { for ( j = 0 ; j < MAX ; j ++ ) { arr [ i ] [ j ] ++ ; } } } void colMajor ( ) { int i , j ; for ( i = 0 ; i < MAX ; i ++ ) { for ( j = 0 ; j < MAX ; j ++ ) { arr [ j ] [ i ] ++ ; } } } int main ( ) { int i , j ; clock_t t = clock ( ) ; rowMajor ( ) ; t = clock ( ) - t ; printf ( " Row ▁ major ▁ access ▁ time ▁ : % f ▁ s STRNEWLINE " , t / ( float ) CLOCKS_PER_SEC ) ; t = clock ( ) ; colMajor ( ) ; t = clock ( ) - t ; printf ( " Column ▁ major ▁ access ▁ time ▁ : % f ▁ s STRNEWLINE " , t / ( float ) CLOCKS_PER_SEC ) ; return 0 ; }
Rotate the matrix right by K times | CPP program to rotate a matrix right by k times ; size of matrix ; function to rotate matrix by k times ; temporary array of size M ; within the size of matrix ; copy first M - k elements to temporary array ; copy the elements from k to end to starting ; copy elements from temporary array to end ; function to display the matrix ; Driver 's code ; rotate matrix by k ; display rotated matrix
#include <iostream> NEW_LINE #define M  3 NEW_LINE #define N  3 NEW_LINE using namespace std ; void rotateMatrix ( int matrix [ ] [ M ] , int k ) { int temp [ M ] ; k = k % M ; for ( int i = 0 ; i < N ; i ++ ) { for ( int t = 0 ; t < M - k ; t ++ ) temp [ t ] = matrix [ i ] [ t ] ; for ( int j = M - k ; j < M ; j ++ ) matrix [ i ] [ j - M + k ] = matrix [ i ] [ j ] ; for ( int j = k ; j < M ; j ++ ) matrix [ i ] [ j ] = temp [ j - k ] ; } } void displayMatrix ( int matrix [ ] [ M ] ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) cout << matrix [ i ] [ j ] << " ▁ " ; cout << endl ; } } int main ( ) { int matrix [ N ] [ M ] = { { 12 , 23 , 34 } , { 45 , 56 , 67 } , { 78 , 89 , 91 } } ; int k = 2 ; rotateMatrix ( matrix , k ) ; displayMatrix ( matrix ) ; return 0 ; }
Program to check Involutory Matrix | Program to implement involutory matrix . ; Function for matrix multiplication . ; Function to check involutory matrix . ; multiply function call . ; Driver function . ; Function call . If function return true then if part will execute otherwise else part will execute .
#include <bits/stdc++.h> NEW_LINE #define N  3 NEW_LINE using namespace std ; void multiply ( int mat [ ] [ N ] , int res [ ] [ N ] ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { res [ i ] [ j ] = 0 ; for ( int k = 0 ; k < N ; k ++ ) res [ i ] [ j ] += mat [ i ] [ k ] * mat [ k ] [ j ] ; } } } bool InvolutoryMatrix ( int mat [ N ] [ N ] ) { int res [ N ] [ N ] ; multiply ( mat , res ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( i == j && res [ i ] [ j ] != 1 ) return false ; if ( i != j && res [ i ] [ j ] != 0 ) return false ; } } return true ; } int main ( ) { int mat [ N ] [ N ] = { { 1 , 0 , 0 } , { 0 , -1 , 0 } , { 0 , 0 , -1 } } ; if ( InvolutoryMatrix ( mat ) ) cout << " Involutory ▁ Matrix " ; else cout << " Not ▁ Involutory ▁ Matrix " ; return 0 ; }
Interchange elements of first and last rows in matrix | C ++ code to swap the element of first and last row and display the result ; swapping of element between first and last rows ; Driver function ; input in the array ; printing the interchanged matrix
#include <iostream> NEW_LINE using namespace std ; #define n  4 NEW_LINE void interchangeFirstLast ( int m [ ] [ n ] ) { int rows = n ; for ( int i = 0 ; i < n ; i ++ ) { int t = m [ 0 ] [ i ] ; m [ 0 ] [ i ] = m [ rows - 1 ] [ i ] ; m [ rows - 1 ] [ i ] = t ; } } int main ( ) { int m [ n ] [ n ] = { { 8 , 9 , 7 , 6 } , { 4 , 7 , 6 , 5 } , { 3 , 2 , 1 , 8 } , { 9 , 9 , 7 , 7 } } ; interchangeFirstLast ( m ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) cout << m [ i ] [ j ] << " ▁ " ; cout << endl ; } }
Construct Binary Tree from given Parent Array representation | C ++ program to construct a Binary Tree from parent array ; A tree node ; Utility function to create new Node ; Creates a node with key as ' i ' . If i is root , then it changes root . If parent of i is not created , then it creates parent first ; If this node is already created ; Create a new node and set created [ i ] ; If ' i ' is root , change root pointer and return ; If parent is not created , then create parent first ; Find parent pointer ; If this is first child of parent ; If second child ; Creates tree from parent [ 0. . n - 1 ] and returns root of the created tree ; Create an array created [ ] to keep track of created nodes , initialize all entries as NULL ; For adding new line in a program ; Utility function to do inorder traversal ; Driver method
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int key ; struct Node * left , * right ; } ; Node * newNode ( int key ) { Node * temp = new Node ; temp -> key = key ; temp -> left = temp -> right = NULL ; return ( temp ) ; } void createNode ( int parent [ ] , int i , Node * created [ ] , Node * * root ) { if ( created [ i ] != NULL ) return ; created [ i ] = newNode ( i ) ; if ( parent [ i ] == -1 ) { * root = created [ i ] ; return ; } if ( created [ parent [ i ] ] == NULL ) createNode ( parent , parent [ i ] , created , root ) ; Node * p = created [ parent [ i ] ] ; if ( p -> left == NULL ) p -> left = created [ i ] ; else p -> right = created [ i ] ; } Node * createTree ( int parent [ ] , int n ) { Node * created [ n ] ; for ( int i = 0 ; i < n ; i ++ ) created [ i ] = NULL ; Node * root = NULL ; for ( int i = 0 ; i < n ; i ++ ) createNode ( parent , i , created , & root ) ; return root ; } inline void newLine ( ) { cout << " STRNEWLINE " ; } void inorder ( Node * root ) { if ( root != NULL ) { inorder ( root -> left ) ; cout << root -> key << " ▁ " ; inorder ( root -> right ) ; } } int main ( ) { int parent [ ] = { -1 , 0 , 0 , 1 , 1 , 3 , 5 } ; int n = sizeof parent / sizeof parent [ 0 ] ; Node * root = createTree ( parent , n ) ; cout << " Inorder ▁ Traversal ▁ of ▁ constructed ▁ tree STRNEWLINE " ; inorder ( root ) ; newLine ( ) ; }
Row wise sorting in 2D array | C ++ code to sort 2D matrix row - wise ; loop for rows of matrix ; loop for column of matrix ; loop for comparison and swapping ; swapping of elements ; printing the sorted matrix ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sortRowWise ( int m [ ] [ 4 ] , int r , int c ) { for ( int i = 0 ; i < r ; i ++ ) { for ( int j = 0 ; j < c ; j ++ ) { for ( int k = 0 ; k < c - j - 1 ; k ++ ) { if ( m [ i ] [ k ] > m [ i ] [ k + 1 ] ) { swap ( m [ i ] [ k ] , m [ i ] [ k + 1 ] ) ; } } } } for ( int i = 0 ; i < r ; i ++ ) { for ( int j = 0 ; j < c ; j ++ ) cout << m [ i ] [ j ] << " ▁ " ; cout << endl ; } } int main ( ) { int m [ ] [ 4 ] = { { 9 , 8 , 7 , 1 } , { 7 , 3 , 0 , 2 } , { 9 , 5 , 3 , 2 } , { 6 , 3 , 1 , 2 } } ; int r = sizeof ( m [ 0 ] ) / sizeof ( m [ 0 ] [ 0 ] ) ; int c = sizeof ( m ) / sizeof ( m [ 0 ] ) ; sortRowWise ( m , r , c ) ; return 0 ; }
Row wise sorting in 2D array | C ++ code to sort 2D matrix row - wise ; One by one sort individual rows . ; Printing the sorted matrix ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define M  4 NEW_LINE #define N  4 NEW_LINE int sortRowWise ( int m [ M ] [ N ] ) { for ( int i = 0 ; i < M ; i ++ ) sort ( m [ i ] , m [ i ] + N ) ; for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) cout << ( m [ i ] [ j ] ) << " ▁ " ; cout << endl ; } } int main ( ) { int m [ M ] [ N ] = { { 9 , 8 , 7 , 1 } , { 7 , 3 , 0 , 2 } , { 9 , 5 , 3 , 2 } , { 6 , 3 , 1 , 2 } } ; sortRowWise ( m ) ; }
Program for Markov matrix | C ++ code to check Markov Matrix ; outer loop to access rows and inner to access columns ; Find sum of current row ; Driver Code ; Matrix to check ; calls the function check ( )
#include <iostream> NEW_LINE using namespace std ; #define n  3 NEW_LINE bool checkMarkov ( double m [ ] [ n ] ) { for ( int i = 0 ; i < n ; i ++ ) { double sum = 0 ; for ( int j = 0 ; j < n ; j ++ ) sum = sum + m [ i ] [ j ] ; if ( sum != 1 ) return false ; } return true ; } int main ( ) { double m [ 3 ] [ 3 ] = { { 0 , 0 , 1 } , { 0.5 , 0 , 0.5 } , { 1 , 0 , 0 } } ; if ( checkMarkov ( m ) ) cout << " ▁ yes ▁ " ; else cout << " ▁ no ▁ " ; }
Program to check diagonal matrix and scalar matrix | Program to check matrix is diagonal matrix or not . ; Function to check matrix is diagonal matrix or not . ; condition to check other elements except main diagonal are zero or not . ; Driver function
#include <bits/stdc++.h> NEW_LINE #define N  4 NEW_LINE using namespace std ; bool isDiagonalMatrix ( int mat [ N ] [ N ] ) { for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) if ( ( i != j ) && ( mat [ i ] [ j ] != 0 ) ) return false ; return true ; } int main ( ) { int mat [ N ] [ N ] = { { 4 , 0 , 0 , 0 } , { 0 , 7 , 0 , 0 } , { 0 , 0 , 5 , 0 } , { 0 , 0 , 0 , 1 } } ; if ( isDiagonalMatrix ( mat ) ) cout << " Yes " << endl ; else cout << " No " << endl ; return 0 ; }
Program to check diagonal matrix and scalar matrix | Program to check matrix is scalar matrix or not . ; Function to check matrix is scalar matrix or not . ; Check all elements except main diagonal are zero or not . ; Check all diagonal elements are same or not . ; Driver function ; Function call
#include <bits/stdc++.h> NEW_LINE #define N  4 NEW_LINE using namespace std ; bool isScalarMatrix ( int mat [ N ] [ N ] ) { for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) if ( ( i != j ) && ( mat [ i ] [ j ] != 0 ) ) return false ; for ( int i = 0 ; i < N - 1 ; i ++ ) if ( mat [ i ] [ i ] != mat [ i + 1 ] [ i + 1 ] ) return false ; return true ; } int main ( ) { int mat [ N ] [ N ] = { { 2 , 0 , 0 , 0 } , { 0 , 2 , 0 , 0 } , { 0 , 0 , 2 , 0 } , { 0 , 0 , 0 , 2 } } ; if ( isScalarMatrix ( mat ) ) cout << " Yes " << endl ; else cout << " No " << endl ; return 0 ; }